百度模拟登陆出错

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.CookieStore;
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.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.PoolingClientConnectionManager;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.junit.Test;




public class BaiduTest {
@Test
public  void login() throws Exception{
 
// 创建默认的httpClient实例.     一个就够了
DefaultHttpClient httpclient = new DefaultHttpClient(new PoolingClientConnectionManager());
     //必做第一步,直接访问百度,cookie返回必用的 baiduid
HttpGet get = new HttpGet("http://www.baidu.com/"); 
HttpResponse response = httpclient.execute(get);
   
     //第二部用baiduid访问百度的验证机制,获取百度的token,
     HttpGet get1 = new HttpGet("https://passport.baidu.com/v2/api/?getapi&tpl=mn&apiver=v3&class=login&logintype=dialogLogin"); 
response = httpclient.execute(get1);
/*CookieStore cookieStore = httpclient.getCookieStore();  
httpclient.setCookieStore(cookieStore);*/
 
HttpEntity entity = response.getEntity();  
     //正则取出token 
     String totalString = EntityUtils.toString(entity, "UTF-8");
String token = getSubstrListByRegex(totalString,"\"token\" : \"(.*?)\"").get(0);
System.out.println(token);
//post请求模拟登陆,把写好登陆需要的参数。
HttpPost httppost = new HttpPost("https://passport.baidu.com/v2/api/?login");
    
     List<NameValuePair> list = new ArrayList<NameValuePair>();  
        list.add(new BasicNameValuePair("tt", ""+System.currentTimeMillis()));  
        list.add(new BasicNameValuePair("tpl", "mn"));  
        list.add(new BasicNameValuePair("token", token));  
        list.add(new BasicNameValuePair("isPhone", ""));  
        list.add(new BasicNameValuePair("username", "XXX"));  
        list.add(new BasicNameValuePair("password", "XXX"));  
    //    list.add(new BasicNameValuePair("verifycode", ""));  
    //    list.add(new BasicNameValuePair("codestring", ""));  
    UrlEncodedFormEntity uefEntity;  
    uefEntity = new UrlEncodedFormEntity(list, "UTF-8");  
    httppost.setEntity(uefEntity);  
    response = httpclient.execute(httppost);
    CookieStore cookieStore = httpclient.getCookieStore();  
httpclient.setCookieStore(cookieStore);
System.out.println(cookieStore);
    response = httpclient.execute(get);
/* HttpEntity entity1 = response.getEntity();
String totalString1 = EntityUtils.toString(entity1, "UTF-8");
正常判定返回的错误代码为 0   我的问题也在这里,部分账号登陆正常,返回的errorcode为0 可以继续操作
部分账号 返回的errorcode为 2  我就不知道啥意思了。。卡在这了。。
String errorcode = totalString1.split("error=")[1].substring(0, 1);
 
     if (!errorcode.equals("0")) {  
         System.out.println("errorcode ----------- " + errorcode);  
         httppost.releaseConnection();
         return;
     }
     cookieStore = httpclient.getCookieStore();  
httpclient.setCookieStore(cookieStore);
System.out.println(cookieStore);
     
     
     HttpGet get2 = new HttpGet("http://tieba.baidu.com/tbmall/onekeySignin1"); 
response = httpclient.execute(get2);*/
 
HttpEntity entity2 = response.getEntity();
     if (entity2 != null) {  
         System.out.println("-------------------------------------");  
         System.out.println("Response content: " + EntityUtils.toString(entity2, "UTF-8"));  
         System.out.println("--------------------------------------");
         
     }
     
   }

public static List<String> getSubstrListByRegex(String str,String re){
List<String> list = new ArrayList<String>();
Matcher matcher = Pattern.compile(re).matcher(str);
while(matcher.find()){
int groupCount = matcher.groupCount();//始终显示最里面组的内容
list.add(matcher.group(groupCount));
}
return list;
}
 
}

你可能感兴趣的:(百度模拟登陆出错)