HttpClient4.4.1模拟登录知乎

王朝学院·作者佚名  2016-05-28
窄屏简体版  字體: 小  |  中  |  大  |  超大  

HttpClient4.4.1模拟登录知乎HttpClient4.4.1模拟登录知乎一,登录要Post的表单数据是什么这部分可以使用Wireshark工具来抓包就可以了,发现需要以下数据:

"_xsrf" = xxxx(这是一个变动的数据,需要先活取获取知乎首页源码来获得)"email" = 邮箱"passWord" = 密码"rememberme" = "y"(或者n也可以)

获取_xsrf数据:

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 get = new HttpGet("http://www.zhihu.com/"); try { CloseableHttPResponse response = httpClient.execute(get); String responseHtml = EntityUtils.toString(response.getEntity()); String xsrfValue = responseHtml.split("<input type=\"hidden\" name=\"_xsrf\" value=\"")[1].split("\"/>")[0]; System.out.println("xsrfValue:" + 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", "y")); UrlEncodedFormEntity entity = new UrlEncodedFormEntity(valuePairs, Consts.UTF_8); HttpPost post = new HttpPost("http://www.zhihu.com/login"); 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();//设置进去

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航