日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java-commons-HttpClient超时设置setConnectionTimeout和setSoTimeout

發布時間:2023/12/19 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java-commons-HttpClient超时设置setConnectionTimeout和setSoTimeout 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

問題

之前使用httpclient請求數據

源碼方法:

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;}

?

發現其中在

client.executeMethod(result);

?

? 一只會卡在這里不出來,究其原因在于對

managerParams.setConnectionTimeout(timeout);//為連接超時

?

這里使用的是連接超時,而我這里還缺少了一個數據超時,否則會一直等待數據返回所以在下面在添加請求數據超時代碼。

以下為完整的代碼:(其中紅色部分為這次的主角)

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);//等待結果超時 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;}

?

?

?

看介紹

?本文介紹來自http://jinnianshilongnian.iteye.com/blog/2089792

參數設置 1、httpclient 4.2.3 HttpParams params = new BasicHttpParams(); //設置連接超時時間 Integer CONNECTION_TIMEOUT = 2 * 1000; //設置請求超時2秒鐘 根據業務調整 Integer SO_TIMEOUT = 2 * 1000; //設置等待數據超時時間2秒鐘 根據業務調整 //定義了當從ClientConnectionManager中檢索ManagedClientConnection實例時使用的毫秒級的超時時間 //這個參數期望得到一個java.lang.Long類型的值。如果這個參數沒有被設置,默認等于CONNECTION_TIMEOUT,因此一定要設置 Long CONN_MANAGER_TIMEOUT = 500L; //該值就是連接不夠用的時候等待超時時間,一定要設置,而且不能太大 () params.setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, CONNECTION_TIMEOUT); params.setIntParameter(CoreConnectionPNames.SO_TIMEOUT, SO_TIMEOUT); params.setLongParameter(ClientPNames.CONN_MANAGER_TIMEOUT, CONN_MANAGER_TIMEOUT); //在提交請求之前 測試連接是否可用 params.setBooleanParameter(CoreConnectionPNames.STALE_CONNECTION_CHECK, true);PoolingClientConnectionManager conMgr = new PoolingClientConnectionManager(); conMgr.setMaxTotal(200); //設置整個連接池最大連接數 根據自己的場景決定 //是路由的默認最大連接(該值默認為2),限制數量實際使用DefaultMaxPerRoute并非MaxTotal。 //設置過小無法支持大并發(ConnectionPoolTimeoutException: Timeout waiting for connection from pool),路由是對maxTotal的細分。 conMgr.setDefaultMaxPerRoute(conMgr.getMaxTotal());//(目前只有一個路由,因此讓他等于最大值)//另外設置http client的重試次數,默認是3次;當前是禁用掉(如果項目量不到,這個默認即可) httpClient.setHttpRequestRetryHandler(new DefaultHttpRequestRetryHandler(0, false));

?此處解釋下MaxtTotal和DefaultMaxPerRoute的區別:

1、MaxtTotal是整個池子的大小; 2、DefaultMaxPerRoute是根據連接到的主機對MaxTotal的一個細分;比如: MaxtTotal=400 DefaultMaxPerRoute=200 而我只連接到http://xx.com時,到這個主機的并發最多只有200;而不是400; 而我連接到 http://xx.com和 http://xxx.com時,到每個主機的并發最多只有200;即加起來是400(但不能超過400);所以起作用的設置是DefaultMaxPerRoute。 2、httpclient 3.1 HttpConnectionManagerParams params = new HttpConnectionManagerParams(); params.setConnectionTimeout(2000); params.setSoTimeout(2000); // 最大連接數 params.setMaxTotalConnections(500); params.setDefaultMaxConnectionsPerHost(500); params.setStaleCheckingEnabled(true); connectionManager.setParams(params);HttpClientParams httpClientParams = new HttpClientParams(); // 設置httpClient的連接超時,對連接管理器設置的連接超時是無用的 httpClientParams.setConnectionManagerTimeout(5000); //等價于4.2.3中的CONN_MANAGER_TIMEOUT httpClient = new HttpClient(connectionManager); httpClient.setParams(httpClientParams);//另外設置http client的重試次數,默認是3次;當前是禁用掉(如果項目量不到,這個默認即可) httpClientParams.setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(0, false));

?參數類似 就不多解釋了;

?

?

?

?

?

?另看一片轉載內容

?

HttpClient 4 和 HttpClient 3 設置超時

?

?


HttpClient 4:
連接超時:

httpclient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT,60000);

?

// 或者

HttpConnectionParams.setConnectionTimeout(params, 6000);

?


讀取超時:

httpclient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT,60000);

?

// 或者

HttpConnectionParams.setSoTimeout(params, 60000);

?


HttpClient 3:
連接超時:

httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(60000);

?

讀取超時:

httpClient.getHttpConnectionManager().getParams().setSoTimeout(60000);

?

轉載于:https://www.cnblogs.com/hwaggLee/p/5210889.html

創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的java-commons-HttpClient超时设置setConnectionTimeout和setSoTimeout的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。