今天在查看网络抓取运行日志时候发现腾讯微博数据获取出现异常,进行跟踪之后发现之前的模拟登陆出现了问题,确切的说是腾讯进行部分代码的调整导致登陆失败。解决思路如下:
1. 查看一下请求地址及参数是否发生变化,如下图:
结论:action、login_sig这两个参数导致登陆失败
2. 分析action、login_sig参数的来源
通过http://jsbeautifier.org/在线格式化工具进行格式化,最终的文件:h_login_11.rar 找到action参数的处理逻辑,仅仅是记录用户键盘行为,不会对登陆有影响,对应就只剩下login_sig这个参数了,但是在h_login_11.js文件中没有发现这个参数,经过分析推测不在js文件中,那就有可能在html中包含的script标签中,经过查看发现如下:
至此,参数确定完毕。
3.基于httpclient 4 来实现参见附件Weibo.rar
核心代码1 写道
/********************* 获取login_sig***********************/
HttpGet preget = new HttpGet("http://ui.ptlogin2.qq.com/cgi-bin/login?appid=46000101&style=13&lang=&low_login=1&hide_title_bar=1&hide_close_icon=1&self_regurl=http%3A//reg.t.qq.com/index.php&s_url=http%3A%2F%2Ft.qq.com&daid=6");
preget.setHeader("Host", "ui.ptlogin2.qq.com");
preget.setHeader("Referer", "http://t.qq.com/?from=11");
HttpResponse response = client.execute(preget);
String entity = EntityUtils.toString(response.getEntity());
String t=entity.substring(entity.indexOf("login_sig:"), entity.indexOf("clientip:"));
String login_sig=t.substring(t.indexOf("\"")+1, t.lastIndexOf("\""));
HttpGet preget = new HttpGet("http://ui.ptlogin2.qq.com/cgi-bin/login?appid=46000101&style=13&lang=&low_login=1&hide_title_bar=1&hide_close_icon=1&self_regurl=http%3A//reg.t.qq.com/index.php&s_url=http%3A%2F%2Ft.qq.com&daid=6");
preget.setHeader("Host", "ui.ptlogin2.qq.com");
preget.setHeader("Referer", "http://t.qq.com/?from=11");
HttpResponse response = client.execute(preget);
String entity = EntityUtils.toString(response.getEntity());
String t=entity.substring(entity.indexOf("login_sig:"), entity.indexOf("clientip:"));
String login_sig=t.substring(t.indexOf("\"")+1, t.lastIndexOf("\""));
核心代码2 写道
response = client.execute(get);
entity = EntityUtils.toString(response.getEntity());
/************************* login_sig check ****************************/
String term=entity.split(",")[2];
String url=term.substring(term.indexOf("\'")+1, term.lastIndexOf("\'"));
if (entity.indexOf("登录成功") > -1) {
get = new HttpGet(url);
response = client.execute(get);
entity = EntityUtils.toString(response.getEntity());
}
entity = EntityUtils.toString(response.getEntity());
/************************* login_sig check ****************************/
String term=entity.split(",")[2];
String url=term.substring(term.indexOf("\'")+1, term.lastIndexOf("\'"));
if (entity.indexOf("登录成功") > -1) {
get = new HttpGet(url);
response = client.execute(get);
entity = EntityUtils.toString(response.getEntity());
}
相关推荐
此资源为新浪微博和腾讯微博的HttpClient4模拟登录的代码Sina.java为新浪的,TencentClient.java为腾讯的。
httpclient4模拟登陆新浪微博 最新版
HttpClient4.3模拟登陆新版12306
httpclient4之百度模拟登陆,回复与58同城自动登陆
httpclient4 微信公众账号模拟登陆,网络上看到的,觉得不错上传出来,供大家一起共同分享学习。
httpclient方式登陆,以及发布微博
httpclient模拟登陆,请大家快过来赶紧下载哦。
Jsoup+httpclient模拟登陆和抓取页面.pdf
release.7z:SDK编译出来的jar包,含commons-codec.jar,commons-httpclient-3.1.jar,commons-logging-1.1.jar,dom4j-1.6.1.jar,QWeiboSDK.jar QWeiboSDK.7z:SDK源码 QWeibo4j.7z:java 平台调用demo QWeibo4...
httpclient模拟登陆代码以及相关的jar包
模拟登陆方正系统,在此基础上可以做开发类似课程格子之类的软件供个人或者多人之用
httpClient4 最新帮助文档
用HttpClient来模拟浏览器GET_POST,技术简单学!!
模拟登录 不提供用户名密码
HttpClient模拟登录实例,模拟登录网站,获取用户信息,登录后数据,带验证码登录,带解析HTML实例。
当然了,正如前面说到的,如果我们自己使用java.net.HttpURLConnection来搞定这些问题是很恐怖的事情,因此在开始之前我们先要介绍一下一个开放源码的项目,这个项目就是Apache开源组织中的httpclient,它隶属于...
NULL 博文链接:https://jbeduhai.iteye.com/blog/1587228
帮助程序员快速从Apache的HttpClient 3.x升级到HttpClient 4.x
2016年4月7最新新浪微博模拟登录HttpClient4.5.2