Class HttpClientBuilder
- Direct Known Subclasses:
CachingHttpClientBuilder
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
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static classprivate static classprivate static class -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate booleanprivate org.apache.hc.core5.http.config.Lookup<AuthSchemeFactory> private booleanprivate BackoffManagerprivate ConnectionBackoffStrategyprivate booleanprivate HttpClientConnectionManagerprivate booleanprivate booleanprivate LinkedHashMap<String, InputStreamFactory> private booleanprivate org.apache.hc.core5.http.config.Lookup<CookieSpecFactory> private CookieStoreprivate CredentialsProviderprivate Collection<? extends org.apache.hc.core5.http.Header> private RequestConfigprivate booleanprivate booleanprivate booleanprivate ConnectionKeepAliveStrategyprivate org.apache.hc.core5.util.TimeValueprivate org.apache.hc.core5.http.HttpHostprivate AuthenticationStrategyprivate ProxySelectorprivate booleanprivate RedirectStrategyprivate org.apache.hc.core5.http.impl.io.HttpRequestExecutorprivate HttpRequestRetryStrategyprivate org.apache.hc.core5.http.ConnectionReuseStrategyprivate HttpRoutePlannerprivate SchemePortResolverprivate booleanprivate AuthenticationStrategyprivate Stringprivate UserTokenHandler -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected voidaddCloseable(Closeable closeable) Adds to the list ofCloseableresources to be managed by the client.final HttpClientBuilderaddExecInterceptorAfter(String existing, String name, ExecChainHandler interceptor) Adds this execution interceptor after interceptor with the given name.final HttpClientBuilderaddExecInterceptorBefore(String existing, String name, ExecChainHandler interceptor) Adds this execution interceptor before an existing interceptor.final HttpClientBuilderaddExecInterceptorFirst(String name, ExecChainHandler interceptor) Add an interceptor to the head of the processing list.final HttpClientBuilderaddExecInterceptorLast(String name, ExecChainHandler interceptor) Add an interceptor to the tail of the processing list.final HttpClientBuilderaddRequestInterceptorFirst(org.apache.hc.core5.http.HttpRequestInterceptor interceptor) Adds this protocol interceptor to the head of the protocol processing list.final HttpClientBuilderaddRequestInterceptorLast(org.apache.hc.core5.http.HttpRequestInterceptor interceptor) Adds this protocol interceptor to the tail of the protocol processing list.final HttpClientBuilderaddResponseInterceptorFirst(org.apache.hc.core5.http.HttpResponseInterceptor interceptor) Adds this protocol interceptor to the head of the protocol processing list.final HttpClientBuilderaddResponseInterceptorLast(org.apache.hc.core5.http.HttpResponseInterceptor interceptor) Adds this protocol interceptor to the tail of the protocol processing list.build()protected Function<org.apache.hc.core5.http.protocol.HttpContext, HttpClientContext> static HttpClientBuildercreate()protected voidcustomizeExecChain(org.apache.hc.core5.http.config.NamedElementChain<ExecChainHandler> execChainDefinition) Request exec chain customization and extension.final HttpClientBuilderDisables authentication scheme caching.final HttpClientBuilderDisables automatic request recovery and re-execution.final HttpClientBuilderDisables connection state tracking.final HttpClientBuilderDisables automatic content decompression.final HttpClientBuilderDisables state (cookie) management.final HttpClientBuilderDisables the default user agent set by this builder if none has been provided by the user.final HttpClientBuilderDisables automatic redirect handling.final HttpClientBuilderMakes this instance of HttpClient proactively evict expired connections from the connection pool using a background thread.final 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.final HttpClientBuilderreplaceExecInterceptor(String existing, ExecChainHandler interceptor) Replace an existing interceptor with the given name with new interceptor.final HttpClientBuildersetBackoffManager(BackoffManager backoffManager) SetsBackoffManagerinstance.final HttpClientBuildersetConnectionBackoffStrategy(ConnectionBackoffStrategy connectionBackoffStrategy) SetsConnectionBackoffStrategyinstance.final HttpClientBuildersetConnectionManager(HttpClientConnectionManager connManager) SetsHttpClientConnectionManagerinstance.final HttpClientBuildersetConnectionManagerShared(boolean shared) Defines the connection manager is to be shared by multiple client instances.final HttpClientBuildersetConnectionReuseStrategy(org.apache.hc.core5.http.ConnectionReuseStrategy reuseStrategy) SetsConnectionReuseStrategyinstance.final HttpClientBuildersetContentDecoderRegistry(LinkedHashMap<String, InputStreamFactory> contentDecoderMap) Sets a map ofInputStreamFactorys to be used for automatic content decompression.final 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.final 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.final HttpClientBuildersetDefaultCookieStore(CookieStore cookieStore) Sets defaultCookieStoreinstance which will be used for request execution if not explicitly set in the client execution context.final HttpClientBuildersetDefaultCredentialsProvider(CredentialsProvider credentialsProvider) Sets defaultCredentialsProviderinstance which will be used for request execution if not explicitly set in the client execution context.final HttpClientBuildersetDefaultHeaders(Collection<? extends org.apache.hc.core5.http.Header> defaultHeaders) Sets default request header values.final HttpClientBuildersetDefaultRequestConfig(RequestConfig config) Sets defaultRequestConfiginstance which will be used for request execution if not explicitly set in the client execution context.final HttpClientBuildersetKeepAliveStrategy(ConnectionKeepAliveStrategy keepAliveStrategy) SetsConnectionKeepAliveStrategyinstance.final HttpClientBuildersetProxy(org.apache.hc.core5.http.HttpHost proxy) Sets default proxy value.final HttpClientBuildersetProxyAuthenticationStrategy(AuthenticationStrategy proxyAuthStrategy) SetsAuthenticationStrategyinstance for proxy authentication.final HttpClientBuildersetProxySelector(ProxySelector proxySelector) Sets theProxySelectorthat will be used to select the proxies to be used for establishing HTTP connections.final HttpClientBuildersetRedirectStrategy(RedirectStrategy redirectStrategy) SetsRedirectStrategyinstance.final HttpClientBuildersetRequestExecutor(org.apache.hc.core5.http.impl.io.HttpRequestExecutor requestExec) SetsHttpRequestExecutorinstance.final HttpClientBuildersetRetryStrategy(HttpRequestRetryStrategy retryStrategy) SetsHttpRequestRetryStrategyinstance.final HttpClientBuildersetRoutePlanner(HttpRoutePlanner routePlanner) SetsHttpRoutePlannerinstance.final HttpClientBuildersetSchemePortResolver(SchemePortResolver schemePortResolver) SetsSchemePortResolverinstance.final HttpClientBuildersetTargetAuthenticationStrategy(AuthenticationStrategy targetAuthStrategy) SetsAuthenticationStrategyinstance for target host authentication.final HttpClientBuildersetUserAgent(String userAgent) SetsUser-Agentvalue.final HttpClientBuildersetUserTokenHandler(UserTokenHandler userTokenHandler) SetsUserTokenHandlerinstance.final HttpClientBuilderUse system properties when creating and configuring default implementations.
-
Field Details
-
requestExec
private org.apache.hc.core5.http.impl.io.HttpRequestExecutor requestExec -
connManager
-
schemePortResolver
-
reuseStrategy
private org.apache.hc.core5.http.ConnectionReuseStrategy reuseStrategy -
keepAliveStrategy
-
targetAuthStrategy
-
proxyAuthStrategy
-
userTokenHandler
-
requestInterceptors
-
responseInterceptors
-
execInterceptors
-
retryStrategy
-
routePlanner
-
redirectStrategy
-
connectionBackoffStrategy
-
backoffManager
-
authSchemeRegistry
-
cookieSpecRegistry
-
contentDecoderMap
-
cookieStore
-
credentialsProvider
-
userAgent
-
proxy
private org.apache.hc.core5.http.HttpHost proxy -
defaultHeaders
-
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
-
closeables
-
-
Constructor Details
-
HttpClientBuilder
protected HttpClientBuilder()
-
-
Method Details
-
create
-
setRequestExecutor
public final HttpClientBuilder setRequestExecutor(org.apache.hc.core5.http.impl.io.HttpRequestExecutor requestExec) SetsHttpRequestExecutorinstance.- Returns:
- this instance.
-
setConnectionManager
SetsHttpClientConnectionManagerinstance.- Returns:
- this instance.
-
setConnectionReuseStrategy
public final HttpClientBuilder setConnectionReuseStrategy(org.apache.hc.core5.http.ConnectionReuseStrategy reuseStrategy) SetsConnectionReuseStrategyinstance.- Returns:
- this instance.
-
setKeepAliveStrategy
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
SetsUserTokenHandlerinstance.Please note this value can be overridden by the
disableConnectionState()method.- Returns:
- this instance.
-
disableConnectionState
Disables connection state tracking.- Returns:
- this instance.
-
setSchemePortResolver
SetsSchemePortResolverinstance.- Returns:
- this instance.
-
setUserAgent
SetsUser-Agentvalue.- Returns:
- this instance.
-
setDefaultHeaders
public final HttpClientBuilder setDefaultHeaders(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(String existing, String name, ExecChainHandler interceptor) Adds this execution interceptor before an existing interceptor.- Returns:
- this instance.
-
addExecInterceptorAfter
public final HttpClientBuilder addExecInterceptorAfter(String existing, String name, ExecChainHandler interceptor) Adds this execution interceptor after interceptor with the given name.- Returns:
- this instance.
-
replaceExecInterceptor
public final HttpClientBuilder replaceExecInterceptor(String existing, ExecChainHandler interceptor) Replace an existing interceptor with the given name with new interceptor.- Returns:
- this instance.
-
addExecInterceptorFirst
Add an interceptor to the head of the processing list.- Returns:
- this instance.
-
addExecInterceptorLast
Add an interceptor to the tail of the processing list.- Returns:
- this instance.
-
disableCookieManagement
Disables state (cookie) management.- Returns:
- this instance.
-
disableContentCompression
Disables automatic content decompression.- Returns:
- this instance.
-
disableAuthCaching
Disables authentication scheme caching.- Returns:
- this instance.
-
setRetryStrategy
SetsHttpRequestRetryStrategyinstance.Please note this value can be overridden by the
disableAutomaticRetries()method.- Returns:
- this instance.
-
disableAutomaticRetries
Disables automatic request recovery and re-execution.- Returns:
- this instance.
-
setProxy
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
SetsHttpRoutePlannerinstance. -
setRedirectStrategy
SetsRedirectStrategyinstance.Please note this value can be overridden by the
disableRedirectHandling()method.- Returns:
- this instance.
-
disableRedirectHandling
Disables automatic redirect handling.- Returns:
- this instance.
-
setConnectionBackoffStrategy
public final HttpClientBuilder setConnectionBackoffStrategy(ConnectionBackoffStrategy connectionBackoffStrategy) SetsConnectionBackoffStrategyinstance.- Returns:
- this instance.
-
setBackoffManager
SetsBackoffManagerinstance.- Returns:
- this instance.
-
setDefaultCookieStore
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:
-
setContentDecoderRegistry
public final HttpClientBuilder setContentDecoderRegistry(LinkedHashMap<String, InputStreamFactory> contentDecoderMap) Sets a map ofInputStreamFactorys to be used for automatic content decompression.- Returns:
- this instance.
-
setDefaultRequestConfig
Sets defaultRequestConfiginstance which will be used for request execution if not explicitly set in the client execution context.- Returns:
- this instance.
-
useSystemProperties
Use system properties when creating and configuring default implementations.- Returns:
- this instance.
-
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:
-
evictIdleConnections
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:
-
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
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
-
getConnManager
-
contextAdaptor
@Internal protected Function<org.apache.hc.core5.http.protocol.HttpContext, HttpClientContext> contextAdaptor() -
build
-