Respect blogger original, special post blog link. Copy down afraid later link invalid or deleted.
Transferred from: http://blog.csdn.net/hi_kevin/article/details/32316171
HttpClient in the use of two time-out, is always in touch and use, due to the last work of the use of httpclient caused the system tragedy, specifically to its two time-out of a small test, recorded here.
Beta version is httpclient--3.1
One: Connection timeout: ConnectionTimeout
1: Refers to the connection waiting time for connecting a URL.
2: Set the method to:
public class Testhttpclientmain {
/** * @param args */public static void Main (string[] args) { HttpClient client = new HttpClient (); HttpMethod method = new GetMethod ( "http://test.com"); Client.gethttpconnectionmanager (). Getparams () . Setconnectiontimeout (+); Client.gethttpconnectionmanager (). Getparams (). Setsotimeout (+); try { int statusCode = Client.executemethod (method); System.out.println (StatusCode); byte[] responsebody = null; Responsebody = Method.getresponsebody (); string result = new String (responsebody); SYSTEM.OUT.PRINTLN (result); } catch (HttpException e) { //TODO auto-generated catch block e.printstacktrace (); } catch (IOException e) { //TODO auto-generated catch block e.printstacktrace ();}}}
3: When testing, change the URL to a nonexistent URL: "Http://test.com"
4: The system reported an exception after the timeout period of 3000ms.
Org.apache.commons.httpclient.ConnectTimeoutException:The host did not accept the connection within timeout of MS
At Org.apache.commons.httpclient.protocol.ReflectionSocketFactory.createSocket (reflectionsocketfactory.java:155 ) at Org.apache.commons.httpclient.protocol.DefaultProtocolSocketFactory.createSocket ( defaultprotocolsocketfactory.java:125) at Org.apache.commons.httpclient.HttpConnection.open (Httpconnection.java : 707) at Org.apache.commons.httpclient.HttpMethodDirector.executeWithRetry (httpmethoddirector.java:387) at Org.apache.commons.httpclient.HttpMethodDirector.executeMethod (httpmethoddirector.java:171) at Org.apache.commons.httpclient.HttpClient.executeMethod (httpclient.java:397)
Two: Read data timeout: sotimeout
1: Refers to the connection of the previous URL, get response return wait time
2: Setup method
public class Testhttpclientmain {
/** * @param args */public static void Main (string[] args) { HttpClient client = new HttpClient (); HttpMethod method = new GetMethod ( "http://localhost:8080/firstTest.htm?method=test"); Client.gethttpconnectionmanager (). Getparams () . Setconnectiontimeout (+); Client.gethttpconnectionmanager (). Getparams (). Setsotimeout (+); try { int statusCode = Client.executemethod (method); System.out.println (StatusCode); byte[] responsebody = null; Responsebody = Method.getresponsebody (); string result = new String (responsebody); SYSTEM.OUT.PRINTLN (result); } catch (HttpException e) { //TODO auto-generated catch block e.printstacktrace (); } catch (IOException e) { //TODO auto-generated catch block e.printstacktrace ();}}}
3: The connection URL for the test is a url,http://localhost:8080/firsttest.htm?method=test that I opened locally
In my test URL, when the link is accessed, the thread sleep for a period of time to simulate the return response timeout.
@RequestMapping (params = "method=test")//<--②
Public String TestMethod (Modelmap model) { try { thread.sleep (+); } catch (Interruptedexception e) { //TODO auto-generated catch block e.printstacktrace (); } System.out.println ("Call TestMethod method."); Model.addattribute ("name", "test Method"); return "Test"; }
4: The Read response return timeout is set to a shorter time than the sleep time, and the run program gives an exception:
Java.net.SocketTimeoutException:Read timed out
At java.net.SocketInputStream.socketRead0 (Native Method) at Java.net.SocketInputStream.read (Unknown Source) at Java.io.BufferedInputStream.fill (Unknown source) at Java.io.BufferedInputStream.read (Unknown source) 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)
HttpClient 4:
Connection timeout:
httpclient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT,60000);// 或者HttpConnectionParams.setConnectionTimeout(params, 6000); |
Read timeout:
httpclient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT,60000);// 或者HttpConnectionParams.setSoTimeout(params, 60000); |
HttpClient 3:
Connection timeout:
httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(60000); |
Read timeout:
httpClient.getHttpConnectionManager().getParams().setSoTimeout(60000); |
HttpClient 4:
Connection timeout:
httpclient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT,60000);// 或者HttpConnectionParams.setConnectionTimeout(params,6000); |
Read timeout:
httpclient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT,60000);// 或者HttpConnectionParams.setSoTimeout(params,60000); |
HttpClient 3:
Connection timeout:
httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(60000); |
Read timeout:
httpClient.getHttpConnectionManager().getParams().setSoTimeout(60000); |
OK, later write httpclient these two time-outs must add, not add is likely tragic
HttpClient Timeout Time setting (RPM)