(最新)HttpClient4模拟登录腾讯微博

        今天在查看网络抓取运行日志时候发现腾讯微博数据获取出现异常,进行跟踪之后发现之前的模拟登陆出现了问题,确切的说是腾讯进行部分代码的调整导致登陆失败。解决思路如下:

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("\""));
核心代码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());
}
 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Java中模拟登录微博,你可以使用网络请求库(如HttpClient或OkHttp)来发送HTTP请求,并模拟用户登录微博的过程。以下是一个简单的示例代码: ```java import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.NameValuePair; import org.apache.http.client.HttpClient; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.message.BasicNameValuePair; import org.apache.http.util.EntityUtils; import java.io.IOException; import java.util.ArrayList; import java.util.List; public class WeiboLogin { public static void main(String[] args) { HttpClient httpClient = HttpClientBuilder.create().build(); HttpPost httpPost = new HttpPost("https://passport.weibo.cn/sso/login"); // 设置请求参数 List<NameValuePair> params = new ArrayList<>(); params.add(new BasicNameValuePair("username", "your_username")); params.add(new BasicNameValuePair("password", "your_password")); params.add(new BasicNameValuePair("savestate", "1")); params.add(new BasicNameValuePair("r", "https://m.weibo.cn/")); params.add(new BasicNameValuePair("ec", "0")); params.add(new BasicNameValuePair("pagerefer", "https://m.weibo.cn/")); params.add(new BasicNameValuePair("entry", "mweibo")); params.add(new BasicNameValuePair("wentry", "")); params.add(new BasicNameValuePair("loginfrom", "")); params.add(new BasicNameValuePair("client_id", "")); params.add(new BasicNameValuePair("code", "")); params.add(new BasicNameValuePair("qq", "")); params.add(new BasicNameValuePair("mainpageflag", "1")); params.add(new BasicNameValuePair("hff", "")); params.add(new BasicNameValuePair("hfp", "")); try { httpPost.setEntity(new UrlEncodedFormEntity(params, "UTF-8")); // 发送登录请求 HttpResponse response = httpClient.execute(httpPost); HttpEntity entity = response.getEntity(); // 处理登录结果 if (entity != null) { String result = EntityUtils.toString(entity); System.out.println(result); // 在这里可以解析返回的结果,判断登录是否成功 } } catch (IOException e) { e.printStackTrace(); } } } ``` 请注意,这只是一个简单的示例代码,实际上模拟登录微博可能需要更多的参数和步骤。另外,模拟登录涉及到网络请求和用户隐私,需要遵守相关法律法规和网站的使用规定。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值