Class HttpClientBuilder

  • Direct Known Subclasses:
    CachingHttpClientBuilder

    public class HttpClientBuilder
    extends java.lang.Object
    Builder for CloseableHttpClient instances.

    When a particular component is not explicitly set this class will use its default implementation. System properties will be taken into account when configuring the default implementations when useSystemProperties() method is called prior to calling build().

    • http.proxyHost
    • http.proxyPort
    • https.proxyHost
    • https.proxyPort
    • http.nonProxyHosts
    • https.proxyUser
    • http.proxyUser
    • https.proxyPassword
    • http.proxyPassword
    • http.keepAlive
    • http.agent

    Please note that some settings used by this class can be mutually exclusive and may not apply when building CloseableHttpClient instances.

    Since:
    4.3
    • Field Detail

      • requestExec

        private org.apache.hc.core5.http.impl.io.HttpRequestExecutor requestExec
      • connManagerShared

        private boolean connManagerShared
      • reuseStrategy

        private org.apache.hc.core5.http.ConnectionReuseStrategy reuseStrategy
      • authSchemeRegistry

        private org.apache.hc.core5.http.config.Lookup<AuthSchemeFactory> authSchemeRegistry
      • cookieSpecRegistry

        private org.apache.hc.core5.http.config.Lookup<CookieSpecFactory> cookieSpecRegistry
      • contentDecoderMap

        private java.util.LinkedHashMap<java.lang.String,​InputStreamFactory> contentDecoderMap
      • userAgent

        private java.lang.String userAgent
      • proxy

        private org.apache.hc.core5.http.HttpHost proxy
      • defaultHeaders

        private java.util.Collection<? extends org.apache.hc.core5.http.Header> defaultHeaders
      • defaultRequestConfig

        private RequestConfig defaultRequestConfig
      • evictExpiredConnections

        private boolean evictExpiredConnections
      • evictIdleConnections

        private boolean evictIdleConnections
      • maxIdleTime

        private org.apache.hc.core5.util.TimeValue maxIdleTime
      • systemProperties

        private boolean systemProperties
      • redirectHandlingDisabled

        private boolean redirectHandlingDisabled
      • automaticRetriesDisabled

        private boolean automaticRetriesDisabled
      • contentCompressionDisabled

        private boolean contentCompressionDisabled
      • cookieManagementDisabled

        private boolean cookieManagementDisabled
      • authCachingDisabled

        private boolean authCachingDisabled
      • connectionStateDisabled

        private boolean connectionStateDisabled
      • defaultUserAgentDisabled

        private boolean defaultUserAgentDisabled
      • proxySelector

        private java.net.ProxySelector proxySelector
      • closeables

        private java.util.List<java.io.Closeable> closeables
    • Constructor Detail

      • HttpClientBuilder

        protected HttpClientBuilder()
    • Method Detail

      • setRequestExecutor

        public final HttpClientBuilder setRequestExecutor​(org.apache.hc.core5.http.impl.io.HttpRequestExecutor requestExec)
        Assigns HttpRequestExecutor instance.
      • setConnectionManagerShared

        public final HttpClientBuilder setConnectionManagerShared​(boolean shared)
        Defines the connection manager is to be shared by multiple client instances.

        If the connection manager is shared its life-cycle is expected to be managed by the caller and it will not be shut down if the client is closed.

        Parameters:
        shared - defines whether or not the connection manager can be shared by multiple clients.
        Since:
        4.4
      • setConnectionReuseStrategy

        public final HttpClientBuilder setConnectionReuseStrategy​(org.apache.hc.core5.http.ConnectionReuseStrategy reuseStrategy)
        Assigns ConnectionReuseStrategy instance.
      • disableConnectionState

        public final HttpClientBuilder disableConnectionState()
        Disables connection state tracking.
      • setUserAgent

        public final HttpClientBuilder setUserAgent​(java.lang.String userAgent)
        Assigns User-Agent value.
      • setDefaultHeaders

        public final HttpClientBuilder setDefaultHeaders​(java.util.Collection<? extends org.apache.hc.core5.http.Header> defaultHeaders)
        Assigns default request header values.
      • addResponseInterceptorFirst

        public final HttpClientBuilder addResponseInterceptorFirst​(org.apache.hc.core5.http.HttpResponseInterceptor interceptor)
        Adds this protocol interceptor to the head of the protocol processing list.
      • addResponseInterceptorLast

        public final HttpClientBuilder addResponseInterceptorLast​(org.apache.hc.core5.http.HttpResponseInterceptor interceptor)
        Adds this protocol interceptor to the tail of the protocol processing list.
      • addRequestInterceptorFirst

        public final HttpClientBuilder addRequestInterceptorFirst​(org.apache.hc.core5.http.HttpRequestInterceptor interceptor)
        Adds this protocol interceptor to the head of the protocol processing list.
      • addRequestInterceptorLast

        public final HttpClientBuilder addRequestInterceptorLast​(org.apache.hc.core5.http.HttpRequestInterceptor interceptor)
        Adds this protocol interceptor to the tail of the protocol processing list.
      • addExecInterceptorBefore

        public final HttpClientBuilder addExecInterceptorBefore​(java.lang.String existing,
                                                                java.lang.String name,
                                                                ExecChainHandler interceptor)
        Adds this execution interceptor before an existing interceptor.
      • addExecInterceptorAfter

        public final HttpClientBuilder addExecInterceptorAfter​(java.lang.String existing,
                                                               java.lang.String name,
                                                               ExecChainHandler interceptor)
        Adds this execution interceptor after interceptor with the given name.
      • replaceExecInterceptor

        public final HttpClientBuilder replaceExecInterceptor​(java.lang.String existing,
                                                              ExecChainHandler interceptor)
        Replace an existing interceptor with the given name with new interceptor.
      • addExecInterceptorFirst

        public final HttpClientBuilder addExecInterceptorFirst​(java.lang.String name,
                                                               ExecChainHandler interceptor)
        Add an interceptor to the head of the processing list.
      • addExecInterceptorLast

        public final HttpClientBuilder addExecInterceptorLast​(java.lang.String name,
                                                              ExecChainHandler interceptor)
        Add an interceptor to the tail of the processing list.
      • disableCookieManagement

        public final HttpClientBuilder disableCookieManagement()
        Disables state (cookie) management.
      • disableContentCompression

        public final HttpClientBuilder disableContentCompression()
        Disables automatic content decompression.
      • disableAuthCaching

        public final HttpClientBuilder disableAuthCaching()
        Disables authentication scheme caching.
      • disableAutomaticRetries

        public final HttpClientBuilder disableAutomaticRetries()
        Disables automatic request recovery and re-execution.
      • disableRedirectHandling

        public final HttpClientBuilder disableRedirectHandling()
        Disables automatic redirect handling.
      • setDefaultCookieStore

        public final HttpClientBuilder setDefaultCookieStore​(CookieStore cookieStore)
        Assigns default CookieStore instance which will be used for request execution if not explicitly set in the client execution context.
      • setDefaultCredentialsProvider

        public final HttpClientBuilder setDefaultCredentialsProvider​(CredentialsProvider credentialsProvider)
        Assigns default CredentialsProvider instance which will be used for request execution if not explicitly set in the client execution context.
      • setDefaultAuthSchemeRegistry

        public final HttpClientBuilder setDefaultAuthSchemeRegistry​(org.apache.hc.core5.http.config.Lookup<AuthSchemeFactory> authSchemeRegistry)
        Assigns default AuthScheme registry which will be used for request execution if not explicitly set in the client execution context.
      • setDefaultCookieSpecRegistry

        public final HttpClientBuilder setDefaultCookieSpecRegistry​(org.apache.hc.core5.http.config.Lookup<CookieSpecFactory> cookieSpecRegistry)
        Assigns default CookieSpec registry which will be used for request execution if not explicitly set in the client execution context.
        See Also:
        CookieSpecSupport
      • setDefaultRequestConfig

        public final HttpClientBuilder setDefaultRequestConfig​(RequestConfig config)
        Assigns default RequestConfig instance which will be used for request execution if not explicitly set in the client execution context.
      • useSystemProperties

        public final HttpClientBuilder useSystemProperties()
        Use system properties when creating and configuring default implementations.
      • evictExpiredConnections

        public final HttpClientBuilder evictExpiredConnections()
        Makes this instance of HttpClient proactively evict expired connections from the connection pool using a background thread.

        One MUST explicitly close HttpClient with Closeable.close() in order to stop and release the background thread.

        Please note this method has no effect if the instance of HttpClient is configured to use a shared connection manager.

        Since:
        4.4
        See Also:
        setConnectionManagerShared(boolean), ConnPoolControl.closeExpired()
      • evictIdleConnections

        public final HttpClientBuilder evictIdleConnections​(org.apache.hc.core5.util.TimeValue maxIdleTime)
        Makes this instance of HttpClient proactively evict idle connections from the connection pool using a background thread.

        One MUST explicitly close HttpClient with Closeable.close() in order to stop and release the background thread.

        Please note this method has no effect if the instance of HttpClient is configured to use a shared connection manager.

        Parameters:
        maxIdleTime - maximum time persistent connections can stay idle while kept alive in the connection pool. Connections whose inactivity period exceeds this value will get closed and evicted from the pool.
        Since:
        4.4
        See Also:
        setConnectionManagerShared(boolean), ConnPoolControl.closeIdle(TimeValue)
      • disableDefaultUserAgent

        public final HttpClientBuilder disableDefaultUserAgent()
        Disables the default user agent set by this builder if none has been provided by the user.
        Since:
        4.5.7
      • setProxySelector

        public final HttpClientBuilder setProxySelector​(java.net.ProxySelector proxySelector)
        Sets the ProxySelector that will be used to select the proxies to be used for establishing HTTP connections. If a non-null proxy selector is set, it will take precedence over the proxy settings configured in the client.
        Parameters:
        proxySelector - the ProxySelector to be used, or null to use the default system proxy selector.
        Returns:
        this HttpClientBuilder instance, to allow for method chaining.
      • customizeExecChain

        @Internal
        protected void customizeExecChain​(org.apache.hc.core5.http.config.NamedElementChain<ExecChainHandler> execChainDefinition)
        Request exec chain customization and extension.

        For internal use.

      • addCloseable

        @Internal
        protected void addCloseable​(java.io.Closeable closeable)
        Adds to the list of Closeable resources to be managed by the client.

        For internal use.