public class HttpClientBuilder extends Object
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()
.
Please note that some settings used by this class can be mutually
exclusive and may not apply when building CloseableHttpClient
instances.
Modifier | Constructor and Description |
---|---|
protected |
HttpClientBuilder() |
Modifier and Type | Method and Description |
---|---|
protected void |
addCloseable(Closeable closeable)
Adds to the list of
Closeable resources to be managed by the client. |
HttpClientBuilder |
addExecInterceptorAfter(String existing,
String name,
ExecChainHandler interceptor)
Adds this execution interceptor after interceptor with the given name.
|
HttpClientBuilder |
addExecInterceptorBefore(String existing,
String name,
ExecChainHandler interceptor)
Adds this execution interceptor before an existing interceptor.
|
HttpClientBuilder |
addExecInterceptorFirst(String name,
ExecChainHandler interceptor)
Add an interceptor to the head of the processing list.
|
HttpClientBuilder |
addExecInterceptorLast(String name,
ExecChainHandler interceptor)
Add an interceptor to the tail of the processing list.
|
HttpClientBuilder |
addRequestInterceptorFirst(HttpRequestInterceptor interceptor)
Adds this protocol interceptor to the head of the protocol processing list.
|
HttpClientBuilder |
addRequestInterceptorLast(HttpRequestInterceptor interceptor)
Adds this protocol interceptor to the tail of the protocol processing list.
|
HttpClientBuilder |
addResponseInterceptorFirst(HttpResponseInterceptor interceptor)
Adds this protocol interceptor to the head of the protocol processing list.
|
HttpClientBuilder |
addResponseInterceptorLast(HttpResponseInterceptor interceptor)
Adds this protocol interceptor to the tail of the protocol processing list.
|
CloseableHttpClient |
build() |
static HttpClientBuilder |
create() |
protected void |
customizeExecChain(NamedElementChain<ExecChainHandler> execChainDefinition)
Request exec chain customization and extension.
|
HttpClientBuilder |
disableAuthCaching()
Disables authentication scheme caching.
|
HttpClientBuilder |
disableAutomaticRetries()
Disables automatic request recovery and re-execution.
|
HttpClientBuilder |
disableConnectionState()
Disables connection state tracking.
|
HttpClientBuilder |
disableContentCompression()
Disables automatic content decompression.
|
HttpClientBuilder |
disableCookieManagement()
Disables state (cookie) management.
|
HttpClientBuilder |
disableDefaultUserAgent()
Disables the default user agent set by this builder if none has been provided by the user.
|
HttpClientBuilder |
disableRedirectHandling()
Disables automatic redirect handling.
|
HttpClientBuilder |
evictExpiredConnections()
Makes this instance of HttpClient proactively evict expired connections from the
connection pool using a background thread.
|
HttpClientBuilder |
evictIdleConnections(TimeValue maxIdleTime)
Makes this instance of HttpClient proactively evict idle connections from the
connection pool using a background thread.
|
HttpClientBuilder |
replaceExecInterceptor(String existing,
ExecChainHandler interceptor)
Replace an existing interceptor with the given name with new interceptor.
|
HttpClientBuilder |
setBackoffManager(BackoffManager backoffManager)
Assigns
BackoffManager instance. |
HttpClientBuilder |
setConnectionBackoffStrategy(ConnectionBackoffStrategy connectionBackoffStrategy)
Assigns
ConnectionBackoffStrategy instance. |
HttpClientBuilder |
setConnectionManager(HttpClientConnectionManager connManager)
Assigns
HttpClientConnectionManager instance. |
HttpClientBuilder |
setConnectionManagerShared(boolean shared)
Defines the connection manager is to be shared by multiple
client instances.
|
HttpClientBuilder |
setConnectionReuseStrategy(ConnectionReuseStrategy reuseStrategy)
Assigns
ConnectionReuseStrategy instance. |
HttpClientBuilder |
setContentDecoderRegistry(java.util.LinkedHashMap<String,InputStreamFactory> contentDecoderMap)
Assigns a map of
InputStreamFactory s
to be used for automatic content decompression. |
HttpClientBuilder |
setDefaultAuthSchemeRegistry(Lookup<AuthSchemeFactory> authSchemeRegistry)
Assigns default
AuthScheme registry which will
be used for request execution if not explicitly set in the client execution
context. |
HttpClientBuilder |
setDefaultCookieSpecRegistry(Lookup<CookieSpecFactory> cookieSpecRegistry)
Assigns default
CookieSpec registry which will
be used for request execution if not explicitly set in the client execution
context. |
HttpClientBuilder |
setDefaultCookieStore(CookieStore cookieStore)
Assigns default
CookieStore instance which will be used for
request execution if not explicitly set in the client execution context. |
HttpClientBuilder |
setDefaultCredentialsProvider(CredentialsProvider credentialsProvider)
Assigns default
CredentialsProvider instance which will be used
for request execution if not explicitly set in the client execution
context. |
HttpClientBuilder |
setDefaultHeaders(Collection<? extends Header> defaultHeaders)
Assigns default request header values.
|
HttpClientBuilder |
setDefaultRequestConfig(RequestConfig config)
Assigns default
RequestConfig instance which will be used
for request execution if not explicitly set in the client execution
context. |
HttpClientBuilder |
setKeepAliveStrategy(ConnectionKeepAliveStrategy keepAliveStrategy)
Assigns
ConnectionKeepAliveStrategy instance. |
HttpClientBuilder |
setProxy(HttpHost proxy)
Assigns default proxy value.
|
HttpClientBuilder |
setProxyAuthenticationStrategy(AuthenticationStrategy proxyAuthStrategy)
Assigns
AuthenticationStrategy instance for proxy
authentication. |
HttpClientBuilder |
setRedirectStrategy(RedirectStrategy redirectStrategy)
Assigns
RedirectStrategy instance. |
HttpClientBuilder |
setRequestExecutor(HttpRequestExecutor requestExec)
Assigns
HttpRequestExecutor instance. |
HttpClientBuilder |
setRetryStrategy(HttpRequestRetryStrategy retryStrategy)
Assigns
HttpRequestRetryStrategy instance. |
HttpClientBuilder |
setRoutePlanner(HttpRoutePlanner routePlanner)
Assigns
HttpRoutePlanner instance. |
HttpClientBuilder |
setSchemePortResolver(SchemePortResolver schemePortResolver)
Assigns
SchemePortResolver instance. |
HttpClientBuilder |
setTargetAuthenticationStrategy(AuthenticationStrategy targetAuthStrategy)
Assigns
AuthenticationStrategy instance for target
host authentication. |
HttpClientBuilder |
setUserAgent(String userAgent)
Assigns
User-Agent value. |
HttpClientBuilder |
setUserTokenHandler(UserTokenHandler userTokenHandler)
Assigns
UserTokenHandler instance. |
HttpClientBuilder |
useSystemProperties()
Use system properties when creating and configuring default
implementations.
|
@Internal protected void addCloseable(Closeable closeable)
Closeable
resources to be managed by the client.
For internal use.
public final HttpClientBuilder addExecInterceptorAfter(String existing, String name, ExecChainHandler interceptor)
public final HttpClientBuilder addExecInterceptorBefore(String existing, String name, ExecChainHandler interceptor)
public final HttpClientBuilder addExecInterceptorFirst(String name, ExecChainHandler interceptor)
public final HttpClientBuilder addExecInterceptorLast(String name, ExecChainHandler interceptor)
public final HttpClientBuilder addRequestInterceptorFirst(HttpRequestInterceptor interceptor)
public final HttpClientBuilder addRequestInterceptorLast(HttpRequestInterceptor interceptor)
public final HttpClientBuilder addResponseInterceptorFirst(HttpResponseInterceptor interceptor)
public final HttpClientBuilder addResponseInterceptorLast(HttpResponseInterceptor interceptor)
public CloseableHttpClient build()
public static HttpClientBuilder create()
@Internal protected void customizeExecChain(NamedElementChain<ExecChainHandler> execChainDefinition)
For internal use.
public final HttpClientBuilder disableAuthCaching()
public final HttpClientBuilder disableAutomaticRetries()
public final HttpClientBuilder disableConnectionState()
public final HttpClientBuilder disableContentCompression()
public final HttpClientBuilder disableCookieManagement()
public final HttpClientBuilder disableDefaultUserAgent()
public final HttpClientBuilder disableRedirectHandling()
public final HttpClientBuilder evictExpiredConnections()
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.
setConnectionManagerShared(boolean)
,
ConnPoolControl.closeExpired()
public final HttpClientBuilder evictIdleConnections(TimeValue maxIdleTime)
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.
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.setConnectionManagerShared(boolean)
,
ConnPoolControl.closeIdle(TimeValue)
public final HttpClientBuilder replaceExecInterceptor(String existing, ExecChainHandler interceptor)
public final HttpClientBuilder setBackoffManager(BackoffManager backoffManager)
BackoffManager
instance.public final HttpClientBuilder setConnectionBackoffStrategy(ConnectionBackoffStrategy connectionBackoffStrategy)
ConnectionBackoffStrategy
instance.public final HttpClientBuilder setConnectionManager(HttpClientConnectionManager connManager)
HttpClientConnectionManager
instance.public final HttpClientBuilder setConnectionManagerShared(boolean shared)
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.
shared
- defines whether or not the connection manager can be shared
by multiple clients.public final HttpClientBuilder setConnectionReuseStrategy(ConnectionReuseStrategy reuseStrategy)
ConnectionReuseStrategy
instance.public final HttpClientBuilder setContentDecoderRegistry(java.util.LinkedHashMap<String,InputStreamFactory> contentDecoderMap)
InputStreamFactory
s
to be used for automatic content decompression.public final HttpClientBuilder setDefaultAuthSchemeRegistry(Lookup<AuthSchemeFactory> authSchemeRegistry)
AuthScheme
registry which will
be used for request execution if not explicitly set in the client execution
context.public final HttpClientBuilder setDefaultCookieSpecRegistry(Lookup<CookieSpecFactory> cookieSpecRegistry)
CookieSpec
registry which will
be used for request execution if not explicitly set in the client execution
context.CookieSpecSupport
public final HttpClientBuilder setDefaultCookieStore(CookieStore cookieStore)
CookieStore
instance which will be used for
request execution if not explicitly set in the client execution context.public final HttpClientBuilder setDefaultCredentialsProvider(CredentialsProvider credentialsProvider)
CredentialsProvider
instance which will be used
for request execution if not explicitly set in the client execution
context.public final HttpClientBuilder setDefaultHeaders(Collection<? extends Header> defaultHeaders)
public final HttpClientBuilder setDefaultRequestConfig(RequestConfig config)
RequestConfig
instance which will be used
for request execution if not explicitly set in the client execution
context.public final HttpClientBuilder setKeepAliveStrategy(ConnectionKeepAliveStrategy keepAliveStrategy)
ConnectionKeepAliveStrategy
instance.public final HttpClientBuilder setProxy(HttpHost proxy)
Please note this value can be overridden by the setRoutePlanner(
org.apache.hc.client5.http.routing.HttpRoutePlanner)
method.
public final HttpClientBuilder setProxyAuthenticationStrategy(AuthenticationStrategy proxyAuthStrategy)
AuthenticationStrategy
instance for proxy
authentication.public final HttpClientBuilder setRedirectStrategy(RedirectStrategy redirectStrategy)
RedirectStrategy
instance.
Please note this value can be overridden by the disableRedirectHandling()
method.
public final HttpClientBuilder setRequestExecutor(HttpRequestExecutor requestExec)
HttpRequestExecutor
instance.public final HttpClientBuilder setRetryStrategy(HttpRequestRetryStrategy retryStrategy)
HttpRequestRetryStrategy
instance.
Please note this value can be overridden by the disableAutomaticRetries()
method.
public final HttpClientBuilder setRoutePlanner(HttpRoutePlanner routePlanner)
HttpRoutePlanner
instance.public final HttpClientBuilder setSchemePortResolver(SchemePortResolver schemePortResolver)
SchemePortResolver
instance.public final HttpClientBuilder setTargetAuthenticationStrategy(AuthenticationStrategy targetAuthStrategy)
AuthenticationStrategy
instance for target
host authentication.public final HttpClientBuilder setUserAgent(String userAgent)
User-Agent
value.public final HttpClientBuilder setUserTokenHandler(UserTokenHandler userTokenHandler)
UserTokenHandler
instance.
Please note this value can be overridden by the disableConnectionState()
method.
public final HttpClientBuilder useSystemProperties()