---
title: "HttpClient4.5教程(转)"
author: "sky_xin"
date: "2017-10-23T11:39:20Z"
tags: []
categories: ["互联网"]
slug: "httpclient4-5"
draft: false
lastmod: "2017-10-23T11:46:05Z"
id: 2397303
url: "https://sky-xin.iteye.com/blog/2397303"
---

# 转自：http://blog.csdn.net/gjb724332682/article/details/51645883

# 前言

旧版本的HttpClient已经停止维护了，它已经被Apache HttpComponents项目的HttpClient和HttpCore模块替代。Hyper-Text Transfer Protocol (HTTP)也许是当今互联网上使用的最为重要的协议。虽然java.net package提供了基本的从HTTP获取资源的功能，但是它不提供全面的灵活性或许多应用程序所需的功能。HttpClient试图填补这一空白，通过提供一个有效的、最新的、功能丰富的方案实现客户端最近的HTTP标准和建议。注意HttpClient不是浏览器，而是一个客户端HTTP传输库，目的是传送和接收HTTP信息，它缺少浏览器需要的UI, HTML渲染器和JavaScript引擎。  
可以直接到官网下载，或者使用maven配置。HttpClient 4.5 需要Java 1.5或者更新的版本。

**\[html]** [view plain](http://blog.csdn.net/gjb724332682/article/details/51645883 "view plain") [copy](http://blog.csdn.net/gjb724332682/article/details/51645883 "copy")

 

01. &lt;dependency&gt;
02. &lt;groupId&gt;org.apache.httpcomponents&lt;/groupId&gt;
03. &lt;artifactId&gt;httpcore&lt;/artifactId&gt;
04. &lt;version&gt;4.4.6&lt;/version&gt;
05. &lt;/dependency&gt;
06. &lt;dependency&gt;
07. &lt;groupId&gt;org.apache.httpcomponents&lt;/groupId&gt;
08. &lt;artifactId&gt;httpclient&lt;/artifactId&gt;
09. &lt;version&gt;4.5.3&lt;/version&gt;
10. &lt;/dependency&gt;

HttpClient最基本的功能是执行HTTP方法，执行一个HTTP方法包含了一个或者几个的HTTP REQUEST/HTTP RESPONSE交换，这通常在HttpClient内部处理。用户提供一个被执行的请求对象，HttpClient传送这个请求到目标服务器并且返回一个对应的响应对象，或者当请求不成功时抛出一个异常。所有的HTTP请求都有一行，它包含了请求请求方法名、请求URI和HTTP协议版本（1.0或者1.1）。

 

## []()请求

HttpClient支持开箱即用的，所有定义在HTTP/1.1规范中的方法：GET、HEAD、POST、PUT、DELETE、TRACE、OPTIONS。每个方法类型都有一个指定的类：HttpGet、HttpHead、HttpPost、HttpPut、HttpDelete、HttpTrace、HttpOptions。

 

**\[java]** [view plain](http://blog.csdn.net/gjb724332682/article/details/51645883 "view plain") [copy](http://blog.csdn.net/gjb724332682/article/details/51645883 "copy")

 

1. HttpGet httpget = new HttpGet(
2. "http://www.google.com/search?hl=en&q=httpclient&btnG=Google+Search&aq=f&oq=");

HttpClient也提供了URIBuilder用于构建URI。

 

**\[java]** [view plain](http://blog.csdn.net/gjb724332682/article/details/51645883 "view plain") [copy](http://blog.csdn.net/gjb724332682/article/details/51645883 "copy")

 

01. URI uri = new URIBuilder()
02. .setScheme("http")
03. .setHost("www.google.com")
04. .setPath("/search")
05. .setParameter("q", "httpclient")
06. .setParameter("btnG", "Google Search")
07. .setParameter("aq", "f")
08. .setParameter("oq", "")
09. .build();
10. HttpGet httpget = new HttpGet(uri);
11. System.out.println(httpget.getURI());//http://www.google.com/search?q=httpclient&btnG=Google+Search&aq=f&oq=

 

## []()响应

HTTP响应是服务端在接收和处理客户端消息后返回给客户端的消息。消息的第一行包含了协议版本，紧接着是状态码和状态码描述文字。

 

**\[java]** [view plain](http://blog.csdn.net/gjb724332682/article/details/51645883 "view plain") [copy](http://blog.csdn.net/gjb724332682/article/details/51645883 "copy")

 

1. HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP\_1\_1, HttpStatus.SC\_OK, "OK");
2. System.out.println(response.getProtocolVersion());//HTTP/1.1
3. System.out.println(response.getStatusLine().getStatusCode());//200
4. System.out.println(response.getStatusLine().getReasonPhrase());//OK
5. System.out.println(response.getStatusLine().toString());//HTTP/1.1 200 OK

 

## []()消息头

HTTP消息可以包含一些用来描述消息属性的头部消息，例如content length, content type等等。HttpClient提供了方法来检索、增加、删除、列举头部消息。

**\[java]** [view plain](http://blog.csdn.net/gjb724332682/article/details/51645883 "view plain") [copy](http://blog.csdn.net/gjb724332682/article/details/51645883 "copy")

 

01. HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP\_1\_1,
02. HttpStatus.SC\_OK, "OK");
03. response.addHeader("Set-Cookie",
04. "c1=a; path=/; domain=localhost");
05. response.addHeader("Set-Cookie",
06. "c2=b; path=\\"/\\", c3=c; domain=\\"localhost\\"");
07. Header h1 = response.getFirstHeader("Set-Cookie");
08. System.out.println(h1);//Set-Cookie: c1=a; path=/; domain=localhost
09. Header h2 = response.getLastHeader("Set-Cookie");
10. System.out.println(h2);//Set-Cookie: c2=b; path="/", c3=c; domain="localhost"
11. Header\[] hs = response.getHeaders("Set-Cookie");
12. System.out.println(hs.length);//2

最有效的方法还是利用HeaderIterator

 

**\[java]** [view plain](http://blog.csdn.net/gjb724332682/article/details/51645883 "view plain") [copy](http://blog.csdn.net/gjb724332682/article/details/51645883 "copy")

 

01. HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP\_1\_1,
02. HttpStatus.SC\_OK, "OK");
03. response.addHeader("Set-Cookie",
04. "c1=a; path=/; domain=localhost");
05. response.addHeader("Set-Cookie",
06. "c2=b; path=\\"/\\", c3=c; domain=\\"localhost\\"");
07. HeaderIterator it = response.headerIterator("Set-Cookie");
08. while (it.hasNext()) {
09. System.out.println(it.next());
10. }
11. st

同样，它也提供了将头部消息解析成单个元素的方法

 

**\[java]** [view plain](http://blog.csdn.net/gjb724332682/article/details/51645883 "view plain") [copy](http://blog.csdn.net/gjb724332682/article/details/51645883 "copy")

 

01. HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP\_1\_1,
02. HttpStatus.SC\_OK, "OK");
03. response.addHeader("Set-Cookie",
04. "c1=a; path=/; domain=localhost");
05. response.addHeader("Set-Cookie",
06. "c2=b; path=\\"/\\", c3=c; domain=\\"localhost\\"");
07. HeaderElementIterator it = new BasicHeaderElementIterator(
08. response.headerIterator("Set-Cookie"));
09. while (it.hasNext()) {
10. HeaderElement elem = it.nextElement();
11. System.out.println(elem.getName() + " = " + elem.getValue());
12. NameValuePair\[] params = elem.getParameters();
13. for (int i = 0; i &lt; params.length; i++) {
14. System.out.println(" " + params\[i]);
15. }
16. }

输出

 

**\[java]** [view plain](http://blog.csdn.net/gjb724332682/article/details/51645883 "view plain") [copy](http://blog.csdn.net/gjb724332682/article/details/51645883 "copy")

 

1. c1 = a
2. path=/
3. domain=localhost
4. c2 = b
5. path=/
6. c3 = c
7. domain=localhost

 

## []()HTTP实体

HTTP消息可以包含内容实体，HTTP定义了两个实体封装请求方法：PUT和POST。HttpClient依靠内容的来源来区分三种实体。  
streamed：内容来源于流或者动态生成，特别是，包含从HTTP响应接收的实体，streamed实体一般不可重复生成的。  
self-contained：内容位于内存中或者是可获得的，意味着它是独立于连接和其他实体的，Self-contained实体一般可重复，这种类型的实体大都用于HTTP请求的封装。  
wrapping：内容来源于其他实体。  
对于连接管理来说，当从HTTP响应中用流输出内容的时候这些区分的重要的。对于仅仅由应用程序创建并且用HttpClient发送的请求实体来说，streamed和self-contained的区别是不重要的。既然如此，那么就认为不可重复的实体是streamed，可重复的实体是self-contained。  
可重复的实体，表示它的内容可以不止一次被读取，例如ByteArrayEntity和StringEntity。为了读取内容，任何人都可以使用HttpEntity#getContent()返回java.io.InputStream，或者用HttpEntity#writeTo(OutputStream)提供给输出流。  
当实体通过一个收到的报文获取时，HttpEntity#getContentType()方法和HttpEntity#getContentLength()方法可以用来读取通用的元数据，如Content-Type和Content-Length头部信息（如果它们是可用的）。因为头部信息Content-Type可以包含对文本MIME类型的字符编码，比如text/plain或text/html，HttpEntity#getContentEncoding()方法用来读取这个信息。如果头部信息Content-Length不可用，那么就返回长度-1，而对于内容类型返回NULL。如果头部信息Content-Type是可用的，那么就会返回一个Header对象。

 

**\[java]** [view plain](http://blog.csdn.net/gjb724332682/article/details/51645883 "view plain") [copy](http://blog.csdn.net/gjb724332682/article/details/51645883 "copy")

 

1. StringEntity myEntity = new StringEntity("important message",
2. ContentType.create("text/plain", "UTF-8"));
3. System.out.println(myEntity.getContentType());
4. System.out.println(myEntity.getContentLength());
5. System.out.println(EntityUtils.toString(myEntity));
6. System.out.println(EntityUtils.toByteArray(myEntity).length);

输出

 

**\[java]** [view plain](http://blog.csdn.net/gjb724332682/article/details/51645883 "view plain") [copy](http://blog.csdn.net/gjb724332682/article/details/51645883 "copy")

 

1. Content-Type: text/plain; charset=utf-8
2. 17
3. important message
4. 17

 

### []()确保低级别资源释放

为了确保适当地释放系统资源，任何人必须关闭实体相关的流或者response本身。关闭实体相关的流和response的区别是：前者是通过消耗实体内容保持连接可用，后者直接关闭和抛弃连接。请记住，HttpEntity#writeTo(OutputStream)一旦实体被完全写出，也需要保证系统资源适当地释放。 EntityUtils#consume(HttpEntity)可以保证实体内容被完全消耗并且底层的流被关闭。

**\[java]** [view plain](http://blog.csdn.net/gjb724332682/article/details/51645883 "view plain") [copy](http://blog.csdn.net/gjb724332682/article/details/51645883 "copy")

 

01. CloseableHttpClient httpclient = HttpClients.createDefault();
02. HttpGet httpget = new HttpGet("http://localhost/");
03. CloseableHttpResponse response = httpclient.execute(httpget);
04. try {
05. HttpEntity entity = response.getEntity();
06. if (entity != null) {
07. InputStream instream = entity.getContent();
08. try {
09. // do something useful
10. } finally {
11. instream.close();
12. }
13. }
14. } finally {
15. response.close();
16. }

有些情况下，仅仅response的一小部分需要被取回并且消耗内容的剩余部分且保持连接可用的性能代价是很高的，这种情况下可以直接关闭response。

**\[java]** [view plain](http://blog.csdn.net/gjb724332682/article/details/51645883 "view plain") [copy](http://blog.csdn.net/gjb724332682/article/details/51645883 "copy")

 

01. CloseableHttpClient httpclient = HttpClients.createDefault();
02. HttpGet httpget = new HttpGet("http://localhost/");
03. CloseableHttpResponse response = httpclient.execute(httpget);
04. try {
05. HttpEntity entity = response.getEntity();
06. if (entity != null) {
07. InputStream instream = entity.getContent();
08. int byteOne = instream.read();
09. int byteTwo = instream.read();
10. // Do not need the rest
11. }
12. } finally {
13. response.close();
14. }

 

### []()消耗实体内容

推荐的消耗实体内容的方法是HttpEntity#getContent()和HttpEntity#writeTo(OutputStream)，但是EntityUtils类有一些静态方法，这些方法可以更加容易地从实体中读取内容或信息。代替直接读取java.io.InputStream，也可以使用这个类中的方法以字符串/字节数组的形式获取整个内容体。然而，EntityUtils的使用是强烈不鼓励的，除非响应实体源自可靠的HTTP服务器和已知的长度限制。

**\[java]** [view plain](http://blog.csdn.net/gjb724332682/article/details/51645883 "view plain") [copy](http://blog.csdn.net/gjb724332682/article/details/51645883 "copy")

 

01. CloseableHttpClient httpclient = HttpClients.createDefault();
02. HttpGet httpget = new HttpGet("http://localhost/");
03. CloseableHttpResponse response = httpclient.execute(httpget);
04. try {
05. HttpEntity entity = response.getEntity();
06. if (entity != null) {
07. long len = entity.getContentLength();
08. if (len != -1 && len &lt; 2048) {
09. System.out.println(EntityUtils.toString(entity));
10. } else {
11. // Stream content out
12. }
13. }
14. } finally {
15. response.close();
16. }

在一些情况下可能会不止一次的读取实体。此时实体内容必须以某种方式在内存或磁盘上被缓冲起来。最简单的方法是通过使用BufferedHttpEntity类来包装源实体完成。这会引起源实体内容被读取到内存的缓冲区中。在其它所有方式中，实体包装器将会得到源实体。

**\[java]** [view plain](http://blog.csdn.net/gjb724332682/article/details/51645883 "view plain") [copy](http://blog.csdn.net/gjb724332682/article/details/51645883 "copy")

 

1. CloseableHttpResponse response = &lt;...&gt;
2. HttpEntity entity = response.getEntity();
3. if (entity != null) {
4. entity = new BufferedHttpEntity(entity);
5. }

 

### []()生成实体内容

HttpClient提供一些类，它们可以用于通过HTTP连接有效地生成内容。这些类的实例可以和实体包装请求例如POST和PUT相关联以便包装实体内容。HttpClient提供了几个最为常见的数据容器，比如字符串，字节数组，输入流和文件提供了一些类：StringEntity，ByteArrayEntity，InputStreamEntity和FileEntity。请注意InputStreamEntity是不可重复的，因为它仅仅能从低层数据流中读取一次内容。通常来说，我们推荐实现一个定制的HttpEntity类，这是自我包含式的，用来代替使用通用的InputStreamEntity。FileEntity是一个很好的起点，FileEntity继承HttpEntity。

**\[java]** [view plain](http://blog.csdn.net/gjb724332682/article/details/51645883 "view plain") [copy](http://blog.csdn.net/gjb724332682/article/details/51645883 "copy")

 

1. File file = new File("somefile.txt");
2. FileEntity entity = new FileEntity(file,
3. ContentType.create("text/plain", "UTF-8"));
4. HttpPost httppost = new HttpPost("http://localhost/action.do");
5. httppost.setEntity(entity);

### []()HTML表单

许多应用需要模拟表单提交，比如，想要记录一个Web应用程序或提交输入数据。HttpClient提供了实体类UrlEncodedFormEntity使之更容易实现。

**\[java]** [view plain](http://blog.csdn.net/gjb724332682/article/details/51645883 "view plain") [copy](http://blog.csdn.net/gjb724332682/article/details/51645883 "copy")

 

1. List&lt;NameValuePair&gt; formparams = new ArrayList&lt;NameValuePair&gt;();
2. formparams.add(new BasicNameValuePair("param1", "value1"));
3. formparams.add(new BasicNameValuePair("param2", "value2"));
4. UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams, Consts.UTF\_8);
5. HttpPost httppost = new HttpPost("http://localhost/handler.do");
6. httppost.setEntity(entity);

### []()内容分块

一般来说，推荐让HttpClient自己根据Http消息传递的特征来选择最合适的传输编码。当然，如果非要手动控制也是可以的，设置HttpEntity#setChunked()为true，可以分块传输，但是如果是HTTP/1.0，那么这个设置就会被忽略，值有HTTP/1.1才支持。

**\[java]** [view plain](http://blog.csdn.net/gjb724332682/article/details/51645883 "view plain") [copy](http://blog.csdn.net/gjb724332682/article/details/51645883 "copy")

 

1. StringEntity entity = new StringEntity("important message",
2. ContentType.create("plain/text", Consts.UTF\_8));
3. entity.setChunked(true);
4. HttpPost httppost = new HttpPost("http://localhost/acrtion.do");
5. httppost.setEntity(entity);

## []()response处理

最简单也是最方便的处理http响应的方法就是使用ResponseHandler接口，这个接口中有handleResponse(HttpResponse response)方法。使用这个方法，用户完全不用关心http连接管理器。当使用ResponseHandler时，HttpClient会自动地将Http连接释放给Http管理器，即使http请求失败了或者抛出了异常。

**\[java]** [view plain](http://blog.csdn.net/gjb724332682/article/details/51645883 "view plain") [copy](http://blog.csdn.net/gjb724332682/article/details/51645883 "copy")

 

01. CloseableHttpClient httpclient = HttpClients.createDefault();
02. HttpGet httpget = new HttpGet("http://localhost/json");
03. ResponseHandler&lt;MyJsonObject&gt; rh = new ResponseHandler&lt;MyJsonObject&gt;() {
04. @Override
05. public JsonObject handleResponse(
06. final HttpResponse response) throws IOException {
07. StatusLine statusLine = response.getStatusLine();
08. HttpEntity entity = response.getEntity();
09. if (statusLine.getStatusCode() &gt;= 300) {
10. throw new HttpResponseException(
11. statusLine.getStatusCode(),
12. statusLine.getReasonPhrase());
13. }
14. if (entity == null) {
15. throw new ClientProtocolException("Response contains no content");
16. }
17. Gson gson = new GsonBuilder().create();
18. ContentType contentType = ContentType.getOrDefault(entity);
19. Charset charset = contentType.getCharset();
20. Reader reader = new InputStreamReader(entity.getContent(), charset);
21. return gson.fromJson(reader, MyJsonObject.class);
22. }
23. };
24. MyJsonObject myjson = client.execute(httpget, rh);

 

## []()HttpClient的接口

HttpClient的接口代表了请求执行过程中最基本的契约，HttpClient接口没有对Http请求的过程做特别的限制和详细的规定，连接管理、状态管理、认证处理和重定向处理这些功能都单独实现。这样就能更加容易地给接口添加额外的功能例如响应内容缓存。一般说来，HttpClient实际上就是一系列特殊的handler或者说策略接口的实现，这些handler（测试接口）负责着处理Http协议的某一方面，比如重定向、认证处理、有关连接持久性和keep alive持续时间的决策。这样就允许用户使用自定义的参数来代替默认配置，实现个性化的功能。

**\[java]** [view plain](http://blog.csdn.net/gjb724332682/article/details/51645883 "view plain") [copy](http://blog.csdn.net/gjb724332682/article/details/51645883 "copy")

 

01. ConnectionKeepAliveStrategy keepAliveStrat = new DefaultConnectionKeepAliveStrategy() {
02. @Override
03. public long getKeepAliveDuration(
04. HttpResponse response,
05. HttpContext context) {
06. long keepAlive = super.getKeepAliveDuration(response, context);
07. if (keepAlive == -1) {
08. // Keep connections alive 5 seconds if a keep-alive value
09. // has not be explicitly set by the server
10. keepAlive = 5000;
11. }
12. return keepAlive;
13. }
14. };
15. CloseableHttpClient httpclient = HttpClients.custom()
16. .setKeepAliveStrategy(keepAliveStrat)
17. .build();

## []()HTTPCLIENT的线程安全性

HttpClient已经实现了线程安全。因此建议一个实例被多个请求重用。

 

### []()HTTPCLIENT资源分配

当一个CloseableHttpClient的实例不再被使用，并且它的作用范围即将失效，和它相关的连接必须被关闭，关闭方法可以调用CloseableHttpClient的close()方法。

**\[java]** [view plain](http://blog.csdn.net/gjb724332682/article/details/51645883 "view plain") [copy](http://blog.csdn.net/gjb724332682/article/details/51645883 "copy")

 

1. CloseableHttpClient httpclient = HttpClients.createDefault();
2. try {
3. &lt;...&gt;
4. } finally {
5. httpclient.close();
6. }

## []()Http执行上下文

最初，Http被设计成一种无状态的、面向请求-响应的协议。然而，在实际使用中，我们希望能够在一些逻辑相关的请求-响应中，保持状态信息。为了使应用程序可以保持Http的持续状态，HttpClient允许http连接在特定的Http上下文中执行。如果在持续的http请求中使用了同样的上下文，那么这些请求就可以被分配到一个逻辑会话中。HTTP上下文就和一个java.util.Map&lt;String, Object&gt;功能类似。它实际上就是一个任意命名的值的集合。应用程序可以在Http请求执行前填充上下文的值，也可以在请求执行完毕后检查上下文。  
HttpContext可以包含任意类型的对象，因此如果在多线程中共享上下文会不安全。建议每个线程都只包含自己的http上下文。  
在Http请求执行的过程中，HttpClient会自动添加下面的属性到Http上下文中：  
HttpConnection的实例，表示客户端与服务器之间的连接  
HttpHost的实例，表示要连接的目标服务器  
HttpRoute的实例，表示全部的连接路由  
HttpRequest的实例，表示Http请求。在执行上下文中，最终的HttpRequest对象会代表http消息的状态。Http/1.0和Http/1.1都默认使用相对的uri。但是如果使用了非隧道模式的代理服务器，就会使用绝对路径的uri。  
HttpResponse的实例，表示Http响应  
java.lang.Boolean对象，表示是否请求被成功的发送给目标服务器  
RequestConfig对象，表示http request的配置信息  
java.util.List&lt;Uri&gt;对象，表示Http响应中的所有重定向地址  
可以使用HttpClientContext这个适配器来简化和上下文状态交互的过程。

**\[java]** [view plain](http://blog.csdn.net/gjb724332682/article/details/51645883 "view plain") [copy](http://blog.csdn.net/gjb724332682/article/details/51645883 "copy")

 

1. HttpContext context = &lt;...&gt;
2. HttpClientContext clientContext = HttpClientContext.adapt(context);
3. HttpHost target = clientContext.getTargetHost();
4. HttpRequest request = clientContext.getRequest();
5. HttpResponse response = clientContext.getResponse();
6. RequestConfig config = clientContext.getRequestConfig();

同一个逻辑会话中的多个Http请求，应该使用相同的Http上下文来执行，这样就可以自动地在http请求中传递会话上下文和状态信息。  
在下面的例子中，我们在开头设置的参数，会被保存在上下文中，并且会应用到后续的http请求中。

**\[java]** [view plain](http://blog.csdn.net/gjb724332682/article/details/51645883 "view plain") [copy](http://blog.csdn.net/gjb724332682/article/details/51645883 "copy")

 

01. CloseableHttpClient httpclient = HttpClients.createDefault();
02. RequestConfig requestConfig = RequestConfig.custom()
03. .setSocketTimeout(1000)
04. .setConnectTimeout(1000)
05. .build();
06. HttpGet httpget1 = new HttpGet("http://localhost/1");
07. httpget1.setConfig(requestConfig);
08. CloseableHttpResponse response1 = httpclient.execute(httpget1, context);
09. try {
10. HttpEntity entity1 = response1.getEntity();
11. } finally {
12. response1.close();
13. }
14. HttpGet httpget2 = new HttpGet("http://localhost/2");
15. CloseableHttpResponse response2 = httpclient.execute(httpget2, context);
16. try {
17. HttpEntity entity2 = response2.getEntity();
18. } finally {
19. response2.close();
20. }

 

## []()HTTP协议拦截器

HTTP协议拦截器是一种实现一个特定的方面的HTTP协议的代码程序。通常情况下，协议拦截器会将一个或多个头消息加入到接收或者发送的消息中。协议拦截器也可以操作消息的内容实体—消息内容的压缩/解压缩就是个很好的例子。通常，这是通过使用“装饰”开发模式，一个包装实体类用于装饰原来的实体来实现。一个拦截器可以合并，形成一个逻辑单元。协议拦截器可以通过共享信息协作——比如处理状态——通过HTTP执行上下文。协议拦截器可以使用Http上下文存储一个或者多个连续请求的处理状态。通常，只要拦截器不依赖于一个特定状态的http上下文，那么拦截执行的顺序就无所谓。如果协议拦截器有相互依赖关系，必须以特定的顺序执行，那么它们应该按照特定的顺序加入到协议处理器中。协议处理器必须是线程安全的。类似于servlets，协议拦截器不应该使用实例变量，除非访问这些实例变量是同步的（线程安全的）。  
下面是个例子，讲述了本地的上下文时如何在连续请求中记录处理状态的：

**\[java]** [view plain](http://blog.csdn.net/gjb724332682/article/details/51645883 "view plain") [copy](http://blog.csdn.net/gjb724332682/article/details/51645883 "copy")

 

01. CloseableHttpClient httpclient = HttpClients.custom()
02. .addInterceptorLast(new HttpRequestInterceptor() {
03. public void process(
04. final HttpRequest request,
05. final HttpContext context) throws HttpException, IOException {
06. AtomicInteger count = (AtomicInteger) context.getAttribute("count");
07. request.addHeader("Count", Integer.toString(count.getAndIncrement()));
08. }
09. })
10. .build();
11. AtomicInteger count = new AtomicInteger(1);
12. HttpClientContext localContext = HttpClientContext.create();
13. localContext.setAttribute("count", count);
14. HttpGet httpget = new HttpGet("http://localhost/");
15. for (int i = 0; i &lt; 10; i++) {
16. CloseableHttpResponse response = httpclient.execute(httpget, localContext);
17. try {
18. HttpEntity entity = response.getEntity();
19. } finally {
20. response.close();
21. }
22. }

 

## []()异常处理

HttpClient会被抛出两种类型的异常，一种是java.io.IOException，当遇到I/O异常时抛出（socket超时，或者socket被重置）;另一种是HttpException,表示Http失败，如Http协议使用不正确。通常认为，I/O错误时不致命、可修复的，而Http协议错误是致命了，不能自动修复的错误。请注意，HttpException如果被重新抛出成IOException的子类ClientProtocolException，就可以让用户在一个catch语句里同时处理I/O错误和协议错误。

 

### []()HTTP传输安全

Http协议不能满足所有类型的应用场景，我们需要知道这点。Http是个简单的面向协议的请求/响应的协议，当初它被设计用来支持静态或者动态生成的内容检索，之前从来没有人想过让它支持事务性操作。例如，Http服务器成功接收、处理请求后，生成响应消息，并且把状态码发送给客户端，这个过程是Http协议应该保证的。但是，如果客户端由于读取超时、取消请求或者系统崩溃导致接收响应失败，服务器不会回滚这一事务。如果客户端重新发送这个请求，服务器就会重复的解析、执行这个事务。在一些情况下，这会导致应用程序的数据损坏和应用程序的状态不一致。即使Http当初设计是不支持事务操作，但是它仍旧可以作为传输协议为某些关键程序提供服务。为了保证Http传输层的安全性，系统必须保证应用层上的http方法的幂等性。

 

### []()方法的幂等性

HTTP/1.1规范中是这样定义幂等方法的，\[Methods can also have the property of "idempotence" in that (aside from error or expiration issues) the side-effects of N &gt; 0 identical requests is the same as for a single request]。用其他话来说，应用程序需要做好准备，处理同一方法多次执行造成的影响。添加一个具有唯一性的id就能避免重复执行同一个逻辑请求，问题解决。请知晓，这个问题不只是HttpClient才会有，基于浏览器的应用程序也会遇到Http方法不幂等的问题。HttpClient默认把非实体方法get、head方法看做幂等方法，把实体方法post、put方法看做非幂等方法。

 

### []()异常自动修复

默认情况下，HttpClient会尝试自动修复I/O异常。这种自动修复仅限于修复几个公认安全的异常。  
HttpClient不会尝试修复任何逻辑或者http协议错误（即从HttpException衍生出来的异常）。  
HttpClient会自动再次发送幂等的方法（如果首次执行失败)。  
HttpClient会自动再次发送遇到transport异常的方法，前提是Http请求仍旧保持着连接（例如http请求没有全部发送给目标服务器，HttpClient会再次尝试发送）。

 

## []()请求重试HANDLER

如果要自定义异常处理机制，我们需要实现HttpRequestRetryHandler接口。请注意，可以使用StandardHttpRequestRetryHandler替代默认的接口以便使那些在 RFC-2616定义的幂等性的请求方法安全的自动重试：GET, HEAD, PUT, DELETE, OPTIONS, TRACE。

**\[java]** [view plain](http://blog.csdn.net/gjb724332682/article/details/51645883 "view plain") [copy](http://blog.csdn.net/gjb724332682/article/details/51645883 "copy")

 

01. HttpRequestRetryHandler myRetryHandler = new HttpRequestRetryHandler() {
02. public boolean retryRequest(
03. IOException exception,
04. int executionCount,
05. HttpContext context) {
06. if (executionCount &gt;= 5) {
07. // Do not retry if over max retry count
08. return false;
09. }
10. if (exception instanceof InterruptedIOException) {
11. // Timeout
12. return false;
13. }
14. if (exception instanceof UnknownHostException) {
15. // Unknown host
16. return false;
17. }
18. if (exception instanceof ConnectTimeoutException) {
19. // Connection refused
20. return false;
21. }
22. if (exception instanceof SSLException) {
23. // SSL handshake exception
24. return false;
25. }
26. HttpClientContext clientContext = HttpClientContext.adapt(context);
27. HttpRequest request = clientContext.getRequest();
28. boolean idempotent = !(request instanceof HttpEntityEnclosingRequest);
29. if (idempotent) {
30. // Retry if the request is considered idempotent
31. return true;
32. }
33. return false;
34. }
35. };
36. CloseableHttpClient httpclient = HttpClients.custom()
37. .setRetryHandler(myRetryHandler)
38. .build();

 

## []()中断请求

有时候由于目标服务器负载过高或者客户端目前有太多请求积压，http请求不能在指定时间内执行完毕。这时候终止这个请求，释放阻塞I/O的进程，就显得很必要。通过HttpClient执行的Http请求，在任何状态下都能通过调用HttpUriRequest的abort()方法来终止。这个方法是线程安全的，并且能在任何线程中调用。当Http请求被终止了，本线程（即使现在正在阻塞I/O）也会通过抛出一个InterruptedIOException异常，来释放资源。

 

## []()重定向处理

HttpClient会自动处理所有类型的重定向，除了那些Http规范明确禁止的重定向。See Other (status code 303) redirects on POST and PUT requests are converted to GET requests as required by the HTTP specification. 可以使用自定义的重定向策略来放松Http规范对Post方法重定向的限制。

**\[java]** [view plain](http://blog.csdn.net/gjb724332682/article/details/51645883 "view plain") [copy](http://blog.csdn.net/gjb724332682/article/details/51645883 "copy")

 

1. LaxRedirectStrategy redirectStrategy = new LaxRedirectStrategy();
2. CloseableHttpClient httpclient = HttpClients.custom()
3. .setRedirectStrategy(redirectStrategy)
4. .build();

HttpClient在请求执行过程中，经常需要重写请求的消息。 HTTP/1.0和HTTP/1.1都默认使用相对的uri路径。同样，原始的请求可能会被者多次的重定向。最终绝对路径可以使用原始的请求和上下文来构建。URIUtils#resolve可以用于构建绝对路径，产生最终的请求。这个方法包含了最后一个分片标识符或者原始请求。

**\[java]** [view plain](http://blog.csdn.net/gjb724332682/article/details/51645883 "view plain") [copy](http://blog.csdn.net/gjb724332682/article/details/51645883 "copy")

 

01. CloseableHttpClient httpclient = HttpClients.createDefault();
02. HttpClientContext context = HttpClientContext.create();
03. HttpGet httpget = new HttpGet("http://localhost:8080/");
04. CloseableHttpResponse response = httpclient.execute(httpget, context);
05. try {
06. HttpHost target = context.getTargetHost();
07. List&lt;URI&gt; redirectLocations = context.getRedirectLocations();
08. URI location = URIUtils.resolve(httpget.getURI(), target, redirectLocations);
09. System.out.println("Final HTTP location: " + location.toASCIIString());
10. // Expected to be an absolute URI
11. } finally {
12. response.close();
13. }