HttpClient是HttpComponents(简称为hc)项目其中的一部份,访问地址:http://hc.apache.org/
HttpClient是一个代码级的Http客户端工具,可以使用它模拟浏览器向Http服务器发送请求。使用HttpClient还需要HttpCore.后者包括Http请求与Http响应的代码封装。
Hello World
Java代码 收藏代码
public final static void main(String[] args) throws Exception {
HttpClient httpclient = new DefaultHttpClient();
try {
HttpGet httpget = new HttpGet("http://www.apache.org/");
System.out.println("executing request " + httpget.getURI());
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
System.out.println("----------------------------------------");
System.out.println(response.getStatusLine());
if (entity != null) {
System.out.println("Response content length: " + entity.getContentLength());
}
System.out.println("----------------------------------------");
InputStream inSm = entity.getContent();
Scanner inScn = new Scanner(inSm);
while (inScn.hasNextLine()) {
System.out.println(inScn.nextLine());
}
// Do not feel like reading the response body
// Call abort on the request object
httpget.abort();
} finally {
// When HttpClient instance is no longer needed,
// shut down the connection manager to ensure
// immediate deallocation of all system resources
httpclient.getConnectionManager().shutdown();
}
}
[点击查看原始大小图片]
使用HC模拟Firefox的请求
1:分析请求包中这六个头信息
2:代码
Java代码 收藏代码
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) {
HttpClient httpClient = new DefaultHttpClient();
try {
HttpGet httpget = new HttpGet("http://www.iteye.com");
httpget.setHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
httpget.setHeader("Accept-Language", "zh-cn,zh;q=0.5");
httpget.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 5.1; rv:7.0.1) Gecko/20100101 Firefox/7.0.1)");
httpget.setHeader("Accept-Encoding", "gzip, deflate");
httpget.setHeader("Accept-Charset", "GB2312,utf-8;q=0.7,*;q=0.7");
httpget.setHeader("Host", "www.iteye.com");
httpget.setHeader("Connection", "Keep-Alive");
HttpResponse response = httpClient.execute(httpget);
HttpEntity entity = response.getEntity();
System.out.println("----------------------------------------");
System.out.println(response.getStatusLine());
if (entity != null) {
System.out.println("Response content length: " + entity.getContentLength());
}
System.out.println("----------------------------------------");
InputStream inSm = entity.getContent();
Scanner inScn = new Scanner(inSm);
while (inScn.hasNextLine()) {
System.out.println(inScn.nextLine());
}
// Do not feel like reading the response body
// Call abort on the request object
httpget.abort();
} catch (Exception e) {
e.printStackTrace();
} finally {
// When HttpClient instance is no longer needed,
// shut down the connection manager to ensure
// immediate deallocation of all system resources
httpClient.getConnectionManager().shutdown();
}
}
3:效果
[点击查看原始大小图片]
分享到:
相关推荐
模拟登录 不提供用户名密码
HttpClient模拟登录实例,模拟登录网站,获取用户信息,登录后数据,带验证码登录,带解析HTML实例。
HttpClient模拟http发送post和get请求
httpclient模拟登陆,请大家快过来赶紧下载哦。
NULL 博文链接:https://shaozhen.iteye.com/blog/950472
本人的测试用,包括压缩及解压缩,完整模拟,处女作,不想做伸手党
httpclient模拟登录淘宝源码,测试可用。5分重别人那下下来的,jar包没有的我都加全了 httpclient模拟登录淘宝源码,测试可用。5分重别人那下下来的,jar包没有的我都加全了
情景描述 将一个时间转换为对应的unix时间戳, ... ...除此之外,我们对请求添加签名的校验,key设置为X-Sign 接口定义 /// /// 获取时间戳(毫秒). .../// <param name=dateTime></param> /// <returns></returns> ...
httpclient模拟post请求json封装表单数据的实现方法 httpclient模拟post请求json封装表单数据的实现方法
当然了,正如前面说到的,如果我们自己使用java.net.HttpURLConnection来搞定这些问题是很恐怖的事情,因此在开始之前我们先要介绍一下一个开放源码的项目,这个项目就是Apache开源组织中的httpclient,它隶属于...
此资源为新浪微博和腾讯微博的HttpClient4模拟登录的代码Sina.java为新浪的,TencentClient.java为腾讯的。
用HttpClient来模拟浏览器GET_POST,技术简单学!!
httpclient模拟登陆代码以及相关的jar包
httpclient发送模拟工具,可是想post,get等方式的发送,简单易用,通俗易懂
NULL 博文链接:https://lxzqz.iteye.com/blog/2051318
Jsoup+httpclient模拟登陆和抓取页面.pdf
JAVA-用HttpClient来模拟浏览器GET,POST.docx
httpclient4 微信公众账号模拟登陆,网络上看到的,觉得不错上传出来,供大家一起共同分享学习。
模拟登陆方正系统,在此基础上可以做开发类似课程格子之类的软件供个人或者多人之用
NULL 博文链接:https://gary0416.iteye.com/blog/900498