HttpClient在运用中有两个超时时刻,是一向接触和运用的,由于前次工作中运用httpClient造成了体系悲惨剧的情况,特别对它的两个超时时刻进行了小小的测验,在这里记录下。
测验版别为HttpClient——3.1
一:衔接超时:connectionTimeout
1:指的是衔接一个url的衔接等待时刻。
2:设置办法为:
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(3000);
client.getHttpConnectionManager().getParams().setSoTimeout(3000);
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:测验的时分,将url改为一个不存在的url:“http://test.com”
4:超时时刻3000ms往后,体系报出异常。
org.apache.commons.httpclient.ConnectTimeoutException: The host did not accept the connection within timeout of 3000 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)
二:读取数据超时:soTimeout
1:指的是衔接上一个url,获取response的回来等待时刻
2:设置办法
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(3000);
client.getHttpConnectionManager().getParams().setSoTimeout(2000);
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:测验的时分的衔接url为我本地敞开的一个url,http://localhost:8080/firstTest.htm?method=test
在我这个测验url里,当访问到这个连接时,线程sleep一段时刻,来模仿回来response超时。
@RequestMapping(params = "method=test") //<——②
public String testMethod(ModelMap model) {
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("call testMethod method.");
model.addAttribute("name", "test method");
return "test";
}
4:将读取response回来超时时刻设的时刻比那个sleep时刻短之后,运行程序给出异常:
java.net.SocketTimeoutException: Read timed out
at java.net.SocketInputStream.socket("http://www.fanselang.com","plus");
at java.net.SocketInputStream.socket("http://www.haomad.com","plus");
at java.net.SocketInputStream.socket("http://www.metabase.cn","plus");
at java.net.SocketInputStream.socket("http://www.3h5.cn","plus");
at java.net.SocketInputStream.socket("http://www.4lunwen.cn","plus");
at java.net.SocketInputStream.socket("http://www.zx1234.cn","plus");
at java.net.SocketInputStream.socket("http://www.majiangji168.cn","plus");
at java.net.SocketInputStream.socket("http://www.penbar.cn","plus");
at java.net.SocketInputStream.socket("http://www.whathappy.cn","plus");
at java.net.SocketInputStream.socket("http://www.lunjin.net","plus");
at java.net.SocketInputStream.socket("http://www.ssstyle.cn","plus");
at java.net.SocketInputStream.socket("http://www.91fish.cn","plus");
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)
ok,今后再写httpClient这两个超时时刻一定要加上,不加就很能够悲惨剧的了。
HttpClient超时的用法,布布扣,bubuko.com