|
8 | 8 | import org.apache.http.client.methods.HttpPost;
|
9 | 9 | import org.apache.http.impl.client.CloseableHttpClient;
|
10 | 10 | import org.apache.http.impl.client.HttpClients;
|
| 11 | +import org.apache.http.impl.client.SystemDefaultCredentialsProvider; |
11 | 12 | import org.apache.http.message.BasicNameValuePair;
|
12 | 13 | import org.apache.http.util.EntityUtils;
|
13 | 14 |
|
| 15 | +import java.io.File; |
| 16 | +import java.io.FileOutputStream; |
14 | 17 | import java.io.IOException;
|
15 | 18 | import java.util.LinkedList;
|
16 | 19 | import java.util.List;
|
| 20 | +import java.util.Scanner; |
17 | 21 |
|
18 | 22 | public class Main {
|
19 | 23 | public static void main(String[] args) {
|
20 | 24 | RequestConfig requestConfig = RequestConfig.custom().setCookieSpec(CookieSpecs.STANDARD_STRICT).build();
|
21 | 25 | CloseableHttpClient httpClient = HttpClients.custom().setDefaultRequestConfig(requestConfig).build();
|
22 | 26 |
|
23 |
| - HttpGet get = new HttpGet("http://www.zhihu.com/"); |
| 27 | + HttpGet getHomePage = new HttpGet("http://www.zhihu.com/"); |
24 | 28 | try {
|
25 |
| - CloseableHttpResponse response = httpClient.execute(get); |
| 29 | + //填充登陆请求中基本的参数 |
| 30 | + CloseableHttpResponse response = httpClient.execute(getHomePage); |
26 | 31 | String responseHtml = EntityUtils.toString(response.getEntity());
|
27 | 32 | 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); |
29 | 34 | response.close();
|
30 | 35 | List<NameValuePair> valuePairs = new LinkedList<NameValuePair>();
|
31 | 36 | valuePairs.add(new BasicNameValuePair("_xsrf" , xsrfValue));
|
32 | 37 | valuePairs.add(new BasicNameValuePair("email", 用户名));
|
33 | 38 | valuePairs.add(new BasicNameValuePair("password", 密码));
|
34 |
| - valuePairs.add(new BasicNameValuePair("rememberme", "y")); |
| 39 | + valuePairs.add(new BasicNameValuePair("rememberme", "true")); |
35 | 40 |
|
| 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 | + //完成登陆请求的构造 |
36 | 59 | 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"); |
38 | 61 | post.setEntity(entity);
|
39 | 62 | httpClient.execute(post);//登录
|
40 | 63 |
|
|
0 commit comments