大家好,又见面了,我是你们的朋友全栈君。
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();
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/163246.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...