public class TestBaidu {

public static void login(HttpClient client){

String url = "https://passport.baidu.com/?login";

PostMethod method = new PostMethod(url);

method.addRequestHeader("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13");
method.addRequestHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
method.addRequestHeader("Referer", "https://passport.baidu.com/?login");
method.addRequestHeader("Content-Type:","application/x-www-form-urlencoded");

List<NameValuePair> list = new ArrayList<NameValuePair>();
list.add(new NameValuePair("tpl_ok",""));
list.add(new NameValuePair("next_target",""));
list.add(new NameValuePair("tpl","mn"));
list.add(new NameValuePair("skip_ok",""));
list.add(new NameValuePair("aid",""));
list.add(new NameValuePair("need_pay",""));
list.add(new NameValuePair("need_coin",""));
list.add(new NameValuePair("pay_method",""));
list.add(new NameValuePair("u","http%3A%2F%2Fwww.baidu.com%2F"));
list.add(new NameValuePair("return_method","get"));
list.add(new NameValuePair("more_param",""));
list.add(new NameValuePair("return_type",""));
list.add(new NameValuePair("psp_tt","0"));
list.add(new NameValuePair("password",密码));
list.add(new NameValuePair("safeflg","0"));
list.add(new NameValuePair("isphone","tpl0"));
list.add(new NameValuePair("username",帐号));
list.add(new NameValuePair("verifycode",""));
list.add(new NameValuePair("mem_pass","on"));

NameValuePair[] pairs = new NameValuePair[list.size()];
pairs = list.toArray(pairs);

method.setRequestBody(pairs);

try {
int responseCode = client.executeMethod(method);
System.out.println("responseCode:"+responseCode);
InputStream is = method.getResponseBodyAsStream();
byte[] b= new byte[1024];
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int length = 0;
while((length=is.read(b))!=-1){
baos.write(b, 0, length);
}
System.out.println(baos.toString());
} catch (Exception e) {
e.printStackTrace();
}
}

public static void main(String args[]){
HttpClient client = new HttpClient();
login(client);
}
}
想做个程序自动登录百度然后每隔15分钟领一次帖吧豆,但是登录不上去。
运行时,responseCode返回400,但是直接在浏览器上输入 "https://passport.baidu.com/?login" 却可以打开
高手们看看是怎么回事,谢谢了

解决方案 »

  1.   

    method.addRequestHeader("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13");是不是百度针对这些做了处理呢?能识别不是通过浏览器登陆的?
      

  2.   

    想拿你程序跑一下来着,可是好多包都没引入啊,我还在找jar包
      

  3.   

    以前研究过类似问题,诚如5楼所言,将User-Agent信息不送过去或看不出是机器发送也许就可以了