HttpClient設(shè)置連接超時(shí)時(shí)間

使用HttpClient,一般都需要設(shè)置連接超時(shí)時(shí)間和獲取數(shù)據(jù)超時(shí)時(shí)間。這兩個(gè)參數(shù)很重要,目的是為了防止訪問其他http時(shí),由于超時(shí)導(dǎo)致自己的應(yīng)用受影響。

4.5版本中,這兩個(gè)參數(shù)的設(shè)置都抽象到了RequestConfig中,由相應(yīng)的Builder構(gòu)建,具體的例子如下:

CloseableHttpClient httpclient = HttpClients.createDefault();
HttpGet httpGet = new HttpGet("http://www.baidu.com");
RequestConfig requestConfig = RequestConfig.custom()
        .setConnectTimeout(5000).setConnectionRequestTimeout(1000)
        .setSocketTimeout(5000).build();
httpGet.setConfig(requestConfig);
CloseableHttpResponse response = null; try {
    response = httpclient.execute(httpGet);
} catch (IOException e) {
    e.printStackTrace();
}
System.out.println("得到的結(jié)果:" + response.getStatusLine());//得到請(qǐng)求結(jié)果
HttpEntity entity = response.getEntity();//得到請(qǐng)求回來的數(shù)據(jù)
String s = EntityUtils.toString(response.getEntity(), "UTF-8");
System.out.println(s);

setConnectTimeout:設(shè)置連接超時(shí)時(shí)間,單位毫秒。

setConnectionRequestTimeout:設(shè)置從connect Manager獲取Connection 超時(shí)時(shí)間,單位毫秒。這個(gè)屬性是新加的屬性,因?yàn)槟壳鞍姹臼强梢怨蚕磉B接池的。

setSocketTimeout:請(qǐng)求獲取數(shù)據(jù)的超時(shí)時(shí)間,單位毫秒。 如果訪問一個(gè)接口,多少時(shí)間內(nèi)無法返回?cái)?shù)據(jù),就直接放棄此次調(diào)用。

==============================================================================

一、連接超時(shí):connectionTimeout

指的是連接一個(gè)url的連接等待時(shí)間。比如連google.

org.apache.http.conn.ConnectTimeoutException: Connect to www.google.com:80 [www.google.com/203.98.7.65] failed: connect timed out
    at org.apache.http.impl.conn.HttpClientConnectionOperator.connect(HttpClientConnectionOperator.java:132)
    at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:318)
    at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:363)
    at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:219)
    at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:195)
    at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:86)
    at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:108)
    at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:184)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:106)
    at Test.main(Test.java:22)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
Caused by: java.net.SocketTimeoutException: connect timed out
    at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:85)
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
    at java.net.Socket.connect(Socket.java:589)
    at org.apache.http.conn.socket.PlainConnectionSocketFactory.connectSocket(PlainConnectionSocketFactory.java:72)
    at org.apache.http.impl.conn.HttpClientConnectionOperator.connect(HttpClientConnectionOperator.java:123)

二、讀取數(shù)據(jù)超時(shí):SocketTimeout

指的是連接上一個(gè)url,獲取response的返回等待時(shí)間。

測(cè)試的時(shí)候連接url可以是本地開啟的一個(gè)url,http://localhost:8080/firstTest.htm?method=test

當(dāng)訪問到這個(gè)鏈接時(shí),線程sleep一段時(shí)間,來模擬返回response超時(shí)。

<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; overflow-wrap: break-word; font-family: &quot;Courier New&quot; !important; font-size: 12px !important;">@RequestMapping(params = "method=test") public String testMethod(ModelMap model) { try {
        Thread.sleep(3000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    System.out.println("call testMethod method.");
    model.addAttribute("name", "test method"); return "test";
}

參考:

http://blog.csdn.net/hi_kevin/article/details/32316171

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容