100

//今天整理了一下top的sessionkey的获取方法,上次在论坛看到一位高人写的,收录!

 

 

package com.carver.tool;

import java.net.URLEncoder;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
import com.gargoylesoftware.htmlunit.Page;
import com.gargoylesoftware.htmlunit.TextPage;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlButton;
import com.gargoylesoftware.htmlunit.html.HtmlCheckBoxInput;
import com.gargoylesoftware.htmlunit.html.HtmlElement;
import com.gargoylesoftware.htmlunit.html.HtmlForm;
import com.gargoylesoftware.htmlunit.html.HtmlImageInput;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.html.HtmlPasswordInput;
import com.gargoylesoftware.htmlunit.html.HtmlTextInput;


public class LoginUtil {

    private static final String ONLINE_CONTAINER = "http://container.open.taobao.com/container?appkey=";
    private static final Pattern P_SESSION = Pattern.compile("top\\_session=(\\w+?)&");

    static {
        Logger.getLogger("com.gargoylesoftware.htmlunit").setLevel(Level.SEVERE);
        Logger.getLogger("org.apache.commons.httpclient.HttpMethodBase").setLevel(Level.SEVERE);
    }

    public static void main(String[] args) throws Exception {
        String session = getOnlineSession("app_key", "username", "password");
        System.out.println("session_key: " + session);
    }

    public static String getOnlineSession(String appkey, String uid, String pwd) throws Exception {
        String url = "http://member1.taobao.com/member/mini_login.htm?login_type=3&redirect_url=";
        return getSession(url, ONLINE_CONTAINER, appkey, uid, pwd);
    }

    private static String getSession(String loginUrl, String topUrl, String appkey, String uid,
            String pwd) throws Exception {
        WebClient webClient = new WebClient(BrowserVersion.FIREFOX_3);

        String fullUrl = loginUrl + URLEncoder.encode(topUrl + appkey, "utf-8");
        HtmlPage loginPage = webClient.getPage(fullUrl);
        HtmlForm loginForm = loginPage.getForms().get(0);
        HtmlTextInput uidInput = loginForm.getInputByName("TPL_username");
        uidInput.setValueAttribute(uid);
        HtmlPasswordInput pwdInput = loginForm.getInputByName("TPL_password");
        pwdInput.setValueAttribute(pwd);
        HtmlButton loginButton = loginForm.getButtonByName("");

        HtmlPage loginRsp = null;
        try {
            Page rsp = loginButton.click();
            if (rsp instanceof HtmlPage) {
                loginRsp = (HtmlPage) rsp;
            } else if (rsp instanceof TextPage) {
                TextPage textRsp = (TextPage) rsp;
                return extractSession(textRsp.getContent());
            } else {
                return null;
            }
        } catch (FailingHttpStatusCodeException e) {
            if (e.getStatusCode() == 404) {
                return extractSession(e.getMessage());
            }
        } catch (Exception e) {
        }

        HtmlCheckBoxInput agreeCheck = (HtmlCheckBoxInput) loginRsp.getElementById("agreement");
        if (agreeCheck == null) {
            return extractSession(loginRsp.getWebResponse().getRequestSettings().getUrl().toString());
        } else {
            agreeCheck.click();
        }

        HtmlForm agreeForm = loginRsp.getForms().get(1);
        List<HtmlElement> inputElements = agreeForm.getHtmlElementsByTagName("input");
        HtmlImageInput agreeButton = null;
        for (HtmlElement inputElement : inputElements) {
            if (inputElement instanceof HtmlImageInput) {
                agreeButton = (HtmlImageInput) inputElement;
                break;
            }
        }

        Page agreeRsp = null;
        try {
            agreeRsp = agreeButton.click();
        } catch (FailingHttpStatusCodeException e) {
            if (e.getStatusCode() == 404) {
                return extractSession(e.getMessage());
            }
        } catch (Exception e) {
        }
        return extractSession(agreeRsp.getWebResponse().getRequestSettings().getUrl().toString());
    }

    private static String extractSession(String response) {
        Matcher matcher = P_SESSION.matcher(response);
        if (matcher.find()) {
            return matcher.group(1);
        } else {
            return null;
        }
    }

}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值