httpclient模拟登陆微博问题
来源:互联网 发布:php 求好评率 编辑:程序博客网 时间:2024/05/06 13:24
我用httpclient模拟登陆微博报如下错误:
[DEBUG] RequestAddCookies - Cookie [version: 0][name: USRHAWB][value: usrmdins212_186][domain: weibo.com][path: /][expiry: null] match [weibo.com:80/login.php]
[DEBUG] RequestAddCookies - Cookie [version: 0][name: USRHAJAWB][value: usrmdins13148][domain: weibo.com][path: /][expiry: null] match [weibo.com:80/login.php]
我的代码:
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.apache.commons.codec.binary.Base64;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.params.CookiePolicy;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
public class Sina {
/**
* @param args
*/
public static void main(String[] args) {
DefaultHttpClient client = new DefaultHttpClient();
client.getParams().setParameter("http.protocol.cookie-policy",
CookiePolicy.BROWSER_COMPATIBILITY);
client.getParams().setParameter(
HttpConnectionParams.CONNECTION_TIMEOUT, 5000);
try {
HttpPost post = new HttpPost(
"http://login.sina.com.cn/sso/login.php?client=ssologin.js(v1.3.19)");
//新加
post.setHeader("User-Agent","Mozilla/5.0 (X11;Linux i686;rv:5.0) Gecko/20100101 firefox/5.0");
post.setHeader("Referer","htpp://weibo.com");
post.setHeader("Content-Type","application/x-www-form-urlencoded");
String data = getServerTime();
String nonce = makeNonce(6);
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
nvps.add(new BasicNameValuePair("entry", "weibo"));
nvps.add(new BasicNameValuePair("gateway", "1"));
nvps.add(new BasicNameValuePair("from", ""));
nvps.add(new BasicNameValuePair("savestate", "7"));
nvps.add(new BasicNameValuePair("useticket", "1"));
nvps.add(new BasicNameValuePair("ssosimplelogin", "1"));
nvps.add(new BasicNameValuePair("su",
encodeAccount("***@sina.com"))); //用户名
nvps.add(new BasicNameValuePair("service", "miniblog"));
nvps.add(new BasicNameValuePair("servertime", data));
nvps.add(new BasicNameValuePair("nonce", nonce));
nvps.add(new BasicNameValuePair("pwencode", "wsse"));
nvps.add(new BasicNameValuePair("sp", new SinaSSOEncoder().encode(
"****", data, nonce)));//密码
nvps.add(new BasicNameValuePair(
"url",
"http://weibo.com/ajaxlogin.php?framelogin=1&callback=parent.sinaSSOController.feedBackUrlCallBack"));
nvps.add(new BasicNameValuePair("returntype", "META"));
nvps.add(new BasicNameValuePair("encoding", "UTF-8"));
nvps.add(new BasicNameValuePair("vsnval", ""));
post.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
HttpResponse response = client.execute(post);
String entity = EntityUtils.toString(response.getEntity());
//http://weibo.com/ajaxlogin.php?framelogin=1&callback=parent.sinaSSOController.feedBackUrlCallBack&retcode=2092&reason=%B1%A7%C7%B8%A3%A1%B5%C7%C2%BC%CA%A7%B0%DC%A3%AC%C7%EB%C9%D4%BA%F2%D4%D9%CA%D4'"
String url = entity.substring(entity
.indexOf("http://weibo.com/ajaxlogin.php?"), entity
.indexOf(";\"/>"));
System.out.println("aaaaaaaaaaaaaaaa::" + url);
// 获取到实际url进行连接
HttpGet getMethod = new HttpGet(url);
response = client.execute(getMethod);
entity = EntityUtils.toString(response.getEntity());
entity = entity.substring(entity.indexOf("userdomain") + 13, entity
.lastIndexOf("\""));
getMethod = new HttpGet("http://weibo.com/u/2801870824?topnav=1");
response = client.execute(getMethod);
entity = EntityUtils.toString(response.getEntity());
// Document doc =
// Jsoup.parse(EntityUtils.toString(response.getEntity()));
//System.out.println(entity);
} catch (Exception e) {
// TODO: handle exception
}
}
private static String encodeAccount(String account) {
String userName = "";
try {
userName = Base64.encodeBase64(URLEncoder.encode(account, "UTF-8").getBytes()).toString();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return userName;
}
private static String makeNonce(int len) {
String x = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
String str = "";
for (int i = 0; i < len; i++) {
str += x.charAt((int) (Math.ceil(Math.random() * 1000000) % x
.length()));
}
return str;
}
private static String getServerTime() {
long servertime = new Date().getTime() / 1000;
return String.valueOf(servertime);
}
}
请高手指点下!
- httpclient模拟登陆微博问题
- 使用httpclient模拟登陆时乱码问题
- httpclient模拟登陆,不支持https的问题
- httpclient模拟登陆,不支持https的问题
- httpclient 模拟登陆
- HttpClient模拟登陆例子
- PHP HttpClient模拟登陆
- 使用httpclient模拟登陆
- HttpClient模拟登陆
- httpclient模拟登陆CSDN
- 2015腾讯微博登陆解密基于httpclient抓包模拟登陆
- 新浪模拟登陆 HttpClient 4 cookie rejected 问题
- httpclient模拟登陆的学习
- HTTPClient模拟登陆人人网
- HTTPClient模拟登陆人人网
- HTTPClient模拟登陆人人网
- httpclient模拟登陆facebook.com
- httpclient模拟登陆操作实现
- XML CDATA是什么?
- Revit 2013 API新功能讲座--免费网络视频讲座5月31日下午2:00 (已结束,等待录制的视频下载)
- 编译程序和解释程序
- 解析html下的xml文件
- Android 平台上蓝牙开发的关于 UUID 设置的注意事项
- httpclient模拟登陆微博问题
- Linux下常用I/O模型
- 交叉编译python3.2
- 微博文摘——黑客的操作系统你敢用不?
- ISV客户博客系列:Windows Azure上的Softlibrary 和 Kern4Cloud
- C++中类的声明
- Model与UIControl之间的模型关系
- 比Detours更给力的Hook库
- 有个朋友的私信