HttpClient学习研究---第一章:基础知识

时间:2021-12-31 05:56:20

Chapter 1. 第1章。Fundamentals基本面

1.1. 1.1。Request execution请求执行

The most essential function of HttpClient is to execute HTTP methods. 最基本的功能是执行HttpClient HTTP方法。Execution of an HTTP method involves one or several HTTP request / HTTP response exchanges, usually handled internally by HttpClient. 执行一个HTTP方法涉及一个或多个HTTP请求/ HTTP响应交流,通常在内部处理由HttpClient。The user is expected to provide a request object to execute and HttpClient is expected to transmit the request to the target server return a corresponding response object, or throw an exception if execution was unsuccessful.用户预计将提供一个请求对象来执行和HttpClient预计将传送到目标服务器的请求返回相应的响应对象,或者抛出一个异常如果执行不成功。

Quite naturally, the main entry point of the HttpClient API is the HttpClient interface that defines the contract described above.很自然地,主入口点的HttpClient API是HttpClient接口,定义了上面描述的合同。

Here is an example of request execution process in its simplest form:下面是一��示例请求执行过程在其最简单的形式:

CloseableHttpClient httpclient = HttpClients.createDefault();
HttpGet httpget = new HttpGet("http://localhost/");
CloseableHttpResponse response = httpclient.execute(httpget);
try {
    <...>
} finally {
    response.close();
}

1.1.1. 1.1.1。HTTP requestHTTP请求

All HTTP requests have a request line consisting a method name, a request URI and an HTTP protocol version.所有HTTP请求有一个请求线组成一个方法名,请求URI和HTTP协议版本。

HttpClient supports out of the box all HTTP methods defined in the HTTP/1.1 specification:HttpClient支持开箱即用的所有HTTP方法定义在HTTP / 1.1规范: GET, HEAD, POST, PUT, DELETE, TRACEand OPTIONS. There is a specific class for each method type.:有一个特定的类为每个方法类型。 HttpGet, HttpHead, HttpPost, HttpPut, HttpDelete, HttpTrace, and,和 HttpOptions.

The Request-URI is a Uniform Resource Identifier that identifies the resource upon which to apply the request. 请求uri所指定资源是一个统一资源标识符标识资源在应用请求。HTTP request URIs consist of a protocol scheme, host name, optional port, resource path, optional query, and optional fragment.HTTP请求uri包含协议方案,主机名称,可选端口、资源路径,可选的查询,和可选的片段。

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

HttpClient providesHttpClient提供 URIBuilderutility class to simplify creation and modification of request URIs.工具类来简化创建和修改请求uri。

URI uri = new URIBuilder()
        .setScheme("http")
        .setHost("www.google.com")
        .setPath("/search")
        .setParameter("q", "httpclient")
        .setParameter("btnG", "Google Search")
        .setParameter("aq", "f")
        .setParameter("oq", "")
        .build();
HttpGet httpget = new HttpGet(uri);
System.out.println(httpget.getURI());

stdout >stdout >

http://www.google.com/search?q=httpclient&btnG=Google+Search&aq=f&oq=

1.1.2. 1.1.2。HTTP responseHTTP响应

HTTP response is a message sent by the server back to the client after having received and interpreted a request message. HTTP响应消息发送回客户机的服务器在接受了一个请求消息和解释。The first line of that message consists of the protocol version followed by a numeric status code and its associated textual phrase.第一行的消息包括协议版本后跟一个数字状态码和它相关的文本短语。

HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 
HttpStatus.SC_OK, "OK");

System.out.println(response.getProtocolVersion());
System.out.println(response.getStatusLine().getStatusCode());
System.out.println(response.getStatusLine().getReasonPhrase());
System.out.println(response.getStatusLine().toString());

stdout >stdout >

HTTP/1.1
200
OK
HTTP/1.1 200 OK

1.1.3. 1.1.3。Working with message headers处理消息标头

An HTTP message can contain a number of headers describing properties of the message such as the content length, content type and so on. 一个HTTP消息可以包含许多标题描述的属性信息,如内容长度,内容类型等等。HttpClient provides methods to retrieve, add, remove and enumerate headers.HttpClient提供方法来检索、添加、移除和列举头。

HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 
    HttpStatus.SC_OK, "OK");
response.addHeader("Set-Cookie", 
    "c1=a; path=/; domain=localhost");
response.addHeader("Set-Cookie", 
    "c2=b; path=\"/\", c3=c; domain=\"localhost\"");
Header h1 = response.getFirstHeader("Set-Cookie");
System.out.println(h1);
Header h2 = response.getLastHeader("Set-Cookie");
System.out.println(h2);
Header[] hs = response.getHeaders("Set-Cookie");
System.out.println(hs.length);

stdout >stdout >

Set-Cookie: c1=a; path=/; domain=localhost
Set-Cookie: c2=b; path="/", c3=c; domain="localhost"
2

The most efficient way to obtain all headers of a given type is by using the最有效的方式来获得给定类型的所有标题是通过使用 HeaderIteratorinterface.接口。

HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 
    HttpStatus.SC_OK, "OK");
