httpclient4.3登陆百度登陆不上
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package net; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.UnsupportedEncodingException; import java.nio.charset.Charset; import java.util.ArrayList; import java.util.List; import org.apache.http.Header; import org.apache.http.NameValuePair; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.ContentType; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.message.BasicNameValuePair; import org.apache.http.util.EntityUtils; /** * * @author hbj */ public class LoginBaidu { CloseableHttpResponse execute; CloseableHttpClient client; HttpPost post = new HttpPost("https://passport.baidu.com/v2/?login"); HttpGet get = new HttpGet("https://passport.baidu.com/v2/api/?getapi&class=login&tpl=mn&tangram=true"); HttpGet get1 = new HttpGet("http://www.baidu.com"); public void login() throws IOException { createClient(); execute = client.execute(get); InputStream is = execute.getEntity().getContent(); Charset charset = ContentType.getOrDefault(execute.getEntity()).getCharset(); System.out.println(charset); BufferedReader br = new BufferedReader(new InputStreamReader(is, charset)); String line = null; while ((line = br.readLine()) != null) { if (line.contains("login_token")) { line = line.substring(line.indexOf("='") + 2, line.indexOf("';")); postLogin(line); break; } } execute = client.execute(post); System.out.println(execute.getStatusLine().getStatusCode()); execute = client.execute(get1); System.out.println(EntityUtils.toString(execute.getEntity())); client.close(); execute.close(); } public void postLogin(String line) throws UnsupportedEncodingException { List<NameValuePair> nvps = new ArrayList<>(); nvps.add(new BasicNameValuePair("charset", "utf-8")); nvps.add(new BasicNameValuePair("codestring", "")); nvps.add(new BasicNameValuePair("tpl", "mn")); nvps.add(new BasicNameValuePair("index", "0")); nvps.add(new BasicNameValuePair("username", "501711499@qq.com")); nvps.add(new BasicNameValuePair("password", "14435120")); nvps.add(new BasicNameValuePair("token", line)); nvps.add(new BasicNameValuePair("isPhone", "false")); nvps.add(new BasicNameValuePair("loginType", "1")); nvps.add(new BasicNameValuePair("mem_pass", "on")); nvps.add(new BasicNameValuePair("callback", "parent.bdPass.api.login._postCallback")); post.setEntity(new UrlEncodedFormEntity(nvps, "UTF-8")); } public void createClient() throws IOException { HttpClientBuilder create = HttpClientBuilder.create(); client = create.build(); execute = client.execute(get1); // System.out.println(Arrays.toString(execute.getAllHeaders())); Header[] header = execute.getHeaders("Set-Cookie"); // String s = Arrays.toString(execute.getAllHeaders()); // s = s.substring(s.indexOf("BAIDUID=")+8, s.indexOf(":FG")); // System.out.println(s); // BasicClientCookie stdCookie = new BasicClientCookie("BAIDUID", s); // stdCookie.setVersion(1); // stdCookie.setDomain(".baidu.com"); // stdCookie.setPath("/"); // stdCookie.setSecure(true); // // Set attributes EXACTLY as sent by the server // stdCookie.setAttribute(ClientCookie.VERSION_ATTR, "1"); // stdCookie.setAttribute(ClientCookie.DOMAIN_ATTR, ".baidu.com"); // BasicCookieStore cookieStore = new BasicCookieStore(); // cookieStore.addCookie(stdCookie); // create.setDefaultCookieStore(cookieStore); post.setHeaders(header); get.setHeaders(header); get1.setHeaders(header); } public static void main(String[] args) throws IOException { new LoginBaidu().login(); } }
有谁用4.3登陆成功过了吗,求指导啊
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
/**
*
* @param username 用户名
* @param password 密码
*
@return 返回验证码图片 (如果有)
*/
public BufferedImage loginBaidu(String username,String password,String codeNum)
{
//cookieStore.clear();
HttpResponse response=null;
HttpEntity entity=null;
this.username=username;
int num=0;
String token=null;
//先用Get请求访问百度主页拿到BAIDUID等相关cookie
HttpGet get_main=new HttpGet(baiduMainUrl);
try {
response=httpClient.execute(get_main,localContext);
//每次执行完一个请求 必须消耗实体内容或者 也可以中断请求 使得连接可以释放
get_main.abort();
HttpGet get_token=new HttpGet(getLoinTokenUrl);
response=httpClient.execute(get_token,localContext);
entity=response.getEntity();
String input=EntityUtils.toString(entity);
ArrayList<String> list=HttpTool.myRegex("[0-9a-z]{32}",input );
if(list.size()!=1)
{
throw new ClientProtocolException("获取token失败 登录无法继续进行");
}
token=list.get(0);
//构造表单数据
List<NameValuePair> params = new ArrayList<NameValuePair>();
if(codeNum!=null&&!codeNum.equals("")) //有验证码时需要额外加几个参数
{
params.add(new BasicNameValuePair("staticpage","http://www.baidu.com/cache/user/html/v3Jump.html"));
params.add(new BasicNameValuePair("charset","utf-8"));
params.add(new BasicNameValuePair("codestring","captchaservice"+codenum));
params.add(new BasicNameValuePair("verifycode",codeNum));
params.add(new BasicNameValuePair("callback","parent.bd__pcbs__fv6g3o"));
params.add(new BasicNameValuePair("mem_pass","on"));
}
params.add(new BasicNameValuePair("username",username));
params.add(new BasicNameValuePair("password",password));
params.add(new BasicNameValuePair("token", token));
params.add(new BasicNameValuePair("isPhone", "false"));
params.add(new BasicNameValuePair("quick_user", "0"));
params.add(new BasicNameValuePair("tt", System.currentTimeMillis()+""));
params.add(new BasicNameValuePair("loginmerge", "true"));
params.add(new BasicNameValuePair("logintype", "dialogLogin"));
params.add(new BasicNameValuePair("splogin", "rate"));
params.add(new BasicNameValuePair("mem_pass", "on"));
params.add(new BasicNameValuePair("tpl", "mn"));
params.add(new BasicNameValuePair("apiver", "v3"));
params.add(new BasicNameValuePair("u", "http://www.baidu.com/"));
params.add(new BasicNameValuePair("safeflg", "0"));
params.add(new BasicNameValuePair("ppui_logintime", "43661"));
HttpEntity formEntity=new UrlEncodedFormEntity(params,"UTF-8");
HttpPost post_login=new HttpPost(loginUrl);
post_login.setEntity(formEntity);
response=httpClient.execute(post_login,localContext);
HeaderIterator it = response.headerIterator();
while (it.hasNext()) {
if(it.next().toString().contains("Set-Cookie"))
{
num++;
}
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
String loginInfo=HttpTool.htmlToString(response.getEntity());
//如果登录失败 百度只会写入两个cookie 成功会写入6个,这里是根据写入的cookie数量判断是否登录成功
if(num>2)
{
return null;
}
else //登录失败可能是验证码问题 再处理一下验证码
{
codenum=HttpTool.mid(loginInfo, "captchaservice", "&");
if(codenum==null) //如果处理后还是没匹配到验证码字符就拋个异常吧让上层调用者看着办
{
throw new RuntimeException("未知错误");
}
else //否则就处理验证码
{
InputStream in=null;
BufferedImage checkImg=null;
codeUrl=codeUrl.replace("%codenum", codenum);
HttpGet get=new HttpGet(codeUrl);
try {
response=httpClient.execute(get,localContext);
entity=response.getEntity();
in=entity.getContent();
checkImg=ImageIO.read(in);
in.close();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
finally
{
try {
if(in!=null)
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return checkImg;
}
}
}
谢谢了,我放弃了╮(╯_╰)╭
百度登录源码,我没用过你那种方式,这是以前写的,不知道现在还能不能用。
这里面不是吗
这里面不是吗
不明header里面没有这些吧
加上header里面的参数试试,比如host,reference之类的。