HttpClient的超時用法小記
博客分類: javaHttpClient在使用中有兩個超時時間,是一直接觸和使用的,由於上次工作中使用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();
- }
- }
/**
* @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)
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();
- }
- }
/**
* @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";
- }
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.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)
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這兩個超時時間一定要加上,不加就很可能悲劇的了。