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)
For internal use.
|
HttpClientBuilder |
addInterceptorFirst(org.apache.http.HttpRequestInterceptor itcp)
Adds this protocol interceptor to the head of the protocol processing list.
|
HttpClientBuilder |
addInterceptorFirst(org.apache.http.HttpResponseInterceptor itcp)
Adds this protocol interceptor to the head of the protocol processing list.
|
HttpClientBuilder |
addInterceptorLast(org.apache.http.HttpRequestInterceptor itcp)
Adds this protocol interceptor to the tail of the protocol processing list.
|
HttpClientBuilder |
addInterceptorLast(org.apache.http.HttpResponseInterceptor itcp)
Adds this protocol interceptor to the tail of the protocol processing list.
|
CloseableHttpClient |
build() |
static HttpClientBuilder |
create() |
protected ClientExecChain |
createMainExec(org.apache.http.protocol.HttpRequestExecutor requestExec,
HttpClientConnectionManager connManager,
org.apache.http.ConnectionReuseStrategy reuseStrategy,
ConnectionKeepAliveStrategy keepAliveStrategy,
org.apache.http.protocol.HttpProcessor proxyHttpProcessor,
AuthenticationStrategy targetAuthStrategy,
AuthenticationStrategy proxyAuthStrategy,
UserTokenHandler userTokenHandler)
Produces an instance of
ClientExecChain to be used as a main exec. |
protected ClientExecChain |
decorateMainExec(ClientExecChain mainExec)
For internal use.
|
protected ClientExecChain |
decorateProtocolExec(ClientExecChain protocolExec)
For internal use.
|
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(long maxIdleTime,
TimeUnit maxIdleTimeUnit)
Makes this instance of HttpClient proactively evict idle connections from the
connection pool using a background thread.
|
HttpClientBuilder |
evictIdleConnections(Long maxIdleTime,
TimeUnit maxIdleTimeUnit)
Deprecated.
(4.5) use
evictIdleConnections(long, TimeUnit) |
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(org.apache.http.ConnectionReuseStrategy reuseStrategy)
Assigns
ConnectionReuseStrategy instance. |
HttpClientBuilder |
setConnectionTimeToLive(long connTimeToLive,
TimeUnit connTimeToLiveTimeUnit)
Sets maximum time to live for persistent connections
|
HttpClientBuilder |
setContentDecoderRegistry(Map<String,InputStreamFactory> contentDecoderMap)
Assigns a map of
InputStreamFactorys
to be used for automatic content decompression. |
HttpClientBuilder |
setDefaultAuthSchemeRegistry(org.apache.http.config.Lookup<AuthSchemeProvider> authSchemeRegistry)
Assigns default
AuthScheme registry which will
be used for request execution if not explicitly set in the client execution
context. |
HttpClientBuilder |
setDefaultConnectionConfig(org.apache.http.config.ConnectionConfig config)
Assigns default
ConnectionConfig. |
HttpClientBuilder |
setDefaultCookieSpecRegistry(org.apache.http.config.Lookup<CookieSpecProvider> 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 org.apache.http.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 |
setDefaultSocketConfig(org.apache.http.config.SocketConfig config)
Assigns default
SocketConfig. |
HttpClientBuilder |
setDnsResolver(DnsResolver dnsResolver)
Assigns
DnsResolver instance. |
HttpClientBuilder |
setHostnameVerifier(X509HostnameVerifier hostnameVerifier)
Deprecated.
(4.4)
|
HttpClientBuilder |
setHttpProcessor(org.apache.http.protocol.HttpProcessor httpprocessor)
Assigns
HttpProcessor instance. |
HttpClientBuilder |
setKeepAliveStrategy(ConnectionKeepAliveStrategy keepAliveStrategy)
Assigns
ConnectionKeepAliveStrategy instance. |
HttpClientBuilder |
setMaxConnPerRoute(int maxConnPerRoute)
Assigns maximum connection per route value.
|
HttpClientBuilder |
setMaxConnTotal(int maxConnTotal)
Assigns maximum total connection value.
|
HttpClientBuilder |
setProxy(org.apache.http.HttpHost proxy)
Assigns default proxy value.
|
HttpClientBuilder |
setProxyAuthenticationStrategy(AuthenticationStrategy proxyAuthStrategy)
Assigns
AuthenticationStrategy instance for proxy
authentication. |
HttpClientBuilder |
setPublicSuffixMatcher(PublicSuffixMatcher publicSuffixMatcher)
Assigns file containing public suffix matcher.
|
HttpClientBuilder |
setRedirectStrategy(RedirectStrategy redirectStrategy)
Assigns
RedirectStrategy instance. |
HttpClientBuilder |
setRequestExecutor(org.apache.http.protocol.HttpRequestExecutor requestExec)
Assigns
HttpRequestExecutor instance. |
HttpClientBuilder |
setRetryHandler(HttpRequestRetryHandler retryHandler)
Assigns
HttpRequestRetryHandler instance. |
HttpClientBuilder |
setRoutePlanner(HttpRoutePlanner routePlanner)
Assigns
HttpRoutePlanner instance. |
HttpClientBuilder |
setSchemePortResolver(SchemePortResolver schemePortResolver)
Assigns
SchemePortResolver instance. |
HttpClientBuilder |
setServiceUnavailableRetryStrategy(ServiceUnavailableRetryStrategy serviceUnavailStrategy)
Assigns
ServiceUnavailableRetryStrategy instance. |
HttpClientBuilder |
setSslcontext(SSLContext sslcontext)
Deprecated.
(4.5) use
setSSLContext(SSLContext) |
HttpClientBuilder |
setSSLContext(SSLContext sslContext)
Assigns
SSLContext instance. |
HttpClientBuilder |
setSSLHostnameVerifier(HostnameVerifier hostnameVerifier)
Assigns
HostnameVerifier instance. |
HttpClientBuilder |
setSSLSocketFactory(LayeredConnectionSocketFactory sslSocketFactory)
Assigns
LayeredConnectionSocketFactory 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.
|
public static HttpClientBuilder create()
public final HttpClientBuilder setRequestExecutor(org.apache.http.protocol.HttpRequestExecutor requestExec)
HttpRequestExecutor instance.@Deprecated public final HttpClientBuilder setHostnameVerifier(X509HostnameVerifier hostnameVerifier)
X509HostnameVerifier instance.
Please note this value can be overridden by the setConnectionManager(
org.apache.http.conn.HttpClientConnectionManager) and the setSSLSocketFactory(
org.apache.http.conn.socket.LayeredConnectionSocketFactory) methods.
public final HttpClientBuilder setSSLHostnameVerifier(HostnameVerifier hostnameVerifier)
HostnameVerifier instance.
Please note this value can be overridden by the setConnectionManager(
org.apache.http.conn.HttpClientConnectionManager) and the setSSLSocketFactory(
org.apache.http.conn.socket.LayeredConnectionSocketFactory) methods.
public final HttpClientBuilder setPublicSuffixMatcher(PublicSuffixMatcher publicSuffixMatcher)
PublicSuffixMatcherLoader.PublicSuffixMatcher,
PublicSuffixMatcherLoader@Deprecated public final HttpClientBuilder setSslcontext(SSLContext sslcontext)
setSSLContext(SSLContext)SSLContext instance.
Please note this value can be overridden by the setConnectionManager(
org.apache.http.conn.HttpClientConnectionManager) and the setSSLSocketFactory(
org.apache.http.conn.socket.LayeredConnectionSocketFactory) methods.
public final HttpClientBuilder setSSLContext(SSLContext sslContext)
SSLContext instance.
Please note this value can be overridden by the setConnectionManager(
org.apache.http.conn.HttpClientConnectionManager) and the setSSLSocketFactory(
org.apache.http.conn.socket.LayeredConnectionSocketFactory) methods.
public final HttpClientBuilder setSSLSocketFactory(LayeredConnectionSocketFactory sslSocketFactory)
LayeredConnectionSocketFactory instance.
Please note this value can be overridden by the setConnectionManager(
org.apache.http.conn.HttpClientConnectionManager) method.
public final HttpClientBuilder setMaxConnTotal(int maxConnTotal)
Please note this value can be overridden by the setConnectionManager(
org.apache.http.conn.HttpClientConnectionManager) method.
public final HttpClientBuilder setMaxConnPerRoute(int maxConnPerRoute)
Please note this value can be overridden by the setConnectionManager(
org.apache.http.conn.HttpClientConnectionManager) method.
public final HttpClientBuilder setDefaultSocketConfig(org.apache.http.config.SocketConfig config)
SocketConfig.
Please note this value can be overridden by the setConnectionManager(
org.apache.http.conn.HttpClientConnectionManager) method.
public final HttpClientBuilder setDefaultConnectionConfig(org.apache.http.config.ConnectionConfig config)
ConnectionConfig.
Please note this value can be overridden by the setConnectionManager(
org.apache.http.conn.HttpClientConnectionManager) method.
public final HttpClientBuilder setConnectionTimeToLive(long connTimeToLive, TimeUnit connTimeToLiveTimeUnit)
Please note this value can be overridden by the setConnectionManager(
org.apache.http.conn.HttpClientConnectionManager) method.
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(org.apache.http.ConnectionReuseStrategy reuseStrategy)
ConnectionReuseStrategy instance.public final HttpClientBuilder setKeepAliveStrategy(ConnectionKeepAliveStrategy keepAliveStrategy)
ConnectionKeepAliveStrategy instance.public final HttpClientBuilder setTargetAuthenticationStrategy(AuthenticationStrategy targetAuthStrategy)
AuthenticationStrategy instance for target
host authentication.public final HttpClientBuilder setProxyAuthenticationStrategy(AuthenticationStrategy proxyAuthStrategy)
AuthenticationStrategy instance for proxy
authentication.public final HttpClientBuilder setUserTokenHandler(UserTokenHandler userTokenHandler)
UserTokenHandler instance.
Please note this value can be overridden by the disableConnectionState()
method.
public final HttpClientBuilder disableConnectionState()
public final HttpClientBuilder setSchemePortResolver(SchemePortResolver schemePortResolver)
SchemePortResolver instance.public final HttpClientBuilder setUserAgent(String userAgent)
User-Agent value.
Please note this value can be overridden by the setHttpProcessor(
org.apache.http.protocol.HttpProcessor) method.
public final HttpClientBuilder setDefaultHeaders(Collection<? extends org.apache.http.Header> defaultHeaders)
Please note this value can be overridden by the setHttpProcessor(
org.apache.http.protocol.HttpProcessor) method.
public final HttpClientBuilder addInterceptorFirst(org.apache.http.HttpResponseInterceptor itcp)
Please note this value can be overridden by the setHttpProcessor(
org.apache.http.protocol.HttpProcessor) method.
public final HttpClientBuilder addInterceptorLast(org.apache.http.HttpResponseInterceptor itcp)
Please note this value can be overridden by the setHttpProcessor(
org.apache.http.protocol.HttpProcessor) method.
public final HttpClientBuilder addInterceptorFirst(org.apache.http.HttpRequestInterceptor itcp)
Please note this value can be overridden by the setHttpProcessor(
org.apache.http.protocol.HttpProcessor) method.
public final HttpClientBuilder addInterceptorLast(org.apache.http.HttpRequestInterceptor itcp)
Please note this value can be overridden by the setHttpProcessor(
org.apache.http.protocol.HttpProcessor) method.
public final HttpClientBuilder disableCookieManagement()
Please note this value can be overridden by the setHttpProcessor(
org.apache.http.protocol.HttpProcessor) method.
public final HttpClientBuilder disableContentCompression()
Please note this value can be overridden by the setHttpProcessor(
org.apache.http.protocol.HttpProcessor) method.
public final HttpClientBuilder disableAuthCaching()
Please note this value can be overridden by the setHttpProcessor(
org.apache.http.protocol.HttpProcessor) method.
public final HttpClientBuilder setHttpProcessor(org.apache.http.protocol.HttpProcessor httpprocessor)
HttpProcessor instance.public final HttpClientBuilder setDnsResolver(DnsResolver dnsResolver)
DnsResolver instance.
Please note this value can be overridden by the setConnectionManager(HttpClientConnectionManager) method.
public final HttpClientBuilder setRetryHandler(HttpRequestRetryHandler retryHandler)
HttpRequestRetryHandler instance.
Please note this value can be overridden by the disableAutomaticRetries()
method.
public final HttpClientBuilder disableAutomaticRetries()
public final HttpClientBuilder setProxy(org.apache.http.HttpHost proxy)
Please note this value can be overridden by the setRoutePlanner(
org.apache.http.conn.routing.HttpRoutePlanner) method.
public final HttpClientBuilder setRoutePlanner(HttpRoutePlanner routePlanner)
HttpRoutePlanner instance.public final HttpClientBuilder setRedirectStrategy(RedirectStrategy redirectStrategy)
RedirectStrategy instance.
Please note this value can be overridden by the disableRedirectHandling()
method.
public final HttpClientBuilder disableRedirectHandling()
public final HttpClientBuilder setConnectionBackoffStrategy(ConnectionBackoffStrategy connectionBackoffStrategy)
ConnectionBackoffStrategy instance.public final HttpClientBuilder setBackoffManager(BackoffManager backoffManager)
BackoffManager instance.public final HttpClientBuilder setServiceUnavailableRetryStrategy(ServiceUnavailableRetryStrategy serviceUnavailStrategy)
ServiceUnavailableRetryStrategy instance.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 setDefaultAuthSchemeRegistry(org.apache.http.config.Lookup<AuthSchemeProvider> authSchemeRegistry)
AuthScheme registry which will
be used for request execution if not explicitly set in the client execution
context.public final HttpClientBuilder setDefaultCookieSpecRegistry(org.apache.http.config.Lookup<CookieSpecProvider> cookieSpecRegistry)
CookieSpec registry which will
be used for request execution if not explicitly set in the client execution
context.CookieSpecRegistriespublic final HttpClientBuilder setContentDecoderRegistry(Map<String,InputStreamFactory> contentDecoderMap)
InputStreamFactorys
to be used for automatic content decompression.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 useSystemProperties()
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 configuted to use a shared connection manager.
Please note this method may not be used when the instance of HttpClient is created inside an EJB container.
setConnectionManagerShared(boolean),
HttpClientConnectionManager.closeExpiredConnections()@Deprecated public final HttpClientBuilder evictIdleConnections(Long maxIdleTime, TimeUnit maxIdleTimeUnit)
evictIdleConnections(long, TimeUnit)
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 configuted to use a shared connection manager.
Please note this method may not be used when the instance of HttpClient is created inside an EJB container.
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.maxIdleTimeUnit - time unit for the above parameter.setConnectionManagerShared(boolean),
HttpClientConnectionManager.closeExpiredConnections()public final HttpClientBuilder evictIdleConnections(long maxIdleTime, TimeUnit maxIdleTimeUnit)
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 configuted to use a shared connection manager.
Please note this method may not be used when the instance of HttpClient is created inside an EJB container.
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.maxIdleTimeUnit - time unit for the above parameter.setConnectionManagerShared(boolean),
HttpClientConnectionManager.closeExpiredConnections()public final HttpClientBuilder disableDefaultUserAgent()
protected ClientExecChain createMainExec(org.apache.http.protocol.HttpRequestExecutor requestExec, HttpClientConnectionManager connManager, org.apache.http.ConnectionReuseStrategy reuseStrategy, ConnectionKeepAliveStrategy keepAliveStrategy, org.apache.http.protocol.HttpProcessor proxyHttpProcessor, AuthenticationStrategy targetAuthStrategy, AuthenticationStrategy proxyAuthStrategy, UserTokenHandler userTokenHandler)
ClientExecChain to be used as a main exec.
Default implementation produces an instance of MainClientExec
For internal use.
protected ClientExecChain decorateMainExec(ClientExecChain mainExec)
protected ClientExecChain decorateProtocolExec(ClientExecChain protocolExec)
protected void addCloseable(Closeable closeable)
public CloseableHttpClient build()
Copyright © 1999–2022 The Apache Software Foundation. All rights reserved.