HttpClient Library API like Lucene, each version of the API changes a lot, which is a bit of a headache. It's like creating a HttpClient object, each version is different.
3.X is the normal Java syntax
HttpClient httpclient=new defaulthttpclient ();
4.3 became a Factory mode
Closeablehttpclient httpClient = Httpclients.createdefault ();
Timeout Setting method
3.Xhttpclient client =New HttpClient(); client.Setconnectiontimeout(30000); Client.SetTimeout(30000); HttpClient httpclient=New HttpClient(); HttpClient.Gethttpconnectionmanager().Getparams().Setconnectiontimeout( the);4.1HttpClient httpclient=newdefaulthttpclient(); httpClient.Getparams().Setparameter(Coreconnectionpnames.Connection_timeout, -);//Connection timeHttpClient.Getparams().Setparameter(Coreconnectionpnames.So_timeout, -);//Data transfer time4.3Closeablehttpclient httpClient = httpclients.Createdefault(); HttpGet httpget=Newhttpget("Http://www.baidu.com");//http GET Request (post identical)Requestconfig requestconfig = Requestconfig.Custom().SetSocketTimeout( -).Setconnecttimeout( -).Build();//Set request and transmit timeout timeHttpGet.Setconfig(requestconfig); httpClient.Execute(HttpGet);//Execution Request
From the set time point of view, before 4.3 is a class, is a httpclient binding a good set of configurations.
After 4.3, httpclient no longer provides time configuration parameters, but each request corresponds to a set of configurations.
btw,4.3 version does not set timeout, if the server does not respond, wait time n long (>24 hours).
HttpClient upgrade to 4.5, the API has a lot of changes, after HttpClient 4, the API has not been too stable, I feel 4.5 version of the abstraction, many APIs should be fast stable.
With HttpClient, it is generally necessary to set the connection timeout and get the data time-out time. These two parameters are important in order to prevent access to other HTTP, due to timeouts that cause your application to be affected.
In version 4.5, the settings for both parameters are abstracted into requestconfig, built by the corresponding builder, with the following examples:
Closeablehttpclient httpclient = httpclients.Createdefault(); HttpGet HttpGet =New HttpGet("http://stackoverflow.com/"); Requestconfig requestconfig = Requestconfig.Custom() .Setconnecttimeout( the).Setconnectionrequesttimeout( +) .SetSocketTimeout( the).Build(); HttpGet.Setconfig(Requestconfig); Closeablehttpresponse response = httpclient.Execute(HttpGet); System. out.println("Get the result:"+ Response.Getstatusline());//Get request resulthttpentity entity = Response.getentity();//Get the requested data back
Setconnecttimeout: Sets the connection time-out, in milliseconds.
Setconnectionrequesttimeout: Set the connection time-out from connect Manager (connection pool), in milliseconds. This property is a newly added property because the current version is available to share the connection pool.
SetSocketTimeout: The time-out (that is, the response time) of the request to obtain data in milliseconds. If you access an interface and how long it will not be able to return data, simply discard the call.
HttpClient internal three timeout settings: Connection pool Get available connection timeout, connection timeout, read data timeout
First look at the following httpclient initialization code:
Requestconfig requestconfig = Requestconfig.Custom() .Setconnectionrequesttimeout(config.Connreqtimeout)//Get the connection's time-out from the connection pool //Server Connection timeout: HttpClient creates an asynchronous thread to create a socket connection that sets the connection time-out for the socket.Setconnecttimeout(config.Conntimeout) .SetSocketTimeout(config.Sockettimeout)//socket Read Data time-out: time-out from the server for response data.Build(); HttpClient = Httpclientbuilder.Create() .Setmaxconntotal(config.Maxconntotal)maximum number of connections in//connection pool /** * The maximum number of concurrent connections assigned to the same route (route). * Route: A line running the environment machine to the target machine. * For example, we use HttpClient's implementation to request www.baidu.com resources and www.bing.com Resources separately, then he will produce two route. */.Setmaxconnperroute(config.Maxconnperroute) .Setdefaultrequestconfig(Requestconfig).Build();
Three time-out details:
1. Get the available connection timeout from the connection pool
Try to get from the connection pool when you want to use the connection in HttpClient, if you have not yet obtained an available connection after waiting for a certain amount of time (for example, there is no idle connection in the connection pool)
The Get connection timeout exception is thrown.
2. Connection Target Timeout ConnectionTimeout
Refers to the connection time-out period of the connection destination URL, which is the maximum time the customer service sends a request to the destination URL to establish a connection. If a connection has not been established within that time range, the
Throws a ConnectionTimeout exception. When testing, change the URL to a nonexistent URL: http://test.com After the timeout period of 3000ms, the system reported an exception:
Org.apache.commons.httpclient.ConnectTimeoutException:The
Host did not accept the connection within timeout of
3. Waiting for response timeout (read data timeout) sockettimeout
After connecting to the previous URL, get response's return wait time, that is, after establishing a connection to the destination URL, wait for the maximum time to put back the response, and throw sockettimeout if the response is not returned within the specified time.
The connection URL for the test was a URL that I opened locally, and http://localhost:8080/firstTest.htm?method=test in my test URL, when accessing this link, the thread sleep for a period of time to simulate the return response timeout.
Three time-out details:
1. Get the available connection timeout from the connection pool
Try to get from the connection pool when you want to use the connection in HttpClient, if you have not yet obtained an available connection after waiting for a certain amount of time (for example, there is no idle connection in the connection pool)
The Get connection timeout exception is thrown.
2. Connection Target Timeout ConnectionTimeout
Refers to the connection time-out period of the connection destination URL, which is the maximum time the customer service sends a request to the destination URL to establish a connection. If a connection has not been established within that time range, the
Throws a ConnectionTimeout exception. When testing, change the URL to a nonexistent URL: http://test.com After the timeout period of 3000ms, the system reported an exception:
Org.apache.commons.httpclient.ConnectTimeoutException:The
Host did not accept the connection within timeout of
3. Waiting for response timeout (read data timeout) sockettimeout
After connecting to the previous URL, get response's return wait time, that is, after establishing a connection to the destination URL, wait for the maximum time to put back the response, and throw sockettimeout if the response is not returned within the specified time.
Resources
http://blog.csdn.net/u011191463/article/details/78664896
HttpClient Library Settings Timeout