Search in sources :

Example 1 with RequestTargetHost

use of org.apache.hc.core5.http.protocol.RequestTargetHost in project californium by eclipse.

the class HttpClientFactory method createClient.

/**
 * Create the pooled asynchronous http client.
 *
 * @param config configuration for the http client
 * @return asynchronous http client
 * @since 3.0 (changed parameter to Configuration)
 */
public static CloseableHttpAsyncClient createClient(Configuration config) {
    int connectionIdleSecs = config.getTimeAsInt(Proxy2Config.HTTP_CONNECTION_IDLE_TIMEOUT, TimeUnit.SECONDS);
    final CloseableHttpAsyncClient client = HttpAsyncClientBuilder.create().disableCookieManagement().setDefaultRequestConfig(createCustomRequestConfig(config)).setConnectionManager(createPoolingConnManager(config)).setVersionPolicy(HttpVersionPolicy.NEGOTIATE).setIOReactorConfig(IOReactorConfig.custom().setSoTimeout(Timeout.ofSeconds(connectionIdleSecs)).build()).addRequestInterceptorFirst(new RequestConnControl()).addRequestInterceptorFirst(new RequestDate()).addRequestInterceptorFirst(new RequestExpectContinue()).addRequestInterceptorFirst(new RequestTargetHost()).addRequestInterceptorFirst(new RequestUserAgent()).setKeepAliveStrategy(new DefaultConnectionKeepAliveStrategy() {

        @Override
        public TimeValue getKeepAliveDuration(HttpResponse response, HttpContext context) {
            TimeValue keepAlive = super.getKeepAliveDuration(response, context);
            if (keepAlive == null || keepAlive.getDuration() < 0) {
                // Keep connections alive if a keep-alive value
                // has not be explicitly set by the server
                keepAlive = KEEP_ALIVE;
            }
            return keepAlive;
        }
    }).build();
    client.start();
    return client;
}
Also used : RequestConnControl(org.apache.hc.core5.http.protocol.RequestConnControl) RequestDate(org.apache.hc.core5.http.protocol.RequestDate) RequestTargetHost(org.apache.hc.core5.http.protocol.RequestTargetHost) RequestExpectContinue(org.apache.hc.core5.http.protocol.RequestExpectContinue) DefaultConnectionKeepAliveStrategy(org.apache.hc.client5.http.impl.DefaultConnectionKeepAliveStrategy) RequestUserAgent(org.apache.hc.core5.http.protocol.RequestUserAgent) HttpContext(org.apache.hc.core5.http.protocol.HttpContext) CloseableHttpAsyncClient(org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient) HttpResponse(org.apache.hc.core5.http.HttpResponse) TimeValue(org.apache.hc.core5.util.TimeValue)

Aggregations

DefaultConnectionKeepAliveStrategy (org.apache.hc.client5.http.impl.DefaultConnectionKeepAliveStrategy)1 CloseableHttpAsyncClient (org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient)1 HttpResponse (org.apache.hc.core5.http.HttpResponse)1 HttpContext (org.apache.hc.core5.http.protocol.HttpContext)1 RequestConnControl (org.apache.hc.core5.http.protocol.RequestConnControl)1 RequestDate (org.apache.hc.core5.http.protocol.RequestDate)1 RequestExpectContinue (org.apache.hc.core5.http.protocol.RequestExpectContinue)1 RequestTargetHost (org.apache.hc.core5.http.protocol.RequestTargetHost)1 RequestUserAgent (org.apache.hc.core5.http.protocol.RequestUserAgent)1 TimeValue (org.apache.hc.core5.util.TimeValue)1