HttpClient基本用法
《Apache HttpClient 4.3开发指南》
Apache HttpClient 4系列已经发布很久了,但由于它与HttpClient 3.x版本完全不兼容,以至于业内采用此库的公司较少,在互联网上也少有相关的文档资料分享。
本文旨在写一个简要的Apache HttpClient 4.3开发指南,帮助开发者快速上手Apache HttpClient 4.3.x库。
要注意的是,本文档中的代码在低于HttpClient 4.3版本的地方可能不能运行。
二、开发手册
1、创建HTTP客户端
- CloseableHttpClient client = HttpClientBuilder.create().build();
2、发送基本的GET请求
- instance.execute(new HttpGet(“http://www.baidu.com”));
3、获取HTTP响应的状态码
- String url = “http://www.baidu.com”;
- CloseableHttpResponse response = instance.execute(new HttpGet(url));
- assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
4、获取响应的媒体类型
- 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部分
- String url = “http://www.baidu.com”;
- CloseableHttpResponse response = instance.execute(new HttpGet(url));
- String bodyAsString = EntityUtils.toString(response.getEntity());
- assertThat(bodyAsString, notNullValue());
6、配置请求的超时设置
- @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请求
- instance.execute(new HttpPost(SAMPLE_URL));
8、为HTTP请求配置重定向
- CloseableHttpClient instance = HttpClientBuilder.create().disableRedirectHandling().build();
- CloseableHttpResponse response = instance.execute(new HttpGet(SAMPLE_URL));
- assertThat(reponse.getStatusLine().getStatusCode(), equalTo(301));
9、配置请求的HEADER部分
- HttpGet request = new HttpGet(SAMPLE_URL);
- request.addHeader(HttpHeaders.ACCEPT, “application/xml”);
- response = instance.execute(request);
10、获取响应的HEADER部分
- CloseableHttpResponse response = instance.execute(new HttpGet(SAMPLE_URL));
- Header[] headers = response.getHeaders(HttpHeaders.CONTENT_TYPE);
- assertThat(headers, not(emptyArray()));
11、关闭或释放资源
- response = instance.execute(new HttpGet(SAMPLE_URL));
- try{
- HttpEntity entity = response.getEntity();
- if(entity!=null){
- InputStream instream = entity.getContent();
- instream.close();
- }
- } finally{
- response.close();
- }
以上内容涵盖了HttpClient 4.3所有常见的需求,供开发者参考。
HttpClient基本用法的更多相关文章
- yii2 httpClient的用法
yii2 httpClient的用法示例: <?php /* * @Purpose : yii2 httpClient 请求示例 * @Author : Chrdai * @Time : 201 ...
- HttpClient的用法
客户端模拟http请求工具 Postmen(谷歌插件).RestClient 服务器模拟http请求工具 httpclient.HttpURLConnection httpCient请求代码 /** ...
- HttpClient的用法总结
使用HttpClient连接服务端的步骤: 1.创建HttpClient客户端对象 HttpClient client = new DefaultHttpClient(); 2.创建请求对象 ...
- HttpClient基础用法
一.HttpClient HttpClient是Apache HttpComponents 下的子项目,用来提供高效的.最新的.功能丰富的支持HTTP协议的客户端编程工具包(httpclient-4. ...
- Java测试开发--HttpClient常规用法(九)
1.HttpClient可以读取网页(HTTP/HTTPS)内容 2.对url发送get/post请求(带不带参数都可以),进行测试 一.maven项目pom.xml需要引入包 <depende ...
- Android Volley完全解析(一),初识Volley的基本用法
1. Volley简介 我们平时在开发Android应用的时候不可避免地都需要用到网络技术,而多数情况下应用程序都会使用HTTP协议来发送和接收网络数据.Android 系统中主要提供了两种方式来进行 ...
- HttpClient session
session概述 session机制 session机制是一种服务器端的机制,服务器使用一种类似于散列表的结构(也可能就是使用散列表)来保存信息. 当程序需要为某个客户端的请求创建一个session ...
- [转] Android Volley完全解析(一),初识Volley的基本用法
版权声明:本文出自郭霖的博客,转载必须注明出处. 目录(?)[-] Volley简介 下载Volley StringRequest的用法 JsonRequest的用法 转载请注明出处:http ...
- Android Volley入门到精通:初识Volley的基本用法
1. Volley简介 我们平时在开发Android应用的时候不可避免地都需要用到网络技术,而多数情况下应用程序都会使用HTTP协议来发送和接收网络数据.Android系统中主要提供了两种方式来进行H ...
随机推荐
- online training
https://www.skillfeed.com/browse http://teamtreehouse.com/features http://www.pluralsight.com/ https ...
- InputStream和OutputStream与String之间的转换
//1.字符串转inputstream String str="aaaaa"; InputStream in = new ByteArrayInputStream(str.getB ...
- cocos2dx中帧循环的伪代码实现
1.在游戏开发中,帧率很大程度上体现了游戏的流畅度,帧循环是游戏中一个很重要的概念 2.下面用伪代码实现了cocos2dx中的帧循环 /*main函数调用*/ CCApplication::share ...
- C中的一些函数
简述:printf.sprintf函数 转载自http://www.cnblogs.com/adslg/archive/2008/08/22/1274164.html 部分进行了修改,参考http:/ ...
- 嵌套回调异步与$.Deferred异步
HTML: <input type="button" id="btn1" value="嵌套回调异步"> <input t ...
- dmucs与distcc
之前配置distcc没有考虑负载均衡这一项,现在考虑使用dmucs实现distcc的负载均衡 官方手册 http://dmucs.sourceforge.net/ 使用官方手册编译会报错,等解决问题后 ...
- android开发类似coverflow效果的3d旋转
源码下载地址:http://download.csdn.net/detail/feijian_/8888219
- 4、android xml中drawableTop(drawableBoottom、drawableLeft、drawableRight)在java代码中的动态配置
做安卓开发的朋友都知道,我们在xml中可以通过这样来对button设置其上部或者(下.左.右)的图片资源: 那么如果需要动态配置图片呢?我们不得不使用java代码来进行操作: Drawable dra ...
- MyEclipse2015 编写js报 'Calculating completion proposals..' has encountered a problem.
前言:编写js(按点后)弹出这个鬼东西,百度不到..估计是破解有问题.只有换版本了. 版本:MyEclipse 2015 stable 1.0 详细错误信息 解决:换成2.0版本
- ajax跨域请求,页面和java服务端的写法
方法一(jsonp): 页面ajax请求的写法: $.ajax({ type : "get", async : false, cache : false, url : " ...