The hierarchy function of Httpparams

Source: Internet
Author: User
Tags http post

HttpClient is a Java-based HTTP client-side implementation that supports HTTP 1.0 and HTTP 1.1, and httpclient itself does a lot of compatibility with HTTP protocols so that it can be used with some common HTTP proxies and HTTP Server communications (these agents and servers are not implemented strictly according to protocol standards).

In essence, HttpClient uses Java sockets to communicate with the server, except that the protocol for communication is the HTTP protocol. Therefore, in order to successfully communicate, you need to set a lot of parameters, mainly divided into two types: HTTP protocol-related parameters, (the protocol version of the two sides used, is HTTP1.1 or HTTP1.0), there are some network communication parameters, such as the timing of the connection, whether the use of tcpnodelay and so on.

In HttpClient, interface httpparams defines the generic interface for these parameters, viewing httpparams, we can find httpparams support hierarchy, and a httpparams can be another httpparams's parent , and when a particular parameter is found, it is looked up from the current httpparams and is found from its parent if it is not present in the current hierarchy. The hierarchical structure of httpparams is a feature of httpclient design. This design makes it possible to set the parameters of HttpClient flexibly.

In HttpClient, the following several httpparam are implemented, and the class diagram structure is as follows:

HttpClient in the design of a more characteristic is that these parameters to achieve a level, to find a specific parameter, first from the lowest level of the Param start to check, if not, then to its parent parameter query, when found, always return the lowest level of parameters.

So why would httpclient design this kind of parametric architecture? What is the benefit of such a design?

In the use of HttpClient, the program to operate the main is the following classes: Httpclient,httpconnectionmanager,httpconnection,hostconfiguration,httpmethod. HttpClient represents an HTTP client, Httpconnectionmanager is used to manage Httpconnection, Httpconnection represents an HTTP connection, So Httpconnectionmanager is essentially an HTTP connection pool that manages these HTTP connections (and the database connection pool). HttpMethod represents an HTTP method in which a httpclient can run multiple httpmethod (essentially, when HttpClient executes a httpmethod, Gets a httpconnection from the ConnectionManager, executes the method on this connection, and after the method is executed, let method release the Httpconnection. The httpconnection is then returned to the ConnectionManager for subsequent use by method.

Instance code: Multithreadedhttpconnectionmanager ConnectionManager = new Multithreadedhttpconnectionmanager ();
HttpClient client = new HttpClient (ConnectionManager);
...
In a thread.
GetMethod GetMethod = new GetMethod ("http://www.blogbus.com/");
try {
Client.executemethod (GetMethod);
Print response to stdout
System.out.println (Get.getresponsebodyasstream ());
finally {
Be sure the connection are released back to the connection
Manager
Getmethod.releaseconnection ();
}
Httpclient,httpconnectionmanager,httpconnection,hostconfiguration,httpmethod These classes, you can receive the corresponding Httpparam parameters, To control the HTTP protocol used, but different objects, whose parameters range in different sizes, have different levels. For example, the Httpconnectionmanager parameter hierarchy is higher than the httpconnection level. There are two meanings in this: 1. When a ConnectionManager and a httpconnection of its administration set the same parameter, the value of the parameter is taken out of the httpconnection parameter value, 2. When ConnectionManager sets a parameter, but when the parameter is not set by its managed httpconnection, the parameter value of the ConnectionManager is removed when the parameter value is obtained (these 2 points are available from the HttpClient argument architecture).

In HttpClient, the hierarchy of the httpclient is higher than the httphost and ConnectionManager parameters, and the Httphost parameter level is higher than the HttpMethod parameter. The ConnectionManager parameter hierarchy is higher than the httpconnection parameter. The corresponding parameter classes for these classes are as follows:
httpclient<-->httpclientparams|
|--Httpconnectionmanager<-–>httpconnectionmanagerparams
| |
| |--httpconnection<->httpconnectionparams
|--httphost<->httphostconfiguration<->httphostparams|
|--httpmethod<->httpmethodparams
The specific example code is as follows:
HttpClient httpclient = new HttpClient ();
Httpclient.getparams (). Setparameter (Httpclientparams.protocol_version, httpversion.http_1_1);
Httpclient.getparams (). Setparameter (Httpclientparams.so_timeout, New Integer (1000));
Httpclient.getparams (). Setparameter (Httpclientparams.http_content_charset, "Utf-8″)";
Hostconfiguration hostconfig = new Hostconfiguration ();
Hostconfig.sethost ("www.yahoo.com");

GetMethod httpget = new GetMethod ("/");
Httpget.getparams (). Setparameter (Httpmethodparams.so_timeout, New Integer (5000));

Postmethod HttpPost = new Postmethod ("/");
try {
Internally the parameter collections would be linked together
By performing the following operations:
Hostconfig.getparams (). SetDefaults (Httpclient.getparams ());
Httpget.getparams (). SetDefaults (Hostconfig.getparams ());
Httpclient.executemethod (Hostconfig, HttpGet);
Httpclient.executemethod (Hostconfig,httppost);
System.out.println ("The HTTP Get:" + httpget.getparams (). GetParameter (httpmethodparams.protocol_version));
System.out.println ("The HTTP Get:" + httpget.getparams (). GetParameter (Httpmethodparams.so_timeout));
System.out.println ("The HTTP Get:" + httpget.getparams (). GetParameter (Httpmethodparams.http_content_charset));

System.out.println ("The HTTP Post:" + Httppost.getparams (). GetParameter (Httpmethodparams.so_timeout));
}catch (HttpException e) {

E.printstacktrace ();
}catch (IOException ex) {

Ex.printstacktrace ();
} finally {
httpget.releaseconnection ();
} The program output is:
The HTTP get:http/1.1
the HTTP get:5000
the HTTP get:utf-8
The HTTP post:1000
Read here, we can probably guess why the parameters of the httpclient system to design this way, in fact, there are 2 reasons for this design:
1, the parameters are hierarchical, Different levels of parameters may be set with different parameter categories. For example, in the httpclient params can set the HTTP protocol used, but httpconnection params is not suitable for this setting, its params is more suitable for storing and the underlying socket control-related parameters.
2, because the range of different levels of params is different, the application scenario is also different. For example, there are two HttpMethod, which represents access to the same server on the two applications, but the response speed of these 2 applications is not the same, a very fast, 10ms can return, a very slow, to 100ms before you can return.
at the same time, assuming that these two method are all used by the same httpconnection (serial use), then when setting the read timeout, we should set the HttpMethod, because this is related to the specific application response time, and set the connection timeout, You only need to set up on the Httpconnection, because 2 applications are on the same machine, and the network link time consuming situation is the same.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.