http client实践
程序员文章站
2022-03-10 13:27:48
...
1 使用http client
参考网上例子登陆baidu、人人、淘宝
http://robblog.iteye.com/blog/638206
官网文档
http://hc.apache.org/httpcomponents-client-ga/examples.html
http://hc.apache.org/httpcomponents-client-ga/tutorial/html/index.html
登陆人人代码
DefaultHttpClient httpclient = new DefaultHttpClient();
try {
HttpGet httpget = new HttpGet("http://www.renren.com");
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
System.out.println("Login form get: " + response.getStatusLine());
EntityUtils.consume(entity);
System.out.println("Initial set of cookies:");
List<Cookie> cookies = httpclient.getCookieStore().getCookies();
if (cookies.isEmpty()) {
System.out.println("None");
} else {
for (int i = 0; i < cookies.size(); i++) {
System.out.println("- " + cookies.get(i).toString());
}
}
HttpPost httpost = new HttpPost("http://www.renren.com/ajaxLogin/login");
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
nvps.add(new BasicNameValuePair("_rtk", "6bca5579"));
nvps.add(new BasicNameValuePair("domain", "renren.com"));
nvps.add(new BasicNameValuePair("email", "XXXX"));
nvps.add(new BasicNameValuePair("key_id", "1"));
nvps.add(new BasicNameValuePair("origURL", "http://www.renren.com/home"));
nvps.add(new BasicNameValuePair("password", "XXXXXX"));
httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
response = httpclient.execute(httpost);
entity = response.getEntity();
System.out.println("Login form get: " + response.getStatusLine());
EntityUtils.consume(entity);
System.out.println("Post logon cookies:");
cookies = httpclient.getCookieStore().getCookies();
if (cookies.isEmpty()) {
System.out.println("None");
} else {
for (int i = 0; i < cookies.size(); i++) {
System.out.println("- " + cookies.get(i).toString());
}
}
} finally {
// When HttpClient instance is no longer needed,
// shut down the connection manager to ensure
// immediate deallocation of all system resources
httpclient.getConnectionManager().shutdown();
}
有些网站的登录稍微复杂,需要模拟js的行为,向服务端发送进一步的校验信息,如天涯,它对cookie的获取需要通过js多次向服务端发送请求。
对于这类研究,采用http watch工具可以事半功倍.
2 使用tcp,通过http watch截获发送包,用socket重发这个包,模拟http请求。
推荐阅读
-
Nginx出现The plain HTTP request was sent to HTTPS port问题解决方法
-
Nginx实现根据域名http、https分发配置示例
-
php中用socket模拟http中post或者get提交数据的示例代码
-
详解HTTP请求与响应基础及实例
-
React SSR样式及SEO的实践
-
mongodb连接配置实践
-
生产环境配置见: mongodb连接配置实践
-
MongoDB DBA 实践1-----Windows Server 单机安装
-
iOS ScrollView嵌套tableView联动滚动的思路与最佳实践
-
Node.js中的http请求客户端示例(request client)