一,登錄要Post的表單數據是什么
這部分可以使用Wireshark工具來抓包就可以了,發現需要以下數據:
“_xsrf” = xxxx(這是一個變動的數據,需要先活取獲取知乎首頁源碼來獲得)
“email” = 郵箱
“password” = 密碼
“rememberme” = “true”
“captcha” = 驗證碼(知乎有兩種驗證碼,你們可以去看,我使用的是數字字符的那種驗證碼)
String xsrfValue = responseHtml.split("<input type=\"hidden\" name= \"_xsrf\" value=\"")[1].split("\"/>")[0];
responseHtml是首頁的源碼,根據網頁的組織形式,把_xsrf數據分割出來。
RequestConfig requestConfig = RequestConfig.custom().setCookieSpec(CookieSpecs.STANDARD_STRICT).build();
CloseableHttpClient httpClient = HttpClients.custom().setDefaultRequestConfig(requestConfig).build();
HttpGet getHomePage = new HttpGet("http://www.zhihu.com/");
try { //填充登陸請求中基本的參數
CloseableHttpResponse response = httpClient.execute(getHomePage);
String responseHtml = EntityUtils.toString(response.getEntity());
String xsrfValue = responseHtml.split("<input type=\"hidden\" name=\"_xsrf\" value=\"")[1].split("\"/>")[0];
System.out.println("_xsrf:" + xsrfValue);
response.close();
List<NameValuePair> valuePairs = new LinkedList<NameValuePair>();
valuePairs.add(new BasicNameValuePair("_xsrf" , xsrfValue));
valuePairs.add(new BasicNameValuePair("email", 用戶名));
valuePairs.add(new BasicNameValuePair("password", 密碼));
valuePairs.add(new BasicNameValuePair("rememberme", "true")); //獲取驗證碼
HttpGet getCaptcha = new HttpGet("http://www.zhihu.com/captcha.gif?r=" + System.currentTimeMillis() + "&type=login");
CloseableHttpResponse imageResponse = httpClient.execute(getCaptcha);
FileOutputStream out = new FileOutputStream("/tmp/zhihu.gif");
byte[] bytes = new byte[8192];
int len;
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("請輸入驗證碼:");
Scanner scanner = new Scanner(System.in);
String captcha = scanner.next();
valuePairs.add(new BasicNameValuePair("captcha", captcha)); //完成登陸請求的構造
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(valuePairs, Consts.UTF_8);
HttpPost post = new HttpPost("http://www.zhihu.com/login/email");
post.setEntity(entity); httpClient.execute(post);//登錄
HttpGet g = new HttpGet("http://www.zhihu.com/question/following");//獲取“我關注的問題”頁面
CloseableHttpResponse r = httpClient.execute(g);
System.out.println(EntityUtils.toString(r.getEntity()));
r.close(); } catch (IOException e) {
e.printStackTrace(); } finally {
try { httpClient.close(); }
catch (IOException e) {
e.printStackTrace();
}
}
此處要注意開頭的RequestConfig,我一開始是沒有設置cookie這方面的額內容的,結果一直提示有cookie錯誤,所以查看了HttpClient手冊,上面提到了選擇Cookie策略,通過這種方法設置一個全局的Cookie策略,
RequestConfig requestConfig = RequestConfig.custom().setCookieSpec(CookieSpecs.STANDARD_STRICT).build();//標準Cookie策略
CloseableHttpClient httpClient = HttpClients.custom().setDefaultRequestConfig(requestConfig).build();//設置進去