《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. online training

    https://www.skillfeed.com/browse http://teamtreehouse.com/features http://www.pluralsight.com/ https ...

  2. InputStream和OutputStream与String之间的转换

    //1.字符串转inputstream String str="aaaaa"; InputStream in = new ByteArrayInputStream(str.getB ...

  3. cocos2dx中帧循环的伪代码实现

    1.在游戏开发中,帧率很大程度上体现了游戏的流畅度,帧循环是游戏中一个很重要的概念 2.下面用伪代码实现了cocos2dx中的帧循环 /*main函数调用*/ CCApplication::share ...

  4. C中的一些函数

    简述:printf.sprintf函数 转载自http://www.cnblogs.com/adslg/archive/2008/08/22/1274164.html 部分进行了修改,参考http:/ ...

  5. 嵌套回调异步与$.Deferred异步

    HTML: <input type="button" id="btn1" value="嵌套回调异步"> <input t ...

  6. dmucs与distcc

    之前配置distcc没有考虑负载均衡这一项,现在考虑使用dmucs实现distcc的负载均衡 官方手册 http://dmucs.sourceforge.net/ 使用官方手册编译会报错,等解决问题后 ...

  7. android开发类似coverflow效果的3d旋转

    源码下载地址:http://download.csdn.net/detail/feijian_/8888219

  8. 4、android xml中drawableTop(drawableBoottom、drawableLeft、drawableRight)在java代码中的动态配置

    做安卓开发的朋友都知道,我们在xml中可以通过这样来对button设置其上部或者(下.左.右)的图片资源: 那么如果需要动态配置图片呢?我们不得不使用java代码来进行操作: Drawable dra ...

  9. MyEclipse2015 编写js报 'Calculating completion proposals..' has encountered a problem.

    前言:编写js(按点后)弹出这个鬼东西,百度不到..估计是破解有问题.只有换版本了. 版本:MyEclipse 2015 stable 1.0 详细错误信息 解决:换成2.0版本

  10. ajax跨域请求,页面和java服务端的写法

    方法一(jsonp): 页面ajax请求的写法: $.ajax({ type : "get", async : false, cache : false, url : " ...