大家好,欢迎来到IT知识分享网。
httpComponents HttpClient4.x
获取httpClient
CloseableHttpClient httpClient = HttpClients.custom()
.setMaxConnTotal(config.maxConnTotal)
.setMaxConnPerRoute(config.maxConnPerRoute)
.setDefaultRequestConfig(requestConfig);
或者
PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();
connectionManager.setMaxTotal(maxTotal);
connectionManager.setDefaultMaxPerRoute(defaultMaxPerRoute);
//可用空闲连接过期时间,重用空闲连接时会先检查是否空闲时间超过这个时间,如果超过,释放socket重新建
connectionManager.setValidateAfterInactivity(validateAfterInactivity);
CloseableHttpClient httpClient = HttpClients.custom()
.setConnectionManager(connectionManager)
.setRetryHandler(new DefaultHttpRequestRetryHandler())
.build();
获取httpRequestBase
RequestConfig requestConfig = RequestConfig.custom()
.setConnectionRequestTimeout(config.connReqTimeout)
.setConnectTimeout(config.connTimeout)
.setSocketTimeout(config.socketTimeout)
.build();
HttpRequestBase httpRequestBase = new HttpGet/HttpPost/HttpDelete(url);
httpRequestBase.setConfig(requestConfig);
httpRequestBase.setxxx();
调用
CloseableHttpResponse response = httpClient.execute(httpRequestBase);
关闭
response.close();
HttpClient HttpClient3.x
获取HttpClient
MultiThreadedHttpConnectionManager httpConnectionManager = new MultiThreadedHttpConnectionManager();
httpConnectionManager.getParams().setDefaultMaxConnectionsPerHost(maxConnectionsPerHost);
httpConnectionManager.getParams().setMaxTotalConnections(maxTotalConnections);
//4.x版本提供connectionManager.setValidateAfterInactivity(validateAfterInactivity);
httpConnectionManager.getParams().setStaleCheckingEnabled(false/true);
httpConnectionManager.getParams().setLinger(linger);
httpConnectionManager.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler());
httpConnectionManager.getParams().setConnectionTimeout(connectionTimeout);
httpConnectionManager.getParams().setSoTimeout(soTimeout);
HttpClient httpClient = new HttpClient(httpConnectionManager);
获取method
EntityEnclosingMethod method = new PostMethod(url);
method.setxxx();
调用
client.executeMethod(method);
关闭
method.releaseConnection();
免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://yundeesoft.com/25706.html