Configure HTTP parameters in httpclient

Source: Internet
Author: User

You can download httpclient files from the Apache website. For HttpClient-3.1, I think an important part is configuring HTTP parameters. In fact, you can also perform simple applications based on the default httpclient parameter configuration. For parameter configuration problems, I think it is messy, but I have mastered the Inheritance Mechanism of httpclient application parameter inheritance, it is very clear.

In the HttpClient-3.1, parameter inheritance structure:

Global -- + | defaulthttpparams
|
Client | httpclient
|
+ -- Connection Manager | httpconnectionmanager
|
| + -- Connection | httpconnection
|
+ -- Host | hostconfiguration
|
+ -- Method | httpmethod
HTTP parameters configured in httpclient can be seen through the above inheritance structure. httpclient has six levels of parameter configuration: Global, client, Connection Manager, connection, host, and method. The following is a combination of HttpClient-3.1 documentation and source code, to talk about the meaning of these 6 levels.

The httpclient parameter configuration is divided into six levels, which may lead to repeated configuration of the httpclient parameter. However, the document also explains that httpclient searches for parameter configurations at the inheritance level during its work. If a parameter is not configured at a minimum level, the program will automatically search for the previous level, the parameter is configured at a certain level until it is found, and then configured at runtime according to the principle closest to the lowest layer of the parameter configuration level. If no configuration is available, it is executed at the global level.

When you create httpclient and httpmethod instances, You can execute httpmethod without configuration. It will be configured at the global level. If you configure them, and the parameter configuration level is lower than the global level, the global parameter configuration is overwritten. From the inheritance level above, we can see that each different level corresponds to an object, from high to low, which is defaulthttpparams, httpclient, httpconnectionmanager, httpconnection, hostconfiguration, and httpmethod.

For details about how to set parameters at each level and what parameters can be configured, refer to the Apache official website help documentation http://hc.apache.org/httpclient-3.x/preference-api.html. The following is an example:

You can use the HTTP. Socket. Timeout parameter to configure the dry parameters, including httpclient, httpconnectionmanager, and httpmethod. The Code is as follows:

Package org. shirdrn. test;

Import java. Io. ioexception;

Import org. Apache. commons. httpclient. hostconfiguration;
Import org. Apache. commons. httpclient. httpclient;
Import org. Apache. commons. httpclient. httpexception;
Import org. Apache. commons. httpclient. httpmethodbase;
Import org. Apache. commons. httpclient. httpstatus;
Import org. Apache. commons. httpclient. Uri;
Import org. Apache. commons. httpclient. uriexception;
Import org. Apache. commons. httpclient. Methods. getmethod;

Public class myhttpclient {

Private httpclient client;
Private httpmethodbase method;
Private string stringdata;

Public myhttpclient (){
This. Client = new httpclient ();
This. method = new getmethod ();
}

Public void setparams (string URL) throws uriexception, nullpointerexception {
This. Client. getparams (). setparameter ("HTTP. Socket. Timeout", 1); // set parameters for httpclient
This. Client. gethttpconnectionmanager (). getparams (). setparameter ("HTTP. Socket. Timeout", 1); // set parameters for httpconnetionmanager

This. method. getparams (). setparameter ("HTTP. Socket. Timeout", 10000); // set parameters for httpmethod

Hostconfiguration hostconf = new hostconfiguration ();
Hostconf. sethost (New uri (URL, true ));
This. Client. sethostconfiguration (hostconf );
}

Public void execute () throws httpexception, ioexception {
Int status = this.client.exe cutemethod (this. method );
If (status = httpstatus. SC _ OK ){
This. stringdata = new string (this. method. getresponsebody (), "gb2312 ");
}
}

Public void print (){
System. Out. println (this. stringdata );
}

Public static void main (string [] ARGs ){
Myhttpclient client = new myhttpclient ();
Try {
Client. setparams ("http://www.csdn.net ");
Client.exe cute ();
Client. Print ();
} Catch (exception e ){
E. printstacktrace ();
}
}
}

In the above program, HTTP is configured at three levels: httpclient, httpconnectionmanager, and httpmethod. socket. run the timeout parameter. You can see that the page data of the specified website is downloaded. You can see that the timeout parameter set by httpmethod takes effect. Therefore, its level is the lowest among the above three levels.

You can test it. Comment out all the parameters set by httpconnectionmanager and httpmethod, and then execute the above program, a socket timeout exception will occur:

Java.net. sockettimeoutexception: Read timed out
At java.net. socketinputstream. socketread0 (native method)
At java.net. socketinputstream. Read (fig. Java: 129)
At java. Io. bufferedinputstream. Fill (bufferedinputstream. Java: 218)
At java. Io. bufferedinputstream. Read (bufferedinputstream. Java: 237)
At org. Apache. commons. httpclient. httpparser. readrawline (httpparser. Java: 78)
At org. Apache. commons. httpclient. httpparser. Readline (httpparser. Java: 106)
At org. Apache. commons. httpclient. httpconnection. Readline (httpconnection. Java: 1116)
At org. Apache. commons. httpclient. httpmethodbase. readstatusline (httpmethodbase. Java: 1973)
At org. Apache. commons. httpclient. httpmethodbase. readresponse (httpmethodbase. Java: 1735)
At org.apache.commons.httpclient.httpmethodbase.exe cute (httpmethodbase. Java: 1098)
At org.apache.commons.httpclient.httpmethoddirector.exe cutewithretry (httpmethoddirector. Java: 398)
At org.apache.commons.httpclient.httpmethoddirector.exe cutemethod (httpmethoddirector. Java: 171)
At org.apache.commons.httpclient.httpclient.exe cutemethod (httpclient. Java: 397)
At org.apache.commons.httpclient.httpclient.exe cutemethod (httpclient. Java: 323)
At org.shirdrn.test.myhttpclient.exe cute (myhttpclient. Java: 35)
At org. shirdrn. Test. myhttpclient. Main (myhttpclient. Java: 49)

Because httpclient sets the socket timeout time to 1 ms, the data of the specified website cannot be obtained.

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.