response.addHeader("Set-Cookie", 
    "c1=a; path=/; domain=localhost");
response.addHeader("Set-Cookie", 
    "c2=b; path=\"/\", c3=c; domain=\"localhost\"");

HeaderIterator it = response.headerIterator("Set-Cookie");

while (it.hasNext()) {
    System.out.println(it.next());
}

stdout >stdout >

Set-Cookie: c1=a; path=/; domain=localhost
Set-Cookie: c2=b; path="/", c3=c; domain="localhost"

It also provides convenience methods to parse HTTP messages into individual header elements.它还提供了方便的方法来解析HTTP消息到个人头元素。

HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 
    HttpStatus.SC_OK, "OK");
response.addHeader("Set-Cookie", 
    "c1=a; path=/; domain=localhost");
response.addHeader("Set-Cookie", 
    "c2=b; path=\"/\", c3=c; domain=\"localhost\"");

HeaderElementIterator it = new BasicHeaderElementIterator(
    response.headerIterator("Set-Cookie"));

while (it.hasNext()) {
    HeaderElement elem = it.nextElement(); 
    System.out.println(elem.getName() + " = " + elem.getValue());
    NameValuePair[] params = elem.getParameters();
    for (int i = 0; i < params.length; i++) {
        System.out.println(" " + params[i]);
    }
}

stdout >stdout >

c1 = a
path=/
domain=localhost
c2 = b
path=/
c3 = c
domain=localhost

1.1.4. 1 1 4。HTTP entityHTTP实体

HTTP messages can carry a content entity associated with the request or response. HTTP消息可以携带一个内容实体相关的请求或响应。Entities can be found in some requests and in some responses, as they are optional. 实体可以发现在一些请求和在某些反应,因为它们是可选的。Requests that use entities are referred to as entity enclosing requests. 要求使用实体被称为实体封闭请求。The HTTP specification defines two entity enclosing request methods:HTTP规范定义了两个实体封闭请求方法: POSTand PUT. Responses are usually expected to enclose a content entity. 反应通常将附上一个内容实体。There are exceptions to this rule such as responses to当然也有例外,如反应 HEADmethod and方法和 204 No Content, 304 Not Modified, 205 Reset Contentresponses.响应。

HttpClient distinguishes three kinds of entities, depending on where their content originates:HttpClient区分三种类型的实体,根据他们的内容来源于:

  • streamed:流:The content is received from a stream, or generated on the fly. 收到的内容从一个流,或动态生成。In particular, this category includes entities being received from HTTP responses. 特别是,这个类别包括实体被收到HTTP响应。Streamed entities are generally not repeatable.流实体通常不是可重复的。

  • self-contained:独立的:The content is in memory or obtained by means that are independent from a connection or other entity. 内容是在内存或得到意味着独立于一个连接或其他实体。Self-contained entities are generally repeatable. 独立的实体通常是可重复的。This type of entities will be mostly used for entity enclosing HTTP requests.这种类型的实体将主要用于实体封闭的HTTP请求。

  • wrapping:包装:The content is obtained from another entity.内容可由另一个实体。

This distinction is important for connection management when streaming out content from an HTTP response. 这种区别是重要的连接管理当涌出内容从一个HTTP响应。For request entities that are created by an application and only sent using HttpClient, the difference between streamed and self-contained is of little importance. 为请求的实体,是由一个应用程序,只发送使用HttpClient,区别和独立的流是不重要的。In that case, it is suggested to consider non-repeatable entities as streamed, and those that are repeatable as self-contained.在这种情况下,建议考虑不可重复实体,那些流可重复的独立。

1.1.4.1. 1 1 4 1。Repeatable entities可重复的实体

