问题
之前使用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
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的区别:
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);