Java中httpClient中的三種逾時設定小結

來源:互聯網
上載者:User

ConnectTimeoutException:
    當串連HTTP伺服器或者等待HttpConnectionManager管理的一個有效連線逾時引發該異常。
 
SocketTimeoutException:
    當讀取或者接收Socket逾時引起該異常。
 
在Apache的HttpClient包中,有三個設定逾時的地方:

 代碼如下 複製代碼

/* 從串連池中取串連的逾時時間*/
ConnManagerParams.setTimeout(params, 1000);
/*連線逾時*/
HttpConnectionParams.setConnectionTimeout(params, 2000);
/*請求逾時*/
HttpConnectionParams.setSoTimeout(params, 4000);

 
第一行設定ConnectionPoolTimeout:


這定義了從ConnectionManager管理的串連池中取出串連的逾時時間,此處設定為1秒。

第二行設定ConnectionTimeout:  


這定義了通過網路與伺服器建立串連的逾時時間。Httpclient包中通過一個非同步線程去建立與伺服器的socket串連,這就是該socket串連的逾時時間,此處設定為2秒。


第三行設定SocketTimeout:    

這定義了Socket讀資料的逾時時間,即從伺服器擷取響應資料需要等待的時間,此處設定為4秒。

以上3種逾時分別會拋出ConnectionPoolTimeoutException,ConnectionTimeoutException與SocketTimeoutException。

測試版本為HttpClient——3.1

一:連線逾時:connectionTimeout

1:指的是串連一個url的串連等待時間。

2:設定方法為:

 代碼如下 複製代碼

public class TestHttpClientMain {

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://www.111cn.net"

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 {

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.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)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.