forked from gaopu/Java
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
Latest commit
79 lines (72 loc) · 3.61 KB
/
Copy pathMain.java
File metadata and controls
79 lines (72 loc) · 3.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
importorg.apache.http.Consts;
importorg.apache.http.NameValuePair;
importorg.apache.http.client.config.CookieSpecs;
importorg.apache.http.client.config.RequestConfig;
importorg.apache.http.client.entity.UrlEncodedFormEntity;
importorg.apache.http.client.methods.CloseableHttpResponse;
importorg.apache.http.client.methods.HttpGet;
importorg.apache.http.client.methods.HttpPost;
importorg.apache.http.impl.client.CloseableHttpClient;
importorg.apache.http.impl.client.HttpClients;
importorg.apache.http.impl.client.SystemDefaultCredentialsProvider;
importorg.apache.http.message.BasicNameValuePair;
importorg.apache.http.util.EntityUtils;
importjava.io.File;
importjava.io.FileOutputStream;
importjava.io.IOException;
importjava.util.LinkedList;
importjava.util.List;
importjava.util.Scanner;
publicclassMain {
publicstaticvoidmain(String[] args) {
RequestConfigrequestConfig = RequestConfig.custom().setCookieSpec(CookieSpecs.STANDARD_STRICT).build();
CloseableHttpClienthttpClient = HttpClients.custom().setDefaultRequestConfig(requestConfig).build();
HttpGetgetHomePage = newHttpGet("http://www.zhihu.com/");
try {
//填充登陆请求中基本的参数
CloseableHttpResponseresponse = httpClient.execute(getHomePage);
StringresponseHtml = EntityUtils.toString(response.getEntity());
StringxsrfValue = responseHtml.split("<input type=\"hidden\" name=\"_xsrf\" value=\"")[1].split("\"/>")[0];
System.out.println("_xsrf:" + xsrfValue);
response.close();
List<NameValuePair> valuePairs = newLinkedList<NameValuePair>();
valuePairs.add(newBasicNameValuePair("_xsrf" , xsrfValue));
valuePairs.add(newBasicNameValuePair("email", 用户名));
valuePairs.add(newBasicNameValuePair("password", 密码));
valuePairs.add(newBasicNameValuePair("rememberme", "true"));
//获取验证码
HttpGetgetCaptcha = newHttpGet("http://www.zhihu.com/captcha.gif?r=" + System.currentTimeMillis() + "&type=login");
CloseableHttpResponseimageResponse = httpClient.execute(getCaptcha);
FileOutputStreamout = newFileOutputStream("/tmp/zhihu.gif");
byte[] bytes = newbyte[8192];
intlen;
while ((len = imageResponse.getEntity().getContent().read(bytes)) != -1) {
out.write(bytes,0,len);
}
out.close();
Runtime.getRuntime().exec("eog /tmp/zhihu.gif");//ubuntu下看图片的命令是eog
//请用户输入验证码
System.out.print("请输入验证码:");
Scannerscanner = newScanner(System.in);
Stringcaptcha = scanner.next();
valuePairs.add(newBasicNameValuePair("captcha", captcha));
//完成登陆请求的构造
UrlEncodedFormEntityentity = newUrlEncodedFormEntity(valuePairs, Consts.UTF_8);
HttpPostpost = newHttpPost("http://www.zhihu.com/login/email");
post.setEntity(entity);
httpClient.execute(post);//登录
HttpGetg = newHttpGet("http://www.zhihu.com/question/following");//获取“我关注的问题”页面
CloseableHttpResponser = httpClient.execute(g);
System.out.println(EntityUtils.toString(r.getEntity()));
r.close();
} catch (IOExceptione) {
e.printStackTrace();
} finally {
try {
httpClient.close();
} catch (IOExceptione) {
e.printStackTrace();
}
}
}
}