如何自动登录百度

 作者  aosfather

版本  1.0

sina微博   @aosfather

         百度有很多服务,但这些服务都是必须要登录才能使用,对于个人用户倒好,但对于一些想写点工具的个人和公司来说,就是一大障碍。而且现在网上能搜到的都是以前的文章,百度的API已经改过几个版本了,现成的代码基本都无效了。于是本人追踪了百度的登录请求,并试验多次终于成功了。

百度登录过程

  1. 请求https://passport.baidu.com/v2/api/?getapi&class=login&tpl=mn&tangram=true获 取一个cookie

  2. 再次请求https://passport.baidu.com/v2/api/? getapi&class=login&tpl=mn&tangram=true获取token,解析返回的数据,通过正则表 达式匹配("bdPass.api.params.login_token='(.*?)';")得到token.

  3. 发送登录请求https://passport.baidu.com/v2/api/?login

  4. 记录下BAIDUIDBDUSSSSUDB,用于发送(另起httpclient)调用其它服务的

    URL时候用。

代码示例

代码基于httpclient3

HttpClient httpClient = new HttpClient();

httpClient.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);

//获取一个cookie

GetMethod httpget = new GetMethod("https://passport.baidu.com/v2/api/?getapi&class=login&tpl=mn&tangram=true");

httpClient.executeMethod(httpget);

 

//获取token

String token=null;

GetMethod getToken=new GetMethod("https://passport.baidu.com/v2/api/?getapi&class=login&tpl=mn&tangram=true");

httpClient.executeMethod(getToken);

Pattern tokenp=Pattern.compile("bdPass.api.params.login_token='(.*?)';");

Matcher m=tokenp.matcher(getToken.getResponseBodyAsString());

if(m.find()){

    token=m.group(1);

}

 

//登录

PostMethod httppost = new PostMethod("https://passport.baidu.com/v2/api/?login");

//构造登录请求post的数据

httppost.setParameter("tpl", "mn");

        httppost.setParameter("callback","parent.bdPass.api.login._postCallback");

        httppost.setParameter("staticpage","https://passport.baidu.com/v3Jump.html");

        httppost.setParameter("codestring","");

        httppost.setParameter("ppui_logintime", "10484");

        httppost.setParameter("u", "");

        httppost.setParameter("charset","utf-8");

        httppost.setParameter("index","0");

        httppost.setParameter("password","***************");//密码

        httppost.setParameter("loginType","1");

        httppost.setParameter("safeflg", "0");

        httppost.setParameter("isphone", "false");

        httppost.setParameter("username", "aosfather");//登录用户名

        httppost.setParameter("verifycode", "");

        httppost.setParameter("mem_pass", "on");

        httppost.setRequestHeader("User-Agent","Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0)");

        httppost.setRequestHeader("Host","passport.baidu.com");

        httppost.setParameter("token",token);//刚才解析出来的token。

        httpClient.executeMethod(httppost);

       

//获取cookie中的值

Cookie[] cookies=httpClient.getState().getCookies();

String tmpcookies= "";//;)原谅我吧,这只是个测试例子,编译器会翻译成StringBuilder的。

for(Cookie c:cookies){

      if(c.getName().equals("BAIDUID")||c.getName().equals("BDUSS")||c.getName().equals("SSUDB"))

                  tmpcookies += c.toString()+";";

}

 

 

//-------------------华丽的分割线,下面的代码用于验证,访问百度的其它服务时候,是否认为我们已经登录了--------------// 

//验证一下,访问baidu的网站,你会发现已经登录了。

//另建一个httpclient

HttpClient client=new HttpClient();

          //------------访问百度-------------//

GetMethod getMethod = new GetMethod("http://www.baidu.com");               

getMethod.setRequestHeader("cookie", tmpcookies);//设置cookie值,将登录返回的信息设定进去

int statusCode = client.executeMethod(getMethod)  ;

  if (statusCode != HttpStatus.SC_OK) 

              {  

                  System.err.println("Method failed: "  

                          + getMethod.getStatusLine());  

              } 

  //将返回信息打印出来,你会看到找到账号信息,就说明百度已经认可你登录了。否则返回的字符中有“登录 注册”的字眼。              

  byte[] responseBody = getMethod.getResponseBody();   

  System.out.println(new String(responseBody));  

 
 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章