Apache HttpClient 4.3开发指南
2016-10-26 14:09
453 查看
《Apache HttpClient 4.3开发指南》
作者:chszs,转载需注明。博客主页:http://blog.csdn.NET/chszs一、概述
Apache HttpClient 4系列已经发布很久了,但由于它与HttpClient 3.x版本完全不兼容,以至于业内采用此库的公司较少,在互联网上也少有相关的文档资料分享。本文旨在写一个简要的Apache HttpClient 4.3开发指南,帮助开发者快速上手Apache HttpClient 4.3.x库。
要注意的是,本文档中的代码在低于HttpClient 4.3版本的地方可能不能运行。
二、开发手册
1、创建HTTP客户端[java] view
plain copy
print?
![](https://code.csdn.net/assets/CODE_ico.png)
CloseableHttpClient client = HttpClientBuilder.create().build();
2、发送基本的GET请求
[java] view
plain copy
print?
![](https://code.csdn.net/assets/CODE_ico.png)
instance.execute(new HttpGet(“http://www.baidu.com”));
3、获取HTTP响应的状态码
[java] view
plain copy
print?
![](https://code.csdn.net/assets/CODE_ico.png)
String url = “http://www.baidu.com”;
CloseableHttpResponse response = instance.execute(new HttpGet(url));
assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
4、获取响应的媒体类型
[java] view
plain copy
print?
![](https://code.csdn.net/assets/CODE_ico.png)
String url = “http://www.baidu.com”;
CloseableHttpResponse response = instance.execute(new HttpGet(url));
String contentMimeType = ContentType.getOrDefault(response.getEntity()).getMimeType();
assertThat(contentMimeType, equalTo(ContentType.TEXT_HTML.getMimeType()));
5、获取响应的BODY部分
[java] view
plain copy
print?
![](https://code.csdn.net/assets/CODE_ico.png)
String url = “http://www.baidu.com”;
CloseableHttpResponse response = instance.execute(new HttpGet(url));
String bodyAsString = EntityUtils.toString(response.getEntity());
assertThat(bodyAsString, notNullValue());
6、配置请求的超时设置
[java] view
plain copy
print?
![](https://code.csdn.net/assets/CODE_ico.png)
@Test(expected=SocketTimeoutException.class)
public void givenLowTimeout_whenExecutingRequestWithTimeout_thenException() throws ClientProtocolException, IOException{
RequestConfig requestConfig = RequestConfig.custom()
.setConnectionRequestTimeout(50).setConnectTimeout(50)
.setSocketTimeout(50).build();
HttpGet request = new HttpGet(SAMPLE_URL);
request.setConfig(requestConfig);
instance.execute(request);
}
7、发送POST请求
[java] view
plain copy
print?
![](https://code.csdn.net/assets/CODE_ico.png)
instance.execute(new HttpPost(SAMPLE_URL));
8、为HTTP请求配置重定向
[java] view
plain copy
print?
![](https://code.csdn.net/assets/CODE_ico.png)
CloseableHttpClient instance = HttpClientBuilder.create().disableRedirectHandling().build();
CloseableHttpResponse response = instance.execute(new HttpGet(SAMPLE_URL));
assertThat(reponse.getStatusLine().getStatusCode(), equalTo(301));
9、配置请求的HEADER部分
[java] view
plain copy
print?
![](https://code.csdn.net/assets/CODE_ico.png)
HttpGet request = new HttpGet(SAMPLE_URL);
request.addHeader(HttpHeaders.ACCEPT, “application/xml”);
response = instance.execute(request);
10、获取响应的HEADER部分
[java] view
plain copy
print?
![](https://code.csdn.net/assets/CODE_ico.png)
CloseableHttpResponse response = instance.execute(new HttpGet(SAMPLE_URL));
Header[] headers = response.getHeaders(HttpHeaders.CONTENT_TYPE);
assertThat(headers, not(emptyArray()));
11、关闭或释放资源
[java] view
plain copy
print?
![](https://code.csdn.net/assets/CODE_ico.png)
response = instance.execute(new HttpGet(SAMPLE_URL));
try{
HttpEntity entity = response.getEntity();
if(entity!=null){
InputStream instream = entity.getContent();
instream.close();
}
} finally{
response.close();
}
转:http://blog.csdn.net/chszs/article/details/16854747
![](http://static.blog.csdn.net/images/save_snippets.png)
以上内容涵盖了HttpClient 4.3所有常见的需求,供开发者参考。
相关文章推荐
- Apache HttpClient 4.3开发指南
- 《Apache HttpClient 4.3开发指南》
- Apache HttpClient 4.3开发指南
- Apache HttpClient 4.3开发指南 实现URL重定向
- Apache HttpClient 4.3开发指南(三个例子)
- 《Apache HttpClient 4.3开发指南》
- Apache HttpClient 4.3开发指南
- 《Apache HttpClient 4.3开发指南》
- Apache HttpClient 4.3开发指南
- 《Apache HttpClient 4.3开发指南》
- Apache HttpClient 4.3开发指南
- Apache HttpClient 4.3开发手册
- Android开发-基础网络组件(2)使用ApacheHttpClient登陆-AndroidStudio
- HttpClient 4.3 开发学习(3)
- android开发问题-关于Xutils框架出现无法访问HttpRequestBase 找不到org.apache.http.client.methods.HttpRequestBase解决方案
- JSP开发中Apache-HTTPClient 用户验证的实例详解
- HttpClient 4.3 开发学习(1)
- HttpClient 4.3 开发学习(2)
- 为 Android 开发访问 JAX-RS Web 服务的 Apache HttpClient 客户端 学习创建 Apache HttpClient Android 客户端
- HttpClient 4.3 开发学习(4)