《Apache HttpClient 4.3开发指南》

Apache HttpClient 4系列已经发布很久了,但由于它与HttpClient 3.x版本完全不兼容,以至于业内采用此库的公司较少,在互联网上也少有相关的文档资料分享。

本文旨在写一个简要的Apache HttpClient 4.3开发指南,帮助开发者快速上手Apache HttpClient 4.3.x库。

要注意的是,本文档中的代码在低于HttpClient 4.3版本的地方可能不能运行。

二、开发手册

1、创建HTTP客户端

  1. CloseableHttpClient client = HttpClientBuilder.create().build();

2、发送基本的GET请求

  1. instance.execute(new HttpGet(“http://www.baidu.com”));

3、获取HTTP响应的状态码

  1. String url = “http://www.baidu.com”;
  2. CloseableHttpResponse response = instance.execute(new HttpGet(url));
  3. assertThat(response.getStatusLine().getStatusCode(), equalTo(200));

4、获取响应的媒体类型

  1. String url = “http://www.baidu.com”;
  2. CloseableHttpResponse response = instance.execute(new HttpGet(url));
  3. String contentMimeType = ContentType.getOrDefault(response.getEntity()).getMimeType();
  4. assertThat(contentMimeType, equalTo(ContentType.TEXT_HTML.getMimeType()));

5、获取响应的BODY部分

  1. String url = “http://www.baidu.com”;
  2. CloseableHttpResponse response = instance.execute(new HttpGet(url));
  3. String bodyAsString = EntityUtils.toString(response.getEntity());
  4. assertThat(bodyAsString, notNullValue());

6、配置请求的超时设置

  1. @Test(expected=SocketTimeoutException.class)
  2. public void givenLowTimeout_whenExecutingRequestWithTimeout_thenException() throws ClientProtocolException, IOException{
  3. RequestConfig requestConfig = RequestConfig.custom()
  4. .setConnectionRequestTimeout(50).setConnectTimeout(50)
  5. .setSocketTimeout(50).build();
  6. HttpGet request = new HttpGet(SAMPLE_URL);
  7. request.setConfig(requestConfig);
  8. instance.execute(request);
  9. }

7、发送POST请求

  1. instance.execute(new HttpPost(SAMPLE_URL));

8、为HTTP请求配置重定向

  1. CloseableHttpClient instance = HttpClientBuilder.create().disableRedirectHandling().build();
  2. CloseableHttpResponse response = instance.execute(new HttpGet(SAMPLE_URL));
  3. assertThat(reponse.getStatusLine().getStatusCode(), equalTo(301));

9、配置请求的HEADER部分

  1. HttpGet request = new HttpGet(SAMPLE_URL);
  2. request.addHeader(HttpHeaders.ACCEPT, “application/xml”);
  3. response = instance.execute(request);

10、获取响应的HEADER部分

  1. CloseableHttpResponse response = instance.execute(new HttpGet(SAMPLE_URL));
  2. Header[] headers = response.getHeaders(HttpHeaders.CONTENT_TYPE);
  3. assertThat(headers, not(emptyArray()));

11、关闭或释放资源

  1. response = instance.execute(new HttpGet(SAMPLE_URL));
  2. try{
  3. HttpEntity entity = response.getEntity();
  4. if(entity!=null){
  5. InputStream instream = entity.getContent();
  6. instream.close();
  7. }
  8. } finally{
  9. response.close();
  10. }

以上内容涵盖了HttpClient 4.3所有常见的需求,供开发者参考。

HttpClient基本用法的更多相关文章

  1. yii2 httpClient的用法

    yii2 httpClient的用法示例: <?php /* * @Purpose : yii2 httpClient 请求示例 * @Author : Chrdai * @Time : 201 ...

  2. HttpClient的用法

    客户端模拟http请求工具 Postmen(谷歌插件).RestClient 服务器模拟http请求工具 httpclient.HttpURLConnection httpCient请求代码 /** ...

  3. HttpClient的用法总结

    使用HttpClient连接服务端的步骤: 1.创建HttpClient客户端对象 HttpClient client = new DefaultHttpClient(); 2.创建请求对象      ...

  4. HttpClient基础用法

    一.HttpClient HttpClient是Apache HttpComponents 下的子项目,用来提供高效的.最新的.功能丰富的支持HTTP协议的客户端编程工具包(httpclient-4. ...

  5. Java测试开发--HttpClient常规用法(九)

    1.HttpClient可以读取网页(HTTP/HTTPS)内容 2.对url发送get/post请求(带不带参数都可以),进行测试 一.maven项目pom.xml需要引入包 <depende ...

  6. Android Volley完全解析(一),初识Volley的基本用法

    1. Volley简介 我们平时在开发Android应用的时候不可避免地都需要用到网络技术,而多数情况下应用程序都会使用HTTP协议来发送和接收网络数据.Android 系统中主要提供了两种方式来进行 ...

  7. HttpClient session

    session概述 session机制 session机制是一种服务器端的机制,服务器使用一种类似于散列表的结构(也可能就是使用散列表)来保存信息. 当程序需要为某个客户端的请求创建一个session ...

  8. [转] Android Volley完全解析(一),初识Volley的基本用法

    版权声明:本文出自郭霖的博客,转载必须注明出处.   目录(?)[-] Volley简介 下载Volley StringRequest的用法 JsonRequest的用法   转载请注明出处:http ...

  9. Android Volley入门到精通:初识Volley的基本用法

    1. Volley简介 我们平时在开发Android应用的时候不可避免地都需要用到网络技术,而多数情况下应用程序都会使用HTTP协议来发送和接收网络数据.Android系统中主要提供了两种方式来进行H ...

随机推荐

  1. Abstract Class与 Interface 的区别

    表格                                                                                               Abs ...

  2. IT服务系统组成

    软件+硬件+数据 + 运维人员 = IT服务系统 车 司机 乘客 修车 = 车模式 效率 系统 用户 业务 运维 = 信息化 效率 如果司机不会开车,没有人会修车就不会有车轮上的世界 同样没有人会运维 ...

  3. 不会JS中的OOP,你也太菜了吧!(第一篇)

    一.你必须知道的 1) 字面量 2) 原型 3) 原型链 4) 构造函数 5) 稳妥对象(没有公共属性,而且其方法也不引用this的对象.稳妥对象适合用在安全的环境中和防止数据被其它程序改变的时候) ...

  4. htaccess 探秘

    .htaccess访问控制(Allow/Deny) 1. 验证是否支持.htaccess 在目录下新建一个.htaccess 文件,随笔输入一串字符(毫无意义),看看什么反应,如果是500错误,说明目 ...

  5. 剑指offer--面试题23

    //该题原本想用递归实现,但却用循环实现了... //关键用了个队列 #include <queue> void Print(BinaryTreeNode* pRoot, std::que ...

  6. GS线程

    void GameServer::ProcessThread() { try {//在ui线程里面搞个大try不是说try效率不好吗,难道只是为了出现错误发现在GS线程里面出现的吗 ProcessTh ...

  7. unity3d 自动保存

    using UnityEngine; using UnityEditor; using System; public class AutoSave : EditorWindow { private b ...

  8. python模拟shell

    import fileinput import readline raw_input(xxx) exec filepinput.input

  9. JSP-declareAndOutput

    <%@ page language="java" contentType="text/html; charset=ISO-8859-1" errorPag ...

  10. 理解Flight框架核心

    http://blog.csdn.net/sky_zhe/article/details/38906689 Flight 框架 Flight类 1.加载 autoload.php ,启动框架的自动加载 ...