Search in sources :

Example 1 with PoolingHttpClientConnectionManager

use of org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager 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();
}
Also used : RequestConfig(org.apache.hc.client5.http.config.RequestConfig) HttpClientBuilder(org.apache.hc.client5.http.impl.classic.HttpClientBuilder) BasicHeader(org.apache.hc.core5.http.message.BasicHeader) PoolingHttpClientConnectionManager(org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager)

Example 2 with PoolingHttpClientConnectionManager

use of org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager 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();
}
Also used : RequestConfig(org.apache.hc.client5.http.config.RequestConfig) HttpClientBuilder(org.apache.hc.client5.http.impl.classic.HttpClientBuilder) BasicHeader(org.apache.hc.core5.http.message.BasicHeader) PoolingHttpClientConnectionManager(org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager)

Aggregations

RequestConfig (org.apache.hc.client5.http.config.RequestConfig)2 HttpClientBuilder (org.apache.hc.client5.http.impl.classic.HttpClientBuilder)2 PoolingHttpClientConnectionManager (org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager)2 BasicHeader (org.apache.hc.core5.http.message.BasicHeader)2