問題
之前使用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);