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 ...
随机推荐
- OpenGL Shader源码分享
Opengl shader程序,旗帜混合纹理加载,通过N张图片,能够组合出数百个:http://www.eyesourcecode.com/thread-39015-1-1.html 用GLSL做了一 ...
- android 开发 讯飞语音唤醒功能
场景:进入程序后处于语音唤醒状态,当说到某个关键词的时候打开某个子界面(如:语音识别界面) 技术要点: 1. // 设置唤醒一直保持,直到调用stopListening,传入0则完成一次唤醒后,会话立 ...
- SQL Server性能优化(3)使用SQL Server Profiler查询性能瓶颈
关于SQL Server Profiler的使用,网上已经有很多教程,比如这一篇文章:SQL Server Profiler:使用方法和指标说明.微软官方文档:https://msdn.microso ...
- 使用try-with-resources注意的问题
package coin; import java.io.FileInputStream; import java.io.ObjectInputStream; /** * 使用 try-with-re ...
- python 数据结构-列表
列表常用方法汇总: #定义列表li li=[12.23,456,88,9] a=[1,2,3] #添加元素到列表结尾 li.append(360) #追加列表元素extend(L) li.extend ...
- eclipse 中卸载插件的方法
卸载步骤: Help -> About Eclipse -> Installation Details -> "点到你要删除的插件,如EclipseME" –&g ...
- 设计模式之组合模式(Composite)
组合模式原理:组合模式的作用是讲继承同一父类的不同子类对象组合起来,形成一个树形的结构,例如公司的部门组织 代码如下 #include <iostream> #include <st ...
- 【BZOJ】【1855】【SCOI2010】/【HDOJ】【3401】股票交易
DP/单调队列优化 题解:http://www.cnblogs.com/jianglangcaijin/p/3799736.html 令f[i][j]表示第 i 天结束后,手里剩下 j 股的最大利润, ...
- 2014年全球SEO行业调查报告
前言: 1.该调查报告是MOZ每两年一度针对SEO行业的数据分析报告. 2.随着SEO的进化,该报告已不仅仅是SEO行业,今年的调查数据更多分析网络营销行业,可以称作"网络营销行业调查报告& ...
- 引擎设计跟踪(九.14.2a) 导出插件问题修复和 Tangent Space 裂缝修复
由于工作很忙, 近半年的业余时间没空搞了, 不过工作马上忙完了, 趁十一有时间修了一些小问题. 这次更新跟骨骼动画无关, 修复了一个之前的, 关于tangent space裂缝的问题: 引擎设计跟踪( ...