Class HttpClientBuilder
- java.lang.Object
-
- org.apache.hc.client5.http.impl.classic.HttpClientBuilder
-
public class HttpClientBuilder extends java.lang.ObjectBuilder forCloseableHttpClientinstances.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 callingbuild().- 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
CloseableHttpClientinstances.- Since:
- 4.3
-
-
Constructor Summary
Constructors Modifier Constructor Description protectedHttpClientBuilder()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description protected voidaddCloseable(java.io.Closeable closeable)Adds to the list ofCloseableresources to be managed by the client.HttpClientBuilderaddExecInterceptorAfter(java.lang.String existing, java.lang.String name, ExecChainHandler interceptor)Adds this execution interceptor after interceptor with the given name.HttpClientBuilderaddExecInterceptorBefore(java.lang.String existing, java.lang.String name, ExecChainHandler interceptor)Adds this execution interceptor before an existing interceptor.HttpClientBuilderaddExecInterceptorFirst(java.lang.String name, ExecChainHandler interceptor)Add an interceptor to the head of the processing list.HttpClientBuilderaddExecInterceptorLast(java.lang.String name, ExecChainHandler interceptor)Add an interceptor to the tail of the processing list.HttpClientBuilderaddRequestInterceptorFirst(org.apache.hc.core5.http.HttpRequestInterceptor interceptor)Adds this protocol interceptor to the head of the protocol processing list.HttpClientBuilderaddRequestInterceptorLast(org.apache.hc.core5.http.HttpRequestInterceptor interceptor)Adds this protocol interceptor to the tail of the protocol processing list.HttpClientBuilderaddResponseInterceptorFirst(org.apache.hc.core5.http.HttpResponseInterceptor interceptor)Adds this protocol interceptor to the head of the protocol processing list.HttpClientBuilderaddResponseInterceptorLast(org.apache.hc.core5.http.HttpResponseInterceptor interceptor)Adds this protocol interceptor to the tail of the protocol processing list.CloseableHttpClientbuild()protected java.util.function.Function<org.apache.hc.core5.http.protocol.HttpContext,HttpClientContext>contextAdaptor()static HttpClientBuildercreate()protected voidcustomizeExecChain(org.apache.hc.core5.http.config.NamedElementChain<ExecChainHandler> execChainDefinition)Request exec chain customization and extension.HttpClientBuilderdisableAuthCaching()Disables authentication scheme caching.HttpClientBuilderdisableAutomaticRetries()Disables automatic request recovery and re-execution.HttpClientBuilderdisableConnectionState()Disables connection state tracking.HttpClientBuilderdisableContentCompression()Disables automatic content decompression.HttpClientBuilderdisableCookieManagement()Disables state (cookie) management.HttpClientBuilderdisableDefaultUserAgent()Disables the default user agent set by this builder if none has been provided by the user.HttpClientBuilderdisableRedirectHandling()Disables automatic redirect handling.HttpClientBuilderevictExpiredConnections()Makes this instance of HttpClient proactively evict expired connections from the connection pool using a background thread.HttpClientBuilderevictIdleConnections(org.apache.hc.core5.util.TimeValue maxIdleTime)Makes this instance of HttpClient proactively evict idle connections from the connection pool using a background thread.HttpClientConnectionManagergetConnManager()HttpClientBuilderreplaceExecInterceptor(java.lang.String existing, ExecChainHandler interceptor)Replace an existing interceptor with the given name with new interceptor.HttpClientBuildersetBackoffManager(BackoffManager backoffManager)SetsBackoffManagerinstance.HttpClientBuildersetConnectionBackoffStrategy(ConnectionBackoffStrategy connectionBackoffStrategy)SetsConnectionBackoffStrategyinstance.HttpClientBuildersetConnectionManager(HttpClientConnectionManager connManager)SetsHttpClientConnectionManagerinstance.HttpClientBuildersetConnectionManagerShared(boolean shared)Defines the connection manager is to be shared by multiple client instances.HttpClientBuildersetConnectionReuseStrategy(org.apache.hc.core5.http.ConnectionReuseStrategy reuseStrategy)SetsConnectionReuseStrategyinstance.HttpClientBuildersetContentDecoderRegistry(java.util.LinkedHashMap<java.lang.String,InputStreamFactory> contentDecoderMap)Sets a map ofInputStreamFactorys to be used for automatic content decompression.HttpClientBuildersetDefaultAuthSchemeRegistry(org.apache.hc.core5.http.config.Lookup<AuthSchemeFactory> authSchemeRegistry)Sets defaultAuthSchemeregistry which will be used for request execution if not explicitly set in the client execution context.HttpClientBuildersetDefaultCookieSpecRegistry(org.apache.hc.core5.http.config.Lookup<CookieSpecFactory> cookieSpecRegistry)Sets defaultCookieSpecregistry which will be used for request execution if not explicitly set in the client execution context.HttpClientBuildersetDefaultCookieStore(CookieStore cookieStore)Sets defaultCookieStoreinstance which will be used for request execution if not explicitly set in the client execution context.HttpClientBuildersetDefaultCredentialsProvider(CredentialsProvider credentialsProvider)Sets defaultCredentialsProviderinstance which will be used for request execution if not explicitly set in the client execution context.HttpClientBuildersetDefaultHeaders(java.util.Collection<? extends org.apache.hc.core5.http.Header> defaultHeaders)Sets default request header values.HttpClientBuildersetDefaultRequestConfig(RequestConfig config)Sets defaultRequestConfiginstance which will be used for request execution if not explicitly set in the client execution context.HttpClientBuildersetKeepAliveStrategy(ConnectionKeepAliveStrategy keepAliveStrategy)SetsConnectionKeepAliveStrategyinstance.HttpClientBuildersetProxy(org.apache.hc.core5.http.HttpHost proxy)Sets default proxy value.HttpClientBuildersetProxyAuthenticationStrategy(AuthenticationStrategy proxyAuthStrategy)SetsAuthenticationStrategyinstance for proxy authentication.HttpClientBuildersetProxySelector(java.net.ProxySelector proxySelector)Sets theProxySelectorthat will be used to select the proxies to be used for establishing HTTP connections.HttpClientBuildersetRedirectStrategy(RedirectStrategy redirectStrategy)SetsRedirectStrategyinstance.HttpClientBuildersetRequestExecutor(org.apache.hc.core5.http.impl.io.HttpRequestExecutor requestExec)SetsHttpRequestExecutorinstance.HttpClientBuildersetRetryStrategy(HttpRequestRetryStrategy retryStrategy)SetsHttpRequestRetryStrategyinstance.HttpClientBuildersetRoutePlanner(HttpRoutePlanner routePlanner)SetsHttpRoutePlannerinstance.HttpClientBuildersetSchemePortResolver(SchemePortResolver schemePortResolver)SetsSchemePortResolverinstance.HttpClientBuildersetTargetAuthenticationStrategy(AuthenticationStrategy targetAuthStrategy)SetsAuthenticationStrategyinstance for target host authentication.HttpClientBuildersetUserAgent(java.lang.String userAgent)SetsUser-Agentvalue.HttpClientBuildersetUserTokenHandler(UserTokenHandler userTokenHandler)SetsUserTokenHandlerinstance.HttpClientBuilderuseSystemProperties()Use system properties when creating and configuring default implementations.
-
-
-
Method Detail
-
create
public static HttpClientBuilder create()
-
setRequestExecutor
public final HttpClientBuilder setRequestExecutor(org.apache.hc.core5.http.impl.io.HttpRequestExecutor requestExec)
SetsHttpRequestExecutorinstance.- Returns:
- this instance.
-
setConnectionManager
public final HttpClientBuilder setConnectionManager(HttpClientConnectionManager connManager)
SetsHttpClientConnectionManagerinstance.- Returns:
- this 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.- Returns:
- this instance.
- Since:
- 4.4
-
setConnectionReuseStrategy
public final HttpClientBuilder setConnectionReuseStrategy(org.apache.hc.core5.http.ConnectionReuseStrategy reuseStrategy)
SetsConnectionReuseStrategyinstance.- Returns:
- this instance.
-
setKeepAliveStrategy
public final HttpClientBuilder setKeepAliveStrategy(ConnectionKeepAliveStrategy keepAliveStrategy)
SetsConnectionKeepAliveStrategyinstance.- Returns:
- this instance.
-
setTargetAuthenticationStrategy
public final HttpClientBuilder setTargetAuthenticationStrategy(AuthenticationStrategy targetAuthStrategy)
SetsAuthenticationStrategyinstance for target host authentication.- Returns:
- this instance.
-
setProxyAuthenticationStrategy
public final HttpClientBuilder setProxyAuthenticationStrategy(AuthenticationStrategy proxyAuthStrategy)
SetsAuthenticationStrategyinstance for proxy authentication.- Returns:
- this instance.
-
setUserTokenHandler
public final HttpClientBuilder setUserTokenHandler(UserTokenHandler userTokenHandler)
SetsUserTokenHandlerinstance.Please note this value can be overridden by the
disableConnectionState()method.- Returns:
- this instance.
-
disableConnectionState
public final HttpClientBuilder disableConnectionState()
Disables connection state tracking.- Returns:
- this instance.
-
setSchemePortResolver
public final HttpClientBuilder setSchemePortResolver(SchemePortResolver schemePortResolver)
SetsSchemePortResolverinstance.- Returns:
- this instance.
-
setUserAgent
public final HttpClientBuilder setUserAgent(java.lang.String userAgent)
SetsUser-Agentvalue.- Returns:
- this instance.
-
setDefaultHeaders
public final HttpClientBuilder setDefaultHeaders(java.util.Collection<? extends org.apache.hc.core5.http.Header> defaultHeaders)
Sets default request header values.- Returns:
- this instance.
-
addResponseInterceptorFirst
public final HttpClientBuilder addResponseInterceptorFirst(org.apache.hc.core5.http.HttpResponseInterceptor interceptor)
Adds this protocol interceptor to the head of the protocol processing list.- Returns:
- this instance.
-
addResponseInterceptorLast
public final HttpClientBuilder addResponseInterceptorLast(org.apache.hc.core5.http.HttpResponseInterceptor interceptor)
Adds this protocol interceptor to the tail of the protocol processing list.- Returns:
- this instance.
-
addRequestInterceptorFirst
public final HttpClientBuilder addRequestInterceptorFirst(org.apache.hc.core5.http.HttpRequestInterceptor interceptor)
Adds this protocol interceptor to the head of the protocol processing list.- Returns:
- this instance.
-
addRequestInterceptorLast
public final HttpClientBuilder addRequestInterceptorLast(org.apache.hc.core5.http.HttpRequestInterceptor interceptor)
Adds this protocol interceptor to the tail of the protocol processing list.- Returns:
- this instance.
-
addExecInterceptorBefore
public final HttpClientBuilder addExecInterceptorBefore(java.lang.String existing, java.lang.String name, ExecChainHandler interceptor)
Adds this execution interceptor before an existing interceptor.- Returns:
- this instance.
-
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.- Returns:
- this instance.
-
replaceExecInterceptor
public final HttpClientBuilder replaceExecInterceptor(java.lang.String existing, ExecChainHandler interceptor)
Replace an existing interceptor with the given name with new interceptor.- Returns:
- this instance.
-
addExecInterceptorFirst
public final HttpClientBuilder addExecInterceptorFirst(java.lang.String name, ExecChainHandler interceptor)
Add an interceptor to the head of the processing list.- Returns:
- this instance.
-
addExecInterceptorLast
public final HttpClientBuilder addExecInterceptorLast(java.lang.String name, ExecChainHandler interceptor)
Add an interceptor to the tail of the processing list.- Returns:
- this instance.
-
disableCookieManagement
public final HttpClientBuilder disableCookieManagement()
Disables state (cookie) management.- Returns:
- this instance.
-
disableContentCompression
public final HttpClientBuilder disableContentCompression()
Disables automatic content decompression.- Returns:
- this instance.
-
disableAuthCaching
public final HttpClientBuilder disableAuthCaching()
Disables authentication scheme caching.- Returns:
- this instance.
-
setRetryStrategy
public final HttpClientBuilder setRetryStrategy(HttpRequestRetryStrategy retryStrategy)
SetsHttpRequestRetryStrategyinstance.Please note this value can be overridden by the
disableAutomaticRetries()method.- Returns:
- this instance.
-
disableAutomaticRetries
public final HttpClientBuilder disableAutomaticRetries()
Disables automatic request recovery and re-execution.- Returns:
- this instance.
-
setProxy
public final HttpClientBuilder setProxy(org.apache.hc.core5.http.HttpHost proxy)
Sets default proxy value.Please note this value can be overridden by the
setRoutePlanner( org.apache.hc.client5.http.routing.HttpRoutePlanner)method.- Returns:
- this instance.
-
setRoutePlanner
public final HttpClientBuilder setRoutePlanner(HttpRoutePlanner routePlanner)
SetsHttpRoutePlannerinstance.
-
setRedirectStrategy
public final HttpClientBuilder setRedirectStrategy(RedirectStrategy redirectStrategy)
SetsRedirectStrategyinstance.Please note this value can be overridden by the
disableRedirectHandling()method.- Returns:
- this instance.
-
disableRedirectHandling
public final HttpClientBuilder disableRedirectHandling()
Disables automatic redirect handling.- Returns:
- this instance.
-
setConnectionBackoffStrategy
public final HttpClientBuilder setConnectionBackoffStrategy(ConnectionBackoffStrategy connectionBackoffStrategy)
SetsConnectionBackoffStrategyinstance.- Returns:
- this instance.
-
setBackoffManager
public final HttpClientBuilder setBackoffManager(BackoffManager backoffManager)
SetsBackoffManagerinstance.- Returns:
- this instance.
-
setDefaultCookieStore
public final HttpClientBuilder setDefaultCookieStore(CookieStore cookieStore)
Sets defaultCookieStoreinstance which will be used for request execution if not explicitly set in the client execution context.- Returns:
- this instance.
-
setDefaultCredentialsProvider
public final HttpClientBuilder setDefaultCredentialsProvider(CredentialsProvider credentialsProvider)
Sets defaultCredentialsProviderinstance which will be used for request execution if not explicitly set in the client execution context.- Returns:
- this instance.
-
setDefaultAuthSchemeRegistry
public final HttpClientBuilder setDefaultAuthSchemeRegistry(org.apache.hc.core5.http.config.Lookup<AuthSchemeFactory> authSchemeRegistry)
Sets defaultAuthSchemeregistry which will be used for request execution if not explicitly set in the client execution context.- Returns:
- this instance.
-
setDefaultCookieSpecRegistry
public final HttpClientBuilder setDefaultCookieSpecRegistry(org.apache.hc.core5.http.config.Lookup<CookieSpecFactory> cookieSpecRegistry)
Sets defaultCookieSpecregistry which will be used for request execution if not explicitly set in the client execution context.- Returns:
- this instance.
- See Also:
CookieSpecSupport
-
setContentDecoderRegistry
public final HttpClientBuilder setContentDecoderRegistry(java.util.LinkedHashMap<java.lang.String,InputStreamFactory> contentDecoderMap)
Sets a map ofInputStreamFactorys to be used for automatic content decompression.- Returns:
- this instance.
-
setDefaultRequestConfig
public final HttpClientBuilder setDefaultRequestConfig(RequestConfig config)
Sets defaultRequestConfiginstance which will be used for request execution if not explicitly set in the client execution context.- Returns:
- this instance.
-
useSystemProperties
public final HttpClientBuilder useSystemProperties()
Use system properties when creating and configuring default implementations.- Returns:
- this instance.
-
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.
- Returns:
- this instance.
- 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.- Returns:
- this instance.
- 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.- Returns:
- this instance.
- Since:
- 4.5.7
-
setProxySelector
public final HttpClientBuilder setProxySelector(java.net.ProxySelector proxySelector)
Sets theProxySelectorthat 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- theProxySelectorto be used, or null to use the default system proxy selector.- Returns:
- this instance.
-
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 ofCloseableresources to be managed by the client.For internal use.
-
getConnManager
@Internal public HttpClientConnectionManager getConnManager()
-
contextAdaptor
@Internal protected java.util.function.Function<org.apache.hc.core5.http.protocol.HttpContext,HttpClientContext> contextAdaptor()
-
build
public CloseableHttpClient build()
-
-