An entity can be repeatable, meaning its content can be read more than once. 一个实体可以是可重复的,这意味着它的内容可以读不止一次。This is only possible with self contained entities (like这是唯一可能与自包含的实体(如 ByteArrayEntityor StringEntity)

1.1.4.2. 1 1 4 2。Using HTTP entities使用HTTP实体

Since an entity can represent both binary and character content, it has support for character encodings (to support the latter, ie. 因为一个实体可以表示两��二进制和字符内容,它支持字符编码(支持后者,即。character content).字符内容)。

The entity is created when executing a request with enclosed content or when the request was successful and the response body is used to send the result back to the client.实体是执行请求时创建与封闭的内容或当请求成功了,身体的反应是用于将结果发送回客户端。

To read the content from the entity, one can either retrieve the input stream via the阅读内容的实体,一个可以获取输入流的通过 HttpEntity#getContent()method, which returns an方法,它返回一个 java.io.InputStream, or one can supply an output stream to the,或一个可以提供一个输出流 HttpEntity#writeTo(OutputStream)method, which will return once all content has been written to the given stream.方法,该方法将返回一旦所有内容已经被写入给定的流。

When the entity has been received with an incoming message, the methods当实体已收到与传入消息的方法 HttpEntity#getContentType()and HttpEntity#getContentLength()methods can be used for reading the common metadata such as方法可以用于阅读常见的元数据,例如 Content-Typeand Content-Lengthheaders (if they are available). 标题(如果可用)。Since the Content-Typeheader can contain a character encoding for text mime-types like text/plain or text/html, the头可以包含字符编码文本mime类型text /普通或者像text / html, HttpEntity#getContentEncoding()method is used to read this information. 方法是用来读取这些信息。If the headers aren't available, a length of -1 will be returned, and NULL for the content type. 如果标题并不可用,一个长度为1,将返回空的内容类型。If the如果 Content-Typeheader is available, a头可用,一个 Headerobject will be returned.对象将被返回。

When creating an entity for a outgoing message, this meta data has to be supplied by the creator of the entity.当创建一个实体,一个即将卸任的消息,这个元数据必须由造物主的实体。

StringEntity myEntity = new StringEntity("important message", 
   ContentType.create("text/plain", "UTF-8"));

System.out.println(myEntity.getContentType());
System.out.println(myEntity.getContentLength());
System.out.println(EntityUtils.toString(myEntity));
System.out.println(EntityUtils.toByteArray(myEntity).length);

stdout >stdout >

Content-Type: text/plain; charset=utf-8
17
important message
17

1.1.5. 1 1 5。Ensuring release of low level resources确保发布的资源水平低

In order to ensure proper release of system resources one must close either the content stream associated with the entity or the response itself为了确保适当的释放系统资源必须关闭或内容流相关实体或响应本身

CloseableHttpClient httpclient = HttpClients.createDefault();
HttpGet httpget = new HttpGet("http://localhost/");
CloseableHttpResponse response = httpclient.execute(httpget);
try {
    HttpEntity entity = response.getEntity();
    if (entity != null) {
        InputStream instream = entity.getContent();
        try {
            // do something useful
        } finally {
            instream.close();
        }
    }
} finally {
    response.close();
}

The difference between closing the content stream and closing the response is that the former will attempt to keep the underlying connection alive by consuming the entity content while the latter immediately shuts down and discards the connection.关的区别内容流和关闭响应是前者将试图保持底层连接活着通过消费实体内容而后者立即关闭和丢弃连接。

Please note that the请注意, HttpEntity#writeTo(OutputStream)method is also required to ensure proper release of system resources once the entity has been fully written out. 方法也需要确保适当的释放系统资源一旦实体已经完全写出来。If this method obtains an instance of如果这种方法获得的一个实例 java.io.InputStreamby calling通过调用 HttpEntity#getContent(), it is also expected to close the stream in a finally clause.,它也将关闭该流在最后条款。

When working with streaming entities, one can use the在处理流的实体,一个可以使用 EntityUtils#consume(HttpEntity)method to ensure that the entity content has been fully consumed and the underlying stream has been closed.方法以确保实体内容已经被完全消耗和底层流已经关闭。

There can be situations, however, when only a small portion of the entire response content needs to be retrieved and the performance penalty for consuming the remaining content and making the connection reusable is too high, in which case one can terminate the content stream by closing the response.在某些情况下,然而,当只有一小部分整个响应内容需要被检索和性能损失消耗剩余的内容并进行连接可重用的太高了,在这种情况下,一个可以终止内容流关闭响应。

CloseableHttpClient httpclient = HttpClients.createDefault();
HttpGet httpget = new HttpGet("http://localhost/");
CloseableHttpResponse response = httpclient.execute(httpget);
try {
    HttpEntity entity = response.getEntity();
    if (entity != null) {
        InputStream instream = entity.getContent();
        int byteOne = instream.read();
        int byteTwo = instream.read();
        // Do not need the rest
    }
} finally {
    response.close();
}

The connection will not be reused, but all level resources held by it will be correctly deallocated.该连接将不被重用,但所有水平持有的资源分配将是正确的。

1.1.6. 1 1 6。Consuming entity content消费实体内容

The recommended way to consume the content of an entity is by using its推荐的方式消费内容的一个实体是利用它的 HttpEntity#getContent()or HttpEntity#writeTo(OutputStream)methods. 方法。HttpClient also comes with theHttpClient采用的也是 EntityUtilsclass, which exposes several static methods to more easily read the content or information from an entity. 类,它暴露了一些静态方法更容易阅读的内容或信息从一个实体。Instead of reading the而不是阅读 java.io.InputStreamdirectly, one can retrieve the whole content body in a string / byte array by using the methods from this class. 直接,一个可以检索整个内容的身体在一个字符串/字节数组的使用方法从这个类。However, the use of然而,使用 EntityUtilsis strongly discouraged unless the response entities originate from a trusted HTTP server and are known to be of limited length.不提倡,除非响应实体源自一个可信的HTTP服务器和已知的有限的长度。

CloseableHttpClient httpclient = HttpClients.createDefault();
HttpGet httpget = new HttpGet("http://localhost/");
CloseableHttpResponse response = httpclient.execute(httpget);
try {
    HttpEntity entity = response.getEntity();
    if (entity != null) {
        long len = entity.getContentLength();
        if (len != -1 && len < 2048) {
            System.out.println(EntityUtils.toString(entity));
        } else {
            // Stream content out
        }
    }
} finally {
    response.close();
}

In some situations it may be necessary to be able to read entity content more than once. 在某些情况下可能需要能够阅读实体内容超过一次。In this case entity content must be buffered in some way, either in memory or on disk. 在这种情况下实体内容必须以某种方式进行缓冲,无论是在内存或磁盘。The simplest way to accomplish that is by wrapping the original entity with the最简单的实现方式,是通过包装原始实体的 BufferedHttpEntityclass. 类。This will cause the content of the original entity to be read into a in-memory buffer. 这将导致原实体的内容读取到内存缓冲区。In all other ways the entity wrapper will be have the original one.在所有其他方面,实体包装会有原始的一个。

CloseableHttpResponse response = <...>
HttpEntity entity = response.getEntity();
if (entity != null) {
    entity = new BufferedHttpEntity(entity);
}

1.1.7. 1 1 7。Producing entity content生产实体内容

HttpClient provides several classes that can be used to efficiently stream out content though HTTP connections. HttpClient提供几个类,可以用来有效地流内容虽然HTTP连接。Instances of those classes can be associated with entity enclosing requests such as这些类的实例可以关联到实体封闭等请求 POSTand PUTin order to enclose entity content into outgoing HTTP requests. 为了附上实体内容为即将离任的HTTP请求。HttpClient provides several classes for most common data containers such as string, byte array, input stream, and file:HttpClient提供几个类最常见的数据容器,比如字符串,字节数组,输入流和文件: StringEntity, ByteArrayEntity, InputStreamEntity, and,和 FileEntity.

File file = new File("somefile.txt");
FileEntity entity = new FileEntity(file, ContentType.create("text/plain", "UTF-8"));        

HttpPost httppost = new HttpPost("http://localhost/action.do");
httppost.setEntity(entity);

Please note请注意 InputStreamEntityis not repeatable, because it can only read from the underlying data stream once. 是不可重复的,因为它只能读从底层数据流一次。Generally it is recommended to implement a custom一般建议来实现一个自定义的 HttpEntityclass which is self-contained instead of using the generic类,它是独立的,而不是使用泛型 InputStreamEntity. FileEntitycan be a good starting point.可以是一个很好的起点。

1.1.7.1. 1 1 7 1。HTML formsHTML表单

Many applications need to simulate the process of submitting an HTML form, for instance, in order to log in to a web application or submit input data. 许多应用程序需要模拟一个HTML表单提交的过程,例如,以登录到web应用程序或提交输入数据。HttpClient provides the entity classHttpClient提供��体类 UrlEncodedFormEntityto facilitate the process.促进这个过程。

List<NameValuePair> formparams = new ArrayList<NameValuePair>();
formparams.add(new BasicNameValuePair("param1", "value1"));
formparams.add(new BasicNameValuePair("param2", "value2"));
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams, Consts.UTF_8);
HttpPost httppost = new HttpPost("http://localhost/handler.do");
httppost.setEntity(entity);

