Skip to content

Commit c388bdf

Browse files
committed
更新登陆知乎
1 parent 2d209de commit c388bdf

File tree

1 file changed

+28
-5
lines changed

1 file changed

+28
-5
lines changed

Main.java

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,33 +8,56 @@
88
import org.apache.http.client.methods.HttpPost;
99
import org.apache.http.impl.client.CloseableHttpClient;
1010
import org.apache.http.impl.client.HttpClients;
11+
import org.apache.http.impl.client.SystemDefaultCredentialsProvider;
1112
import org.apache.http.message.BasicNameValuePair;
1213
import org.apache.http.util.EntityUtils;
1314

15+
import java.io.File;
16+
import java.io.FileOutputStream;
1417
import java.io.IOException;
1518
import java.util.LinkedList;
1619
import java.util.List;
20+
import java.util.Scanner;
1721

1822
public class Main {
1923
public static void main(String[] args) {
2024
RequestConfig requestConfig = RequestConfig.custom().setCookieSpec(CookieSpecs.STANDARD_STRICT).build();
2125
CloseableHttpClient httpClient = HttpClients.custom().setDefaultRequestConfig(requestConfig).build();
2226

23-
HttpGet get = new HttpGet("http://www.zhihu.com/");
27+
HttpGet getHomePage = new HttpGet("http://www.zhihu.com/");
2428
try {
25-
CloseableHttpResponse response = httpClient.execute(get);
29+
//填充登陆请求中基本的参数
30+
CloseableHttpResponse response = httpClient.execute(getHomePage);
2631
String responseHtml = EntityUtils.toString(response.getEntity());
2732
String xsrfValue = responseHtml.split("<input type=\"hidden\" name=\"_xsrf\" value=\"")[1].split("\"/>")[0];
28-
System.out.println("xsrfValue:" + xsrfValue);
33+
System.out.println("_xsrf:" + xsrfValue);
2934
response.close();
3035
List<NameValuePair> valuePairs = new LinkedList<NameValuePair>();
3136
valuePairs.add(new BasicNameValuePair("_xsrf" , xsrfValue));
3237
valuePairs.add(new BasicNameValuePair("email", 用户名));
3338
valuePairs.add(new BasicNameValuePair("password", 密码));
34-
valuePairs.add(new BasicNameValuePair("rememberme", "y"));
39+
valuePairs.add(new BasicNameValuePair("rememberme", "true"));
3540

41+
//获取验证码
42+
HttpGet getCaptcha = new HttpGet("http://www.zhihu.com/captcha.gif?r=" + System.currentTimeMillis() + "&type=login");
43+
CloseableHttpResponse imageResponse = httpClient.execute(getCaptcha);
44+
FileOutputStream out = new FileOutputStream("/tmp/zhihu.gif");
45+
byte[] bytes = new byte[8192];
46+
int len;
47+
while ((len = imageResponse.getEntity().getContent().read(bytes)) != -1) {
48+
out.write(bytes,0,len);
49+
}
50+
out.close();
51+
52+
//请用户输入验证码
53+
System.out.println("请输入验证码:");
54+
Scanner scanner = new Scanner(System.in);
55+
String captcha = scanner.next();
56+
valuePairs.add(new BasicNameValuePair("captcha", captcha));
57+
58+
//完成登陆请求的构造
3659
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(valuePairs, Consts.UTF_8);
37-
HttpPost post = new HttpPost("http://www.zhihu.com/login");
60+
HttpPost post = new HttpPost("http://www.zhihu.com/login/email");
3861
post.setEntity(entity);
3962
httpClient.execute(post);//登录
4063

0 commit comments

Comments
 (0)