use of org.apache.hc.client5.http.impl.classic.HttpClientBuilder in project weicoder by wdcode.
the class HttpClient5 method init.
/**
* 初始化httpclient
*
* @return CloseableHttpClient
*/
private static CloseableHttpClient init() {
// Http连接池
PoolingHttpClientConnectionManager pool = new PoolingHttpClientConnectionManager();
pool.setDefaultMaxPerRoute(C.O.CPU_NUM);
pool.setMaxTotal(HttpParams.HTTP_MAX);
// 设置请求参数
RequestConfig.Builder config = RequestConfig.custom();
config.setConnectionRequestTimeout(Timeout.ofSeconds(W.C.toLong(HttpParams.HTTP_TIMEOUT)));
config.setConnectTimeout(Timeout.ofSeconds(W.C.toLong(HttpParams.HTTP_TIMEOUT)));
config.setCircularRedirectsAllowed(false);
// HttpClientBuilder
HttpClientBuilder builder = HttpClientBuilder.create();
builder.setDefaultRequestConfig(config.build());
builder.setConnectionManager(pool);
// builder.setMaxConnPerRoute(SystemConstants.CPU_NUM);
// 设置 头
List<BasicHeader> headers = Lists.newList();
headers.add(new BasicHeader(C.H.USER_AGENT_KEY, C.H.USER_AGENT_VAL));
headers.add(new BasicHeader(C.H.ACCEPT_KEY, C.H.ACCEPT_VAL));
builder.setDefaultHeaders(headers);
// 实例化客户端
return builder.build();
}
use of org.apache.hc.client5.http.impl.classic.HttpClientBuilder in project weicoder by wdcode.
the class HttpClient method init.
/**
* 初始化httpclient
*
* @return CloseableHttpClient
*/
private static CloseableHttpClient init() {
// Http连接池
PoolingHttpClientConnectionManager pool = new PoolingHttpClientConnectionManager();
pool.setDefaultMaxPerRoute(SystemConstants.CPU_NUM);
pool.setMaxTotal(HttpParams.HTTP_MAX);
// 设置请求参数
RequestConfig.Builder config = RequestConfig.custom();
config.setConnectionRequestTimeout(Timeout.ofSeconds(W.C.toLong(HttpParams.HTTP_TIMEOUT)));
config.setConnectTimeout(Timeout.ofSeconds(W.C.toLong(HttpParams.HTTP_TIMEOUT)));
config.setCircularRedirectsAllowed(false);
// HttpClientBuilder
HttpClientBuilder builder = HttpClientBuilder.create();
builder.setDefaultRequestConfig(config.build());
builder.setConnectionManager(pool);
// builder.setMaxConnPerRoute(SystemConstants.CPU_NUM);
// 设置 头
List<BasicHeader> headers = Lists.newList();
headers.add(new BasicHeader(HttpConstants.USER_AGENT_KEY, HttpConstants.USER_AGENT_VAL));
headers.add(new BasicHeader(HttpConstants.ACCEPT_KEY, HttpConstants.ACCEPT_VAL));
builder.setDefaultHeaders(headers);
// 实例化客户端
return builder.build();
}
Aggregations