- UID
- 662
- 积分
- 441
- TBS
- 393
- 智商
- 1572
- 节操
- 1615
- 海贝
- 120
- 阅读权限
- 30
- 在线时间
- 212 小时
- 注册时间
- 2014-1-4
- 最后登录
- 2024-7-30
|
- 写机器还早,现在也就弄着玩,等我真正实现了发个完整的代码给你,现在给你看看登录的post
- try {
- HttpGet get = new HttpGet("https://passport.baidu.com/v2/api/?getapi&class=login&tpl=mn&tangram=true");
- response = httpClient.execute(get);
- // System.out.println(response.getStatusLine().getStatusCode());
- @SuppressWarnings("unused")
- List<Cookie> cookies = cookieStore.getCookies();
- System.out.println("请求页面成功");
-
- /*以上获取初始cookie完成*/
-
- get = new HttpGet("https://passport.baidu.com/v2/api/?getapi&class=login&tpl=pp&tangram=true");
- response = httpClient.execute(get);
- // System.out.println(response.getStatusLine().getStatusCode());//打印状态码
- cookies = cookieStore.getCookies();
- entity = response.getEntity();
- BufferedReader br = new BufferedReader(new InputStreamReader(
- entity.getContent(), "utf-8"));
- String tempLine = br.readLine();
- StringBuffer sb = new StringBuffer();
- while(tempLine != null){
- if (tempLine.contains("bdPass.api.params.login_token=")) {
- sb.append(tempLine.substring(
- "bdPass.api.params.login_token=".length() + 1,
- tempLine.length() - 2));
- }
- tempLine = br.readLine();
- }
- System.out.println("[Token] >>> " + sb);
- cookies = cookieStore.getCookies();
- // Gongju.infoCookie(cookies);
- System.out.println("请求参数成功!");
-
- /*以上获取登陆需要的关键参数Token完成
- * 以下是构造POST请求进行模拟登录*/
-
- HttpPost post = new HttpPost("https://passport.baidu.com/v2/api/?login");
- List<NameValuePair> params = new ArrayList<NameValuePair>();
- params.add(new BasicNameValuePair("charset", "utf-8"));
- params.add(new BasicNameValuePair("token", sb.toString()));
- params.add(new BasicNameValuePair("tpl","pp"));
- params.add(new BasicNameValuePair("apiver","v3"));
- params.add(new BasicNameValuePair("tt","1390751409263"));
- params.add(new BasicNameValuePair("safeflg","0"));
- params.add(new BasicNameValuePair("isPhone","false"));
- params.add(new BasicNameValuePair("quick_user","0"));
- params.add(new BasicNameValuePair("logintype","basicLogin"));
- params.add(new BasicNameValuePair("username",username));
- params.add(new BasicNameValuePair("password",password));
- params.add(new BasicNameValuePair("isPhone","false"));
- params.add(new BasicNameValuePair("mem_pass","on"));
- params.add(new BasicNameValuePair("verifycode",""));
- params.add(new BasicNameValuePair("callback","parent.bd__pcbs__axjnsn"));
-
- UrlEncodedFormEntity uef;
- uef = new UrlEncodedFormEntity(params,"utf-8");
- post.setEntity(uef);
- response = httpClient.execute(post);
- System.out.println("[POST]Receive >>> " +response.getStatusLine().getStatusCode());
- cookies =cookieStore.getCookies();
- entity = response.getEntity();
- String err=EntityUtils.toString(entity, "utf-8");
- err = err.substring(err.indexOf("err_no=")+7,err.indexOf("&callback"));
- System.out.println(err);
- if (err.equals("0")) {
- System.out.println("登录成功!");
- }
- else if(err.equals("257"))
- {
- System.out.println("密码错误!");
- }
- else {
- System.out.println("未知错误!");
- }
- // System.out.println(EntityUtils.toString(entity, "utf-8"));
- // System.out.println("Cookies >>> " + cookies.size());
- Gongju.infoCookie(cookies);
- }
- catch (IOException e) {
- System.out.println("登录失败!");
- }
复制代码 @阿星
|
|