The这个 UrlEncodedFormEntityinstance will use the so called URL encoding to encode parameters and produce the following content:实例将使用所谓的URL编码编码参数和生成以下内容:

param1=value1&param2=value2

1.1.7.2. 1 1 7 2。Content chunking内容分块

Generally it is recommended to let HttpClient choose the most appropriate transfer encoding based on the properties of the HTTP message being transferred. 一般建议让HttpClient选择最适当的传输编码基于HTTP消息的属性被转移。It is possible, however, to inform HttpClient that chunk coding is preferred by setting它是可能的,然而,通知HttpClient,块编码优先设置 HttpEntity#setChunked()to true. 为true。Please note that HttpClient will use this flag as a hint only. 请注意,HttpClient将使用此标志作为一个提示只有。This value will be ignored when using HTTP protocol versions that do not support chunk coding, such as HTTP/1.0.这个值将被忽略在使用HTTP协议版本,不支持块编码,如HTTP / 1.0。

StringEntity entity = new StringEntity("important message",
        ContentType.create("plain/text", Consts.UTF_8));
entity.setChunked(true);
HttpPost httppost = new HttpPost("http://localhost/acrtion.do");
httppost.setEntity(entity);

1.1.8. 1.1.8。Response handlers响应处理程序

The simplest and the most convenient way to handle responses is by using the最简单、最方便的方式来处理响应是通过使用 ResponseHandlerinterface, which includes the接口,包括 handleResponse(HttpResponse response)method. 法。This method completely relieves the user from having to worry about connection management. 这个方法完全缓解用户不必担心连接管理。When using a当使用一个 ResponseHandler, HttpClient will automatically take care of ensuring release of the connection back to the connection manager regardless whether the request execution succeeds or causes an exception.,HttpClient将自动照顾确保释放连接回连接管理器的请求执行不论成功或导致异常。

CloseableHttpClient httpclient = HttpClients.createDefault();
HttpGet httpget = new HttpGet("http://localhost/json");

