使用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: "Courier New" !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";
}
參考: