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 ...
随机推荐
- Abstract Class与 Interface 的区别
表格 Abs ...
- IT服务系统组成
软件+硬件+数据 + 运维人员 = IT服务系统 车 司机 乘客 修车 = 车模式 效率 系统 用户 业务 运维 = 信息化 效率 如果司机不会开车,没有人会修车就不会有车轮上的世界 同样没有人会运维 ...
- 不会JS中的OOP,你也太菜了吧!(第一篇)
一.你必须知道的 1) 字面量 2) 原型 3) 原型链 4) 构造函数 5) 稳妥对象(没有公共属性,而且其方法也不引用this的对象.稳妥对象适合用在安全的环境中和防止数据被其它程序改变的时候) ...
- htaccess 探秘
.htaccess访问控制(Allow/Deny) 1. 验证是否支持.htaccess 在目录下新建一个.htaccess 文件,随笔输入一串字符(毫无意义),看看什么反应,如果是500错误,说明目 ...
- 剑指offer--面试题23
//该题原本想用递归实现,但却用循环实现了... //关键用了个队列 #include <queue> void Print(BinaryTreeNode* pRoot, std::que ...
- GS线程
void GameServer::ProcessThread() { try {//在ui线程里面搞个大try不是说try效率不好吗,难道只是为了出现错误发现在GS线程里面出现的吗 ProcessTh ...
- unity3d 自动保存
using UnityEngine; using UnityEditor; using System; public class AutoSave : EditorWindow { private b ...
- python模拟shell
import fileinput import readline raw_input(xxx) exec filepinput.input
- JSP-declareAndOutput
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" errorPag ...
- 理解Flight框架核心
http://blog.csdn.net/sky_zhe/article/details/38906689 Flight 框架 Flight类 1.加载 autoload.php ,启动框架的自动加载 ...