问题html
以前使用httpclient请求数据java
源码方法:并发
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; }
发现其中在app
client.executeMethod(result);
一只会卡在这里不出来,究其缘由在于对post
managerParams.setConnectionTimeout(timeout);//为链接超时
这里使用的是链接超时,而我这里还缺乏了一个数据超时,不然会一直等待数据返回因此在下面在添加请求数据超时代码。测试
如下为完整的代码:(其中红色部分为此次的主角)spa
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; }
看介绍code
本文介绍来自http://jinnianshilongnian.iteye.com/blog/2089792htm
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的区别:blog
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);