java httpclient 模拟登陆京东

因为京东添加了滑动验证码,以下方法已经无法使用。 

public static void main(String[] args) {
		BasicCookieStore cookieStore = new BasicCookieStore();
		MyClient client = new MyClient("GBK", cookieStore);
		client.get("https://www.jd.com/");
		client.showCookies();
		try {
			Thread.sleep(500);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		Document doc = Jsoup.parse(client.get("https://passport.jd.com/new/login.aspx"));
		client.showCookies();
		try {
			Thread.sleep(500);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
//		System.out.println(doc);
		Elements elements = doc.select("form[id=formlogin] input[type=hidden]");
//		System.out.println(client.get("https://passport.jd.com/uc/showAuthCode?r=0.7007493122946471&version=2015"));
		Map map = new HashMap();
		String k, v;
		for (Element input : elements) {
			k = input.attr("name");
			v = input.attr("value");
			if (StringUtils.isNotBlank(k)) {
				map.put(k, v);
				// System.out.println(input);
			}
		}
		map.put("loginname", "*********");
		map.put("nloginpwd", "*********");
		map.put("eid", "XJAWIOCQHEXZSYPHKMLGUTWBEP65XN5CHU74XPBRKZSYFNJWG2IKVGGGR6G6ULDKASHFVTO42PNVHBVGYYLNGB77IA");
		map.put("fp", "84ecf1100c625a2256dc64ff97ff1e77");
		
		String result = client.get("https://seq.jd.com/jseqf.html?bizId=passport_jd_com_login_pc&platform=js&version=1");
		map.put("seqSid", getPattern(result, "sessionId=.+_jdtdseq_config_data").substring(10, 29));
		try {
			Thread.sleep(500);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		String url = "https://passport.jd.com/uc/loginService?";
		url = url + "&uuid=" + map.get("uuid") + "&r=" + Math.random() + "&version=2015";
		System.out.println("url: " + url);
		String ans = client.post(url, map);
		System.out.println(ans);
		
		doc = Jsoup.parse(client.get("https://i.jd.com/user/info"));
	}

 

首先,MyClient是我自己封装的一个类,里面没什么特殊的。很多人都找不到eid、fp和seqSid.

其中eid和fp可以固定,我就是用谷歌得到后,将httpclient的header弄成谷歌浏览器请求是的一样。

中间加了几个延时,如果不加延时会要求输入验证码。时长我随便写的。

 

大家有兴趣可以参考下。

你可能感兴趣的:(java httpclient 模拟登陆京东)