ResponseHandler<MyJsonObject> rh = new ResponseHandler<MyJsonObject>() {

    @Override
    public JsonObject handleResponse(
            final HttpResponse response) throws IOException {
        StatusLine statusLine = response.getStatusLine();
        HttpEntity entity = response.getEntity();
        if (statusLine.getStatusCode() >= 300) {
            throw new HttpResponseException(
                    statusLine.getStatusCode(),
                    statusLine.getReasonPhrase());
        }
        if (entity == null) {
            throw new ClientProtocolException("Response contains no content");
        }
        Gson gson = new GsonBuilder().create();
        ContentType contentType = ContentType.getOrDefault(entity);
        Charset charset = contentType.getCharset();
        Reader reader = new InputStreamReader(entity.getContent(), charset);
        return gson.fromJson(reader, MyJsonObject.class);
    }
};
MyJsonObject myjson = client.execute(httpget, rh);

1.2. 1.2。HttpClient interfaceHttpClient接口

HttpClientinterface represents the most essential contract for HTTP request execution. 接口代表了最基本的HTTP请求执行合同。It imposes no restrictions or particular details on the request execution process and leaves the specifics of connection management, state management, authentication and redirect handling up to individual implementations. 它没有强加任何限制或特定的细节在请求执行过程和树叶的细节连接管理、状态管理、身份验证和重定向处理个人实现。This should make it easier to decorate the interface with additional functionality such as response content caching.这将使它更容易装饰界面附加功能如响应内容缓存。

Generally一般 HttpClientimplementations act as a facade to a number of special purpose handler or strategy interface implementations responsible for handling of a particular aspect of the HTTP protocol such as redirect or authentication handling or making decision about connection persistence and keep alive duration. 实现作为一个正面,一个数量的专用处理程序或策略接口实现负责处理的特定方面的HTTP协议如重定向或身份验证处理、决策有关连接持久性和维持时间。This enables the users to selectively replace default implementation of those aspects with custom, application specific ones.这使得用户可以选择性地替换默认实现使用自定义的那些方面,特定于应用程序的。

ConnectionKeepAliveStrategy keepAliveStrat = new DefaultConnectionKeepAliveStrategy() {

    @Override
    public long getKeepAliveDuration(
            HttpResponse response,
            HttpContext context) {
        long keepAlive = super.getKeepAliveDuration(response, context);
        if (keepAlive == -1) {
            // Keep connections alive 5 seconds if a keep-alive value
            // has not be explicitly set by the server
            keepAlive = 5000;
        }
        return keepAlive;
    }

};
CloseableHttpClient httpclient = HttpClients.custom()
        .setKeepAliveStrategy(keepAliveStrat)
        .build();

1.2.1. 1.2.1。HttpClient thread safetyHttpClient线程安全

HttpClientimplementations are expected to be thread safe. 实现预计是线程安全的。It is recommended that the same instance of this class is reused for multiple request executions.建议相同的这个类的实例是在多个请求重用死刑。

1.2.2. 1.2.2。HttpClient resource deallocationHttpClient资源回收

When an instance当一个实例 CloseableHttpClientis no longer needed and is about to go out of scope the connection manager associated with it must be shut down by calling the不再需要和即将走出范围与之关联的连接管理器必须关闭通过调用吗 CloseableHttpClient#close()method.法。

CloseableHttpClient httpclient = HttpClients.createDefault();
try {
    <...>
} finally {
    httpclient.close();
}

1.3. 1.3。HTTP execution contextHTTP执行上下文

Originally HTTP has been designed as a stateless, response-request oriented protocol. 最初的HTTP被设计成为一个无状态、响应请求定向协议。However, real world applications often need to be able to persist state information through several logically related request-response exchanges. 然而,真实世界的应用程序通常需要能够持续状态信息通过一些逻辑上相关的请求-响应交换。In order to enable applications to maintain a processing state HttpClient allows HTTP requests to be executed within a particular execution context, referred to as HTTP context. 为了使应用程序能够保持处理状态HttpClient允许HTTP请求执行在一个特定的执行上下文,称为HTTP上下文。Multiple logically related requests can participate in a logical session if the same context is reused between consecutive requests. 多个逻辑上相关的请求可以参与一个逻辑会话如果相同的上下文是连续请求之间重用。HTTP context functions similarly to aHTTP上下文功能类似 java.util.Map<String, Object>. It is simply a collection of arbitrary named values. 它仅仅是一组任意命名的值。An application can populate context attributes prior to request execution or examine the context after the execution has been completed.一个应用程序可以填充上下文属性请求执行之前或检查上下文执行后已经完成。

HttpContextcan contain arbitrary objects and therefore may be unsafe to share between multiple threads. 可以包含任意的对象,因此可能是不安全的,在多个线程间共享。It is recommended that each thread of execution maintains its own context.建议每个线程执行的维护自己的上下文。

In the course of HTTP request execution HttpClient adds the following attributes to the execution context:HTTP请求的过程中添加以下属性执行HttpClient到执行上下文:

  • HttpConnectioninstance representing the actual connection to the target server.实例代表实际连接到目标服务器。

  • HttpHostinstance representing the connection target.实例代表连接的目标。

  • HttpRouteinstance representing the complete connection route实例代表完整的连接线路

  • HttpRequestinstance representing the actual HTTP request. 实例代表实际的HTTP请求。The final HttpRequest object in the execution context always represents the state of the message最后的HttpRequest对象在执行上下文总是代表着国家的消息 exactly到底as it was sent to the target server. 因为它是发送到目标服务器。Per default HTTP/1.0 and HTTP/1.1 use relative request URIs. 每违约HTTP / 1.0和HTTP / 1.1使用相对uri的请求。However if the request is sent via a proxy in a non-tunneling mode then the URI will be absolute.然而如果请求被发送通过代理在一个非隧道模式然后URI将是绝对的。

  • HttpResponseinstance representing the actual HTTP response.实例代表实际的HTTP响应。

  • java.lang.Booleanobject representing the flag indicating whether the actual request has been fully transmitted to the connection target.对象,该对象代表了标志,指示是否实际的请求已经被完全传输到连接的目标。

  • RequestConfigobject representing the actual request configuation.对象代表实际的要求配制。

  • java.util.List<URI>object representing a collection of all redirect locations received in the process of request execution.对象代表一个集合的所有位置重定向的过程中收到的请求执行。

One can use一个可以使用 HttpClientContextadaptor class to simplify interractions with the context state.适配器类来简化interractions与上下文状态。

HttpContext context = <...>
HttpClientContext clientContext = HttpClientContext.adapt(context);
HttpHost target = clientContext.getTargetHost();
HttpRequest request = clientContext.getRequest();
HttpResponse response = clientContext.getResponse();
RequestConfig config = clientContext.getRequestConfig();

Multiple request sequences that represent a logically related session should be executed with the same多个请求序列代表一个逻辑相关会话应该执行相同的 HttpContextinstance to ensure automatic propagation of conversation context and state information between requests.实例以确保自动传播的对话上下文和国家之间的信息请求。

In the following example the request configuration set by the initial request will be kept in the execution context and get propagatd to the consecutive requests sharing the same context.在以下示例请求配置设置的初始请求将被保存在执��上下文和得到propagatd连续的请求共享相同的上下文。

CloseableHttpClient httpclient = HttpClients.createDefault();
RequestConfig requestConfig = RequestConfig.custom()
        .setSocketTimeout(1000)
        .setConnectTimeout(1000)
        .build();

HttpGet httpget1 = new HttpGet("http://localhost/1");
httpget1.setConfig(requestConfig);
CloseableHttpResponse response1 = httpclient.execute(httpget1, context);
try {
    HttpEntity entity1 = response1.getEntity();
} finally {
    response1.close();
}
HttpGet httpget2 = new HttpGet("http://localhost/2");
CloseableHttpResponse response2 = httpclient.execute(httpget2, context);
try {
    HttpEntity entity2 = response2.getEntity();
} finally {
    response2.close();
}

1.4. 1.4。Exception handling异常处理

HttpClient can throw two types of exceptions:HttpClient可以抛出两种类型的异常: java.io.IOExceptionin case of an I/O failure such as socket timeout or an socket reset and在案件的一个I / O失败如套接字超时或一个插座复位和 HttpExceptionthat signals an HTTP failure such as a violation of the HTTP protocol. ,信号一个HTTP失败如违反了HTTP协议。Usually I/O errors are considered non-fatal and recoverable, whereas HTTP protocol errors are considered fatal and cannot be automatically recovered from.通常的I / O错误被认为是致命的和可恢复的,而HTTP协议错误被认为是致命的,不能自动恢复。

1.4.1. 1.4.1。HTTP transport safetyHTTP传输安全

It is important to understand that the HTTP protocol is not well suited to all types of applications. 重要的是要理解HTTP协议,并不适合所有类型的应用程序。HTTP is a simple request/response oriented protocol which was initially designed to support static or dynamically generated content retrieval. HTTP是一个简单的请求/响应定向协议最初设计用来支持静态或动态生成的内容检索。It has never been intended to support transactional operations. 它从未打算支持事务操作。For instance, the HTTP server will consider its part of the contract fulfilled if it succeeds in receiving and processing the request, generating a response and sending a status code back to the client. 例如,HTTP服务器将考虑它的部分合同完成的如果它成功地接收和处理请求,生成一个响应和发送回客户机的状态代码。The server will make no attempt to roll back the transaction if the client fails to receive the response in its entirety due to a read timeout, a request cancellation or a system crash. 服务器将不打算回滚该事务,如果客户未能接收响应完整由于读超时,请求取消或系统崩溃。If the client decides to retry the same request, the server will inevitably end up executing the same transaction more than once. 如果客户决定重试相同的请求,服务器最终都不可避免地执行相同的事务不止一次。In some cases this may lead to application data corruption or inconsistent application state.在某些情况下,这可能导致应用程序数据腐败或不一致的应用程序的状态。

Even though HTTP has never been designed to support transactional processing, it can still be used as a transport protocol for mission critical applications provided certain conditions are met. 尽管HTTP从未被设计用来支持事务处理,它仍然可以被用作一个传输协议为关键任务的应用程序提供了符合特定的条件。To ensure HTTP transport layer safety the system must ensure the idempotency of HTTP methods on the application layer.以确保HTTP传输层安全系统必须确保幂等性的HTTP方法在应用程序层。

1.4.2. 1.4.2。Idempotent methods幂等方法

HTTP/1.1 specification defines an idempotent method asHTTP / 1.1规范定义了一个幂等方法

[Methods can also have the property of "idempotence" in that (aside from error or expiration issues) the side-effects of N > 0 identical requests is the same as for a single request方法也可以有属性的“幂等性”,(除了错误或过期问题)的副作用N > 0完全相同的请求一样单个请求]

In other words the application ought to ensure that it is prepared to deal with the implications of multiple execution of the same method. 换句话说,应用程序应该确保它是准备好应对多个执行的含义相同的方法。This can be achieved, for instance, by providing a unique transaction id and by other means of avoiding execution of the same logical operation.这可以实现,例如,通过提供一个独特的事务id和通过其他方式避免执行相同的逻辑操作。

Please note that this problem is not specific to HttpClient. 请注意,这个问题不是特定于HttpClient。Browser based applications are subject to exactly the same issues related to HTTP methods non-idempotency.基于浏览器的应用程序都受到相同的HTTP方法相关问题非幂等性。

HttpClient assumes non-entity enclosing methods such asHttpClient假定非封闭等方法 GETand HEADto be idempotent and entity enclosing methods such as是幂等和实体方法如封闭 POSTand PUTto be not.不。

1.4.3. 3。Automatic exception recovery自动异常恢复

By default HttpClient attempts to automatically recover from I/O exceptions. 默认情况下HttpClient尝试自动恢复I / O例外。The default auto-recovery mechanism is limited to just a few exceptions that are known to be safe.默认自动还原机制是局限于少数例外,已知是安全的。

  • HttpClient will make no attempt to recover from any logical or HTTP protocol errors (those derived fromHttpClient没有试图恢复任何逻辑或HTTP协议错误(来自 HttpExceptionclass).类)。

  • HttpClient will automatically retry those methods that are assumed to be idempotent.HttpClient将自动重试这些方法,被认为是等幂的。

  • HttpClient will automatically retry those methods that fail with a transport exception while the HTTP request is still being transmitted to the target server (i.e. the request has not been fully transmitted to the server).HttpClient将自动重试失败的那些方法与运输异常而HTTP请求仍被传送到目标服务器(即请求尚未完全传递到服务器)。

1.4.4. 1 4 4。Request retry handler请求重试处理程序

In order to enable a custom exception recovery mechanism one should provide an implementation of the为了使一个自定义异常恢复机制应该提供一个实现的 HttpRequestRetryHandlerinterface.接口。

HttpRequestRetryHandler myRetryHandler = new HttpRequestRetryHandler() {

    public boolean retryRequest(
            IOException exception,
            int executionCount,
            HttpContext context) {
        if (executionCount >= 5) {
            // Do not retry if over max retry count
            return false;
        }
        if (exception instanceof InterruptedIOException) {
            // Timeout
            return false;
        }
        if (exception instanceof UnknownHostException) {
            // Unknown host
            return false;
        }
        if (exception instanceof ConnectTimeoutException) {
            // Connection refused
            return false;
        }
        if (exception instanceof SSLException) {
            // SSL handshake exception
            return false;
        }
        HttpClientContext clientContext = HttpClientContext.adapt(context);
        HttpRequest request = clientContext.getRequest();
        boolean idempotent = !(request instanceof HttpEntityEnclosingRequest);
        if (idempotent) {
            // Retry if the request is considered idempotent
            return true;
        }
        return false;
    }

};
CloseableHttpClient httpclient = HttpClients.custom()
        .setRetryHandler(myRetryHandler)
        .build();

1.5. 1.5。Aborting requests流产请求

In some situations HTTP request execution fails to complete within the expected time frame due to high load on the target server or too many concurrent requests issued on the client side. 在某些情况下HTTP请求执行未能完全在预期的时间框架由于高负荷在目标服务器上或太多的并发请求发布了在客户端。In such cases it may be necessary to terminate the request prematurely and unblock the execution thread blocked in a I/O operation. 在这种情况下可能需要提前终止请求和开启执行线程阻塞在I / O操作。HTTP requests being executed by HttpClient can be aborted at any stage of execution by invoking正在执行HTTP请求HttpClient可以中止执行的任何阶段通过调用 HttpUriRequest#abort()method. 法。This method is thread-safe and can be called from any thread. 这个方法是线程安全的,可以要求任何线程。When an HTTP request is aborted its execution thread - even if currently blocked in an I/O operation - is guaranteed to unblock by throwing a当一个HTTP请求中止执行线程,即使目前的阻塞I / O操作,保证开启通过抛出 InterruptedIOException

1.6. 1.6。HTTP protocol interceptorsHTTP协议拦截器

Th HTTP protocol interceptor is a routine that implements a specific aspect of the HTTP protocol. 拦截器是一个HTTP协议Th常规,实现一个特定的方面的HTTP协议。Usually protocol interceptors are expected to act upon one specific header or a group of related headers of the incoming message, or populate the outgoing message with one specific header or a group of related headers. 通常协议拦截器将一个特定的行动头或一组相关的标题传入消息,或填充输出消息与一个特定的头或一组相关的标题。Protocol interceptors can also manipulate content entities enclosed with messages - transparent content compression / decompression being a good example. 协议拦截器也可以操纵内容实体附信息-透明的内容压缩/解压是一个很好的例子。Usually this is accomplished by using the 'Decorator' pattern where a wrapper entity class is used to decorate the original entity. 这通常是通过使用“装饰”模式,其中一个包装实体类是用于装饰原来的实体。Several protocol interceptors can be combined to form one logical unit.几个协议拦截器可以合并,形成一个逻辑单元。

Protocol interceptors can collaborate by sharing information - such as a processing state - through the HTTP execution context. 协议拦截器可以通过共享信息协作——比如处理状态——通过HTTP执行上下文。Protocol interceptors can use HTTP context to store a processing state for one request or several consecutive requests.协议拦截器可以使用HTTP上下文存储处理状态为一个请求或连续的请求。

Usually the order in which interceptors are executed should not matter as long as they do not depend on a particular state of the execution context. 通常的顺序执行拦截并不重要,只要他们不依赖于一个特定状态的执行上下文。If protocol interceptors have interdependencies and therefore must be executed in a particular order, they should be added to the protocol processor in the same sequence as their expected execution order.如果协议拦截器有相互依赖关系,因此必须以特定的顺序,他们应该被添加到协议处理器在同一序列作为他们的预期的执行顺序。

Protocol interceptors must be implemented as thread-safe. 协议拦截器必须实现为线程安全的。Similarly to servlets, protocol interceptors should not use instance variables unless access to those variables is synchronized.类似于servlet、协议拦截器不应使用实例变量,除非访问这些变量是同步的。

This is an example of how local context can be used to persist a processing state between consecutive requests:这是一个例子,说明本地上下文可以用来持久化处理状态连续之间的请求:

CloseableHttpClient httpclient = HttpClients.custom()
        .addInterceptorLast(new HttpRequestInterceptor() {

            public void process(
                    final HttpRequest request,
                    final HttpContext context) throws HttpException, IOException {
                AtomicInteger count = (AtomicInteger) context.getAttribute("count");
                request.addHeader("Count", Integer.toString(count.getAndIncrement()));
            }

        })
        .build();

AtomicInteger count = new AtomicInteger(1);
HttpClientContext localContext = HttpClientContext.create();
localContext.setAttribute("count", count);

HttpGet httpget = new HttpGet("http://localhost/");
for (int i = 0; i < 10; i++) {
    CloseableHttpResponse response = httpclient.execute(httpget, localContext);
    try {
        HttpEntity entity = response.getEntity();
    } finally {
        response.close();
    }
}

1.7. 1.7。Redirect handling重定向处理

HttpClient handles all types of redirects automatically, except those explicitly prohibited by the HTTP specification as requiring user intervention.HttpClient处理所有类型的自动重定向,除明确禁止的HTTP规范需要用户干预。 See Other(status code 303) redirects on(状态码303)重定向在 POSTand PUTrequests are converted to请求转换为 GETrequests as required by the HTTP specification. 请求所需的HTTP规范。One can use a custom redirect strategy to relaxe restrictions on automatic redirection of POST methods imposed by the HTTP specification.一个可以使用一个自定义的重定向策略限制自动重定向的放松一下POST方法实施的HTTP规范。

LaxRedirectStrategy redirectStrategy = new LaxRedirectStrategy();
CloseableHttpClient httpclient = HttpClients.custom()
        .setRedirectStrategy(redirectStrategy)
        .build();

HttpClient often has to rewrite the request message in the process of its execution. HttpClient常常不得不重写请求消息的过程中,其执行。Per default HTTP/1.0 and HTTP/1.1 generally use relative request URIs. 每违约HTTP / 1.0和HTTP / 1.1一般使用相对uri的请求。Likewise, original request may get redirected from location to another multiple times. 同样,原始请求可能会重定向到另一个多次从位置。The final interpreted absolute HTTP location can be built using the original request and the context. 最后的解释绝对HTTP位置可以使用原始的请求和上下文。The utility method该实用程序方法 URIUtils#resolvecan be used to build the interpreted absolute URI used to generate the final request. 可以用来构建解释绝对URI用于生成最后的请求。This method includes the last fragment identifier from the redirect requests or the original request.该方法包括过去的片段标识符的请求重定向或原始请求。

CloseableHttpClient httpclient = HttpClients.createDefault();
HttpClientContext context = HttpClientContext.create();
HttpGet httpget = new HttpGet("http://localhost:8080/");
CloseableHttpResponse response = httpclient.execute(httpget, context);
try {
    HttpHost target = context.getTargetHost();
    List<URI> redirectLocations = context.getRedirectLocations();
    URI location = URIUtils.resolve(httpget.getURI(), target, redirectLocations);
    System.out.println("Final HTTP location: " + location.toASCIIString());
    // Expected to be an absolute URI
} finally {
    response.close();
}