Httpclient basic analysis

This article describes the basic knowledge of httpclient and explains the related concepts. It is shared here for your reference.

1. Request execution:

The most important function of httpclient is to execute HTTP methods. Executing HTTP methods involves one or more HTTP request / HTTP response exchanges, which are usually handled internally by httpclient. The user expects to provide a request object for execution, and wants httpclient to send the request to the target server and return the corresponding response object. If the execution fails, an exception will be thrown.

Naturally, the main entry point of the httpclient API is the httpclient interface that defines the above contract.

This is an example of a request execution process. Its simplest form is:

1.1. HTTP request

All HTTP requests have a request line, including method name, request URI and HTTP protocol version.

Httpclient supports the boxes of all HTTP methods defined in the HTTP / 1.1 specification: get, head, post, put, delete, trace and options. There is no type for each method: a specific class httpget, httphead, httppost, httpput, httpdelete, httptrace, and httpoptions.

Request URI is a uniform resource identifier used to identify the resource requested by the application. The HTTP request URI consists of protocol scheme, host name, optional port, resource path, optional query and optional fragment.

1.2. HTTP Response

HTTP response is a message sent back to the client by the server after receiving and interpreting the request message. The first line of the message includes the protocol version, followed by a numeric status code and its associated text phrase.

1.3. Processing message headers

HTTP messages can contain multiple headers that describe message properties, such as content length, content type, etc. Httpclient provides methods for retrieving, adding, deleting and listing header files. The most efficient way to get all headers of a given type is to use the headeritrator interface.

It also provides a convenient way to parse HTTP messages into separate header elements.

1.4. HTTP entity

HTTP messages can carry content entities associated with requests or responses. Entities can be found in some requests and some responses because they are optional. Requests that use entities are called entity encapsulation requests. The HTTP specification defines two entity encapsulation request methods: Post and put. Responses are often expected to contain content entities. There are exceptions, such as response to head method 204 no content, 304 not modified, 205 reset content.

Httpclient distinguishes three entities according to their content sources:

Streamed: the content is received from the stream or generated immediately. In particular, the category includes entities received from HTTP responses. Streaming entities are usually not repeatable.

Self contained: content is obtained in memory or independently of connections or other entities. Self contained entities are usually repeatable. This type of entity will be mainly used to enclose the entity of HTTP request.

Wrapping: the content is obtained from another entity.

This difference is important for connection management when content flows out of an HTTP response. For request entities created by an application and sent only using httpclient, the difference between flow and independence is not important. In this case, it is recommended to treat non repeatable entities as streaming and repeatable entities as independent.

1.4. 1. Repeatable entities

An entity can be repeatable, which means that its content can be read more than once. This is the only possible self-contained entity (like bytearrayentity or stringentity)

1.4. 2. Use HTTP entity

Since an entity can represent binary and character content, it supports character encoding (to support the latter, that is, character content).

An entity is created when a request with closed content is executed, or when the request succeeds and the result is sent back to the client using the response body.

To read content from an entity, you can use httpentity Getcontent () method to retrieve the input stream, which returns a Java io. InputStream, or httpentity The writeto (OutputStream) method provides an output stream, which returns once all content has been written to the given stream.

When the entity has received the incoming message, the method httpentity Getcontenttype () and httpentity The getcontentlength () method can be used to read public metadata, such as content type headers and content length headers, if available. Because the content type title can contain character encoding of text MIME type such as text / plain or text / HTML, this httpentity The getcontentencoding () method is used to read this information. If the title is not available, the return length is - 1 and the content type is null. If the content type header is available, the header will return an object.

When creating an entity for an outgoing message, the metadata must be provided by the creator of the entity.

1.5. Ensure that low-level assets are published

In order to ensure the correct release of system resources, the content flow associated with the entity or response itself must be closed

The difference between closing the content flow and closing the response is that the former will try to maintain the underlying connection by occupying the entity content, while the latter will immediately close and abandon the connection. Note that httpentity Writeto (OutputStream) once the entity is completely written out, you also need to ensure the correct method to release system resources. If this method gets a Java io. InputStream invokes an instance of httpentity Getcontent(), then you also want to close the flow in the finally clause.

You can use this entityutils. When using stream entities Consume (httpentity) method to ensure that the entity content has been completely consumed and the underlying flow has been closed.

However, there may be cases where the performance loss of consuming the remaining content and making the connection reusable is too high when only a small part of the entire response content needs to be retrieved. In this case, the content flow response can be terminated by closing.

The connection will not be reused, but all levels of resources held by it will be allocated correctly.

1.6. Consumer entity content

The recommended way to consume entity content is to use it httpentity Getcontent () or httpentity Writeto (OutputStream) method. Httpclient also comes with the entityutils class, which exposes several static methods to make it easier to read content or information from entities. java. io. InputStream can use the methods of this class instead of directly reading the entire content body in the string / byte array. However, entityutils strongly recommends not using this feature unless the response entity is from a trusted HTTP server and its length is known to be limited.

In some cases, you may need to read the entity content multiple times. In this case, the entity content must be cached in some way, whether in memory or on disk. The simplest way is to wrap the original entity with the bufferedhttpentity class. This will cause the contents of the original entity to be read into the memory buffer. In all other respects, the solid wrapper will have the original wrapper.

1.7. Make entity content

Httpclient provides several classes that can efficiently flow out content through HTTP connections. Instances of these classes can be associated with entity enclosing requests, such as post and put, to surround the entity content into outgoing HTTP requests. Httpclient provides several classes for the most common data containers, such as strings, byte arrays, input streams, and files: stringentity, bytearrayentity, inputstreamentity, and fileentity.

1.7. 1 HTML forms many applications need to simulate the process of submitting HTML forms, for example, to log in to a web application or submit input data. Httpclient provides the entity class urlencodedformentity to facilitate the process.

The urlencodedformentity instance will use the so-called URL encoding to encode the parameters and produce the following:

1.7. 2. Content segmentation

It is generally recommended that httpclient select the most appropriate transmission code according to the properties of the HTTP message being transmitted. However, httpclient can be notified by setting httpentity Setchunked() is true, and block coding is preferred. Note that httpclient will only use this flag as a hint. This value is ignored when using an HTTP protocol version that does not support block encoding (such as HTTP / 1.0).

1.8. Response handler

The simplest and most convenient way to process the response is to use the responsehandler interface containing the handleresponse (httpresponse response) method. This method can completely relieve users from having to worry about connection management. When using responsehandlerhttpclient, httpclient will automatically ensure that the connection will be released back to the connection manager whether the request execution is successful or leads to an exception.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>