求助httpclient4.X登录百度

NotHys 2013-07-26 10:35:21
httpclient更新到了4.X。使用httpclient4.x登录百度的时候总是出错119998
大神们看看源码:
public void login() throws ClientProtocolException, IOException 
{
DefaultHttpClient httpClient = new DefaultHttpClient();
httpClient.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.BROWSER_COMPATIBILITY);
//获取一个cookie
HttpGet httpget = new HttpGet("https://passport.baidu.com/v2/api/?getapi&class=login&tpl=mn&tangram=true");
try {
httpClient.execute(httpget);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
httpget.abort();
//获取登录要用到的token
String token=null;

HttpGet getToken = new HttpGet("https://passport.baidu.com/v2/api/?getapi&class=login&tpl=mn&tangram=true");
HttpResponse response = httpClient.execute(getToken);
HttpEntity entity = response.getEntity();
String html = HtmlTool.getHtml(entity);//一个函数打印entity没什么作用
Pattern tokenp=Pattern.compile("bdPass.api.params.login_token='(.*?)';");
Matcher m;
m = tokenp.matcher(html);
if(m.find()){
token=m.group(1);
}
System.out.println(token);

HttpPost httppost = new HttpPost("https://passport.baidu.com/v2/api/?login");
//构造登录请求post的数据
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
nvps.add(new BasicNameValuePair("tpl", "mn"));
nvps.add(new BasicNameValuePair("callback", "parent.bdPass.api.login._postCallback"));
nvps.add(new BasicNameValuePair("staticpage", "https://passport.baidu.com/v3Jump.html"));
nvps.add(new BasicNameValuePair("codestring", ""));
nvps.add(new BasicNameValuePair("ppui_logintime", "10484"));
nvps.add(new BasicNameValuePair("u", "https://passport.baidu.com/"));
nvps.add(new BasicNameValuePair("charset", "utf-8"));
nvps.add(new BasicNameValuePair("index", "0"));

nvps.add(new BasicNameValuePair("password", "密码"));
nvps.add(new BasicNameValuePair("loginType", "1"));
nvps.add(new BasicNameValuePair("safeflg", "0"));
nvps.add(new BasicNameValuePair("isphone", "false"));
nvps.add(new BasicNameValuePair("username", "用户名"));

nvps.add(new BasicNameValuePair("verifycode", ""));
nvps.add(new BasicNameValuePair("mem_pass", "on"));
httppost.setHeader("User-Agent","Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0)");
httppost.setHeader("Host","passport.baidu.com");
nvps.add(new BasicNameValuePair("token", token));//得到的token
nvps.add(new BasicNameValuePair("class", "login"));
nvps.add(new BasicNameValuePair("tangram", "true"));

httppost.setEntity(new UrlEncodedFormEntity(nvps,HTTP.UTF_8));
HttpResponse response2 = httpClient.execute(httppost);

String ss = HtmlTool.getHtml(response2.getEntity());
System.out.println(ss);


打印出来的信息就是119998错误https://passport.baidu.com/v3Jump.html?callback=parent.bdPass.api.login._postCallback&index=0&codestring=&username=&phonenumber=&mail=&tpl=&u=&needToModifyPassword=&gotourl=&auth=&error=119998
还有打印的cookie也只有BAIDUID-->BAIDUID=AF28C6D01F4C5377010449BFD7DE04A4:FG=1;HOSUPPORT=1
...全文
267 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
sardine0507 2013-09-19
  • 打赏
  • 举报
回复
太感谢你了,我把你获取cookie的url改成"www.baidu.com"就能登陆了也~~~好开心~~~纠结了我一晚上了~~~
NotHys 2013-07-27
  • 打赏
  • 举报
回复
都没有人给解决一下啊啊!大神们些!看看啊!
NotHys 2013-07-26
  • 打赏
  • 举报
回复
才有的号没有很多分见谅多多指教下

62,620

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