java-commons-HttpClient超时设置setConnectionTimeout和setSoTimeout
問(wèn)題
之前使用httpclient請(qǐng)求數(shù)據(jù)
源碼方法:
public static String doHttp(HttpMethod result, int timeout, String charset) {HttpClient client = new HttpClient();try {HttpConnectionManagerParams managerParams = client.getHttpConnectionManager().getParams();managerParams.setConnectionTimeout(timeout);client.executeMethod(result);InputStream resStream = result.getResponseBodyAsStream();BufferedReader br = new BufferedReader(new InputStreamReader(resStream, charset));StringBuffer resBuffer = new StringBuffer();String resTemp = "";while ((resTemp = br.readLine()) != null) {resBuffer.append(resTemp);}return resBuffer.toString();} catch (HttpException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} catch (Exception e) {e.printStackTrace();} finally {result.releaseConnection();}return null;}?
發(fā)現(xiàn)其中在
client.executeMethod(result);?
? 一只會(huì)卡在這里不出來(lái),究其原因在于對(duì)
managerParams.setConnectionTimeout(timeout);//為連接超時(shí)?
這里使用的是連接超時(shí),而我這里還缺少了一個(gè)數(shù)據(jù)超時(shí),否則會(huì)一直等待數(shù)據(jù)返回所以在下面在添加請(qǐng)求數(shù)據(jù)超時(shí)代碼。
以下為完整的代碼:(其中紅色部分為這次的主角)
public static String doHttp(HttpMethod result, int timeout, String charset) {HttpClient client = new HttpClient();try {HttpConnectionManagerParams managerParams = client.getHttpConnectionManager().getParams();managerParams.setConnectionTimeout(timeout);managerParams.setSoTimeout(timeout);//等待結(jié)果超時(shí) client.executeMethod(result);InputStream resStream = result.getResponseBodyAsStream();BufferedReader br = new BufferedReader(new InputStreamReader(resStream, charset));StringBuffer resBuffer = new StringBuffer();String resTemp = "";while ((resTemp = br.readLine()) != null) {resBuffer.append(resTemp);}return resBuffer.toString();} catch (HttpException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} catch (Exception e) {e.printStackTrace();} finally {result.releaseConnection();}return null;}?
?
?
看介紹
?本文介紹來(lái)自http://jinnianshilongnian.iteye.com/blog/2089792
參數(shù)設(shè)置 1、httpclient 4.2.3 HttpParams params = new BasicHttpParams(); //設(shè)置連接超時(shí)時(shí)間 Integer CONNECTION_TIMEOUT = 2 * 1000; //設(shè)置請(qǐng)求超時(shí)2秒鐘 根據(jù)業(yè)務(wù)調(diào)整 Integer SO_TIMEOUT = 2 * 1000; //設(shè)置等待數(shù)據(jù)超時(shí)時(shí)間2秒鐘 根據(jù)業(yè)務(wù)調(diào)整 //定義了當(dāng)從ClientConnectionManager中檢索ManagedClientConnection實(shí)例時(shí)使用的毫秒級(jí)的超時(shí)時(shí)間 //這個(gè)參數(shù)期望得到一個(gè)java.lang.Long類(lèi)型的值。如果這個(gè)參數(shù)沒(méi)有被設(shè)置,默認(rèn)等于CONNECTION_TIMEOUT,因此一定要設(shè)置 Long CONN_MANAGER_TIMEOUT = 500L; //該值就是連接不夠用的時(shí)候等待超時(shí)時(shí)間,一定要設(shè)置,而且不能太大 () params.setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, CONNECTION_TIMEOUT); params.setIntParameter(CoreConnectionPNames.SO_TIMEOUT, SO_TIMEOUT); params.setLongParameter(ClientPNames.CONN_MANAGER_TIMEOUT, CONN_MANAGER_TIMEOUT); //在提交請(qǐng)求之前 測(cè)試連接是否可用 params.setBooleanParameter(CoreConnectionPNames.STALE_CONNECTION_CHECK, true);PoolingClientConnectionManager conMgr = new PoolingClientConnectionManager(); conMgr.setMaxTotal(200); //設(shè)置整個(gè)連接池最大連接數(shù) 根據(jù)自己的場(chǎng)景決定 //是路由的默認(rèn)最大連接(該值默認(rèn)為2),限制數(shù)量實(shí)際使用DefaultMaxPerRoute并非MaxTotal。 //設(shè)置過(guò)小無(wú)法支持大并發(fā)(ConnectionPoolTimeoutException: Timeout waiting for connection from pool),路由是對(duì)maxTotal的細(xì)分。 conMgr.setDefaultMaxPerRoute(conMgr.getMaxTotal());//(目前只有一個(gè)路由,因此讓他等于最大值)//另外設(shè)置http client的重試次數(shù),默認(rèn)是3次;當(dāng)前是禁用掉(如果項(xiàng)目量不到,這個(gè)默認(rèn)即可) httpClient.setHttpRequestRetryHandler(new DefaultHttpRequestRetryHandler(0, false));?此處解釋下MaxtTotal和DefaultMaxPerRoute的區(qū)別:
1、MaxtTotal是整個(gè)池子的大小; 2、DefaultMaxPerRoute是根據(jù)連接到的主機(jī)對(duì)MaxTotal的一個(gè)細(xì)分;比如: MaxtTotal=400 DefaultMaxPerRoute=200 而我只連接到http://xx.com時(shí),到這個(gè)主機(jī)的并發(fā)最多只有200;而不是400; 而我連接到 http://xx.com和 http://xxx.com時(shí),到每個(gè)主機(jī)的并發(fā)最多只有200;即加起來(lái)是400(但不能超過(guò)400);所以起作用的設(shè)置是DefaultMaxPerRoute。 2、httpclient 3.1 HttpConnectionManagerParams params = new HttpConnectionManagerParams(); params.setConnectionTimeout(2000); params.setSoTimeout(2000); // 最大連接數(shù) params.setMaxTotalConnections(500); params.setDefaultMaxConnectionsPerHost(500); params.setStaleCheckingEnabled(true); connectionManager.setParams(params);HttpClientParams httpClientParams = new HttpClientParams(); // 設(shè)置httpClient的連接超時(shí),對(duì)連接管理器設(shè)置的連接超時(shí)是無(wú)用的 httpClientParams.setConnectionManagerTimeout(5000); //等價(jià)于4.2.3中的CONN_MANAGER_TIMEOUT httpClient = new HttpClient(connectionManager); httpClient.setParams(httpClientParams);//另外設(shè)置http client的重試次數(shù),默認(rèn)是3次;當(dāng)前是禁用掉(如果項(xiàng)目量不到,這個(gè)默認(rèn)即可) httpClientParams.setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(0, false));?參數(shù)類(lèi)似 就不多解釋了;
?
?
?
?
?
?另看一片轉(zhuǎn)載內(nèi)容
?
HttpClient 4 和 HttpClient 3 設(shè)置超時(shí)
?
?
HttpClient 4:
連接超時(shí):
?
// 或者
HttpConnectionParams.setConnectionTimeout(params, 6000);?
讀取超時(shí):
?
// 或者
HttpConnectionParams.setSoTimeout(params, 60000);?
HttpClient 3:
連接超時(shí):
?
讀取超時(shí):
httpClient.getHttpConnectionManager().getParams().setSoTimeout(60000);?
轉(zhuǎn)載于:https://www.cnblogs.com/hwaggLee/p/5210889.html
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來(lái)咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)總結(jié)
以上是生活随笔為你收集整理的java-commons-HttpClient超时设置setConnectionTimeout和setSoTimeout的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: iPad充不进去电怎么回事
- 下一篇: YTU 2412: 帮警长数一数【循环、