2013年9月22日星期日

Sign Baidu https httpclient help simulate problems

The original use httpclient3.1 , now impossible, who can help modify the next ,

or help provide httpclient4.0 analog login code or other code ?

Thank you.


public  int publisharticle(User bu) {
       HttpClient client=null;
       HttpMethodParams params=new HttpMethodParams();
      boolean pubSuccess=false;
try {
Protocol authhttps = new Protocol("https",
          new AuthSSLProtocolSocketFactory( new URL("file:///E:/https/my.keystore"), "1088111",
          new URL("file:///E:/https/my.truststore"), "aaaaa"),
          443);
  client = new HttpClient();
  //中文乱码
String CONTENT_CHARSET = "gb2312";
 
  client.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, CONTENT_CHARSET);
  client.getHostConfiguration().setHost("passport.baidu.com", 443, authhttps);

  /* 只能使用相对路径 */
  GetMethod httpget = new GetMethod("/?login&tpl=mn");
  client.executeMethod(httpget);
 //System.out.println(new String(httpget.getResponseBody(),"gb2312"));
  PostMethod httppost=new PostMethod("https://passport.baidu.com/?login");
  httppost.setParameter("tpl_ok", "");
  httppost.setParameter("next_target", "");
  httppost.setParameter("tpl", "mn");
  httppost.setParameter("skip_ok", "");
  httppost.setParameter("aid", "");
  httppost.setParameter("need_pay", "");
  httppost.setParameter("need_coin", "");
  httppost.setParameter("pay_method", "");
  httppost.setParameter("u", "http://www.baidu.com/");
  httppost.setParameter("return_method", "get");
  httppost.setParameter("more_param", "");
  httppost.setParameter("return_type", "");
  httppost.setParameter("psp_tt", "0");
  httppost.setParameter("password", bu.getPassword());
  httppost.setParameter("safeflg", "0");
  httppost.setParameter("isphone", "tpl");
  httppost.setParameter("username", bu.getUsername());
  httppost.setParameter("verifycode", "");
  httppost.setParameter("mem_pass", "on");
 
  client.executeMethod(httppost);
  StringBuffer sb=new StringBuffer();
  Header[] heads=httppost.getResponseHeaders();
  for (int i=0;i<heads.length;i++){
     // System.out.println(i+":"+heads[i].getName()+":"+heads[i].getValue());
      if(heads[i].getName().equalsIgnoreCase("Set-Cookie")){
       sb.append(heads[i].getValue()+";");
      }
  }
 
  System.out.println("================"+new String(sb)+"======================");
  int i=0;
  InputStream ins=httppost.getResponseBodyAsStream();
  while ((i=ins.read())!=-1){
     // System.out.print(i);
  }
  httppost.abort();
} catch (MalformedURLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (HttpException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}


------ Solution ------------------------------------- -------
original can not take most of the site is now changed , increasing the parity information and the like
error can be seen in anything
------ Solution ----------------------------- ---------------
first find the reasons for it : with httpclient3.1 and httpclient4.0 both test. . Make sure they are unable to log in, and then determine Baidu updated. . Or 4.0 issue. . We go to find the problem to solve it . .

specifically how to solve . . I said: only Bangding . . . .
------ Solution ---------------------------------------- ----
landlord what action you want to do ? is to open Baidu , and then landing Baidu account , then back to Baidu homepage eh ?
------ Solution ---------------------------------------- ----
httpClient 4.0.X

Baidu coding seemed disgusting .


/**
 * 测试百度登陆
 * @author 疯子
 *
 */
public class BaiDuLogin {

private static DefaultHttpClient httpclient;
private Logger log = Logger.getLogger(this.getClass().getName());

public static void main(String[] args)
{
new BaiDuLogin();
}

public BaiDuLogin()
{
try{
httpclient = new DefaultHttpClient();
httpclient = WebClientDevWrapper.wrapClient(httpclient); 
List <NameValuePair> nvps = new ArrayList <NameValuePair>();
nvps.add(new BasicNameValuePair("aid", ""));
nvps.add(new BasicNameValuePair("isphone", "tpl"));
nvps.add(new BasicNameValuePair("mem_pass", "on"));
nvps.add(new BasicNameValuePair("password", "bafqpqtd"));
nvps.add(new BasicNameValuePair("return_method", "get"));
nvps.add(new BasicNameValuePair("safeflg", "0"));
nvps.add(new BasicNameValuePair("tpl", "mn"));
nvps.add(new BasicNameValuePair("u", "http://www.baidu.com/"));
nvps.add(new BasicNameValuePair("username", "chenfeng432"));

HttpPost httpost = new HttpPost("https://passport.baidu.com/?login");
httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
HttpResponse response = httpclient.execute(httpost);
HttpEntity entity = response.getEntity();
if (entity != null) {
log.info("登陆返回的html=="+IOUtils.toString(entity.getContent(), "GB2312"));
}

HttpGet httpget = new HttpGet("http://www.baidu.com");
response = httpclient.execute(httpget);
entity = response.getEntity();
if (entity != null) {
log.info("登陆过后再次访问百度主页返回的html:"+IOUtils.toString(entity.getContent(), "UTF-8"));
}
}catch (Exception e) {
// TODO: handle exception
log.error("出错", e);
}
}
}


------ Solution ------------------------------------- -------
landlord, you solved this problem yet ? I also encountered this problem .

------ For reference only ---------------------------------- -----
their own roof
------ For reference only --------------------------- ------------
there is no error , the method after the finish and did not log in to access Baidu home the same time .
------ For reference only -------------------------------------- -
former home is log on to access Baidu state after
------ For reference only ------------------------ ---------------
has found that , in fact, that the login code is correct, the key is to go after login

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

when we are not logged in ,

If I use : GetMethod getMethod = new GetMethod ("/"); yes you can see the login status . But the host is password.baidu.com, for a host that can not be displayed after login status , and even if I add up the cookies or not logged
------ For reference only - --------------------------------------
still did not solve the problem, thank you , knot posted .
------ For reference only -------------------------------------- -
problem solved it ?
------ For reference only -------------------------------------- -
you coincidence, I wrote a tutorial , including code , just to help you achieve what you want, a simulated landing Baidu :
[ Tutorial ] simulated landing Baidu version of Java code

If you are interested , you can also look at other languages ​​:
(1) Python
[ Tutorial ] simulated landing site of the Python version ( containing two versions of the complete runnable code )

(2) C #
[ Tutorial ] simulated landing site of the C # version ( contains two versions of the complete runnable code )

(3) go language
[ Record] language used go simulated landing Baidu


没有评论:

发表评论