public class HttpClientBuilder extends Object
HttpClient
objects configured with particular settings.
When using the single-arg constructor variant to wrap an existing instance of
HttpClientBuilder
, there are several caveats of which to be aware:
buildClient()
is called:
RequestConfig
ConnectionConfig
This is due to the unfortunate fact that the Apache builder does not currently provide accessor methods to obtain the default instances currently set on the builder. Therefore, if you need to set any default request or connection config parameters which are not exposed by this builder, then you must use the Apache builder directly and may not use this builder.
connectionDisregardTLSCertificate
is set to true
, then any value
previously set via the Apache builder's
HttpClientBuilder.setSSLSocketFactory(org.apache.http.conn.socket.LayeredConnectionSocketFactory)
will be
unconditionally overwritten.connectionProxyHost
, connectionProxyUsername
and
connectionProxyPassword
, then any value previously set via the Apache builder's
HttpClientBuilder.setDefaultCredentialsProvider(CredentialsProvider)
will be
unconditionally overwritten.HttpClientConnectionManager
is set on the Apache builder via
HttpClientBuilder.setConnectionManager(org.apache.http.conn.HttpClientConnectionManager)
,
this supersedes various other properties set on the Apache builder. This includes the following
instances/properties on the Apache builder:
SSLSocketFactory
(LayeredConnectionSocketFactory
)SSLContext
X509HostnameVerifier
SocketConfig
ConnectionConfig
maxConnTotal
maxConnPerRoute
Similarly, the following setters on this builder will become ineffective when a non-null connection manger is set on the Apache builder:
setConnectionDisregardTLSCertificate(boolean)
setSocketBufferSize(int)
setHttpContentCharSet(String)
Therefore, if you need to explicitly supply a connection manager instance to the Apache builder (for example in order
to be able to use IdleConnectionSweeper
), then you must supply these properties or instances directly to the
connection manager rather than to this builder or the Apache builder.
SSLSocketFactory
on the Apache builder will supersede
the following Apache builder properties:
SSLContext
X509HostnameVerifier
Modifier and Type | Field and Description |
---|---|
private org.apache.http.impl.client.HttpClientBuilder |
apacheBuilder
The Apache HttpClientBuilder 4.3+ instance over which to layer this builder.
|
private boolean |
connectionCloseAfterResponse
Whether to instruct the server to close the connection after it has sent its response.
|
private boolean |
connectionDisregardTLSCertificate
Whether the SSL/TLS certificates used by the responder should be ignored.
|
private String |
connectionProxyHost
Host name of the HTTP proxy server through which connections will be made.
|
private String |
connectionProxyPassword
Password used to connect to the HTTP proxy server.
|
private int |
connectionProxyPort
Port number of the HTTP proxy server through which connections will be made.
|
private String |
connectionProxyUsername
Username used to connect to the HTTP proxy server.
|
private boolean |
connectionStalecheck
Whether to check a connection for staleness before using.
|
private int |
connectionTimeout
Maximum length of time in milliseconds to wait for the connection to be established.
|
private String |
httpContentCharSet
Character set used for HTTP entity content.
|
private boolean |
httpFollowRedirects
Whether to follow HTTP redirects.
|
private org.apache.http.client.HttpRequestRetryHandler |
retryHandler
Handler which determines if a request should be retried.
|
private int |
socketBufferSize
Socket buffer size in bytes.
|
private InetAddress |
socketLocalAddress
Local IP address used when establishing connections.
|
private int |
socketTimeout
Maximum period inactivity between two consecutive data packets in milliseconds.
|
private org.apache.http.conn.socket.LayeredConnectionSocketFactory |
tlsSocketFactory
The TLS socket factory to use.
|
private String |
userAgent
Apache UserAgent.
|
Constructor and Description |
---|
HttpClientBuilder()
Constructor.
|
HttpClientBuilder(org.apache.http.impl.client.HttpClientBuilder builder)
Constructor.
|
Modifier and Type | Method and Description |
---|---|
org.apache.http.client.HttpClient |
buildClient()
Constructs an
HttpClient using the settings of this builder. |
protected void |
decorateApacheBuilder()
Decorate the Apache builder as determined by this builder's parameters.
|
protected org.apache.http.impl.client.HttpClientBuilder |
getApacheBuilder()
Get the Apache
HttpClientBuilder instance over which this builder will be
layered. |
String |
getConnectionProxyHost()
Gets the hostname of the default proxy used when making connection.
|
String |
getConnectionProxyPassword()
Gets the password used when authenticating to the proxy.
|
int |
getConnectionProxyPort()
Gets the port of the default proxy used when making connection.
|
String |
getConnectionProxyUsername()
Gets the username to use when authenticating to the proxy.
|
int |
getConnectionTimeout()
Gets the maximum length of time in milliseconds to wait for the connection to be established.
|
String |
getHttpContentCharSet()
Gets the character set used with the HTTP entity (body).
|
org.apache.http.client.HttpRequestRetryHandler |
getHttpRequestRetryHandler()
Get the handler which determines if a request should be retried.
|
int |
getSocketBufferSize()
Gets the size of the socket buffer, in bytes, used for request/response buffering.
|
InetAddress |
getSocketLocalAddress()
Gets the local IP address used when making requests.
|
int |
getSocketTimeout()
Gets the maximum period inactivity between two consecutive data packets in milliseconds.
|
org.apache.http.conn.socket.LayeredConnectionSocketFactory |
getTLSSocketFactory()
Get the TLS socket factory to use.
|
String |
getUserAgent()
Gets user agent.
|
boolean |
isConnectionCloseAfterResponse()
Gets whether to instruct the server to close the connection after it has sent its response.
|
boolean |
isConnectionDisregardTLSCertificate()
Gets whether the responder's SSL/TLS certificate should be ignored.
|
boolean |
isConnectionStalecheck()
Gets whether reused connections are checked if they are closed before being used by the client.
|
boolean |
isHttpFollowRedirects()
Gets whether HTTP redirects will be followed.
|
void |
resetDefaults()
Resets all builder parameters to their defaults.
|
void |
setConnectionCloseAfterResponse(boolean close)
Sets whether to instruct the server to close the connection after it has sent its response.
|
void |
setConnectionDisregardTLSCertificate(boolean disregard)
Sets whether the responder's SSL/TLS certificate should be ignored.
|
void |
setConnectionProxyHost(String host)
Sets the hostname of the default proxy used when making connection.
|
void |
setConnectionProxyPassword(String password)
Sets the password used when authenticating to the proxy.
|
void |
setConnectionProxyPort(int port)
Sets the port of the default proxy used when making connection.
|
void |
setConnectionProxyUsername(String usename)
Sets the username to use when authenticating to the proxy.
|
void |
setConnectionStalecheck(boolean check)
Sets whether reused connections are checked if they are closed before being used by the client.
|
void |
setConnectionTimeout(int timeout)
Sets the maximum length of time in milliseconds to wait for the connection to be established.
|
void |
setHttpContentCharSet(String charSet)
Sets the character set used with the HTTP entity (body).
|
void |
setHttpFollowRedirects(boolean followRedirects)
Gets whether HTTP redirects will be followed.
|
void |
setHttpRequestRetryHandler(org.apache.http.client.HttpRequestRetryHandler handler)
Set the handler which determines if a request should be retried.
|
void |
setSocketBufferSize(int size)
Sets size of the socket buffer, in bytes, used for request/response buffering.
|
void |
setSocketLocalAddress(InetAddress address)
Sets the local IP address used when making requests.
|
void |
setSocketLocalAddress(String ipOrHost)
Sets the local IP address used when making requests.
|
void |
setSocketTimeout(int timeout)
Sets the maximum period inactivity between two consecutive data packets in milliseconds.
|
void |
setTLSSocketFactory(org.apache.http.conn.socket.LayeredConnectionSocketFactory factory)
Set the TLS socket factory to use.
|
void |
setUserAgent(String what)
Sets user agent.
|
private InetAddress socketLocalAddress
private int socketTimeout
private int socketBufferSize
private int connectionTimeout
private boolean connectionDisregardTLSCertificate
@Nullable private org.apache.http.conn.socket.LayeredConnectionSocketFactory tlsSocketFactory
private boolean connectionCloseAfterResponse
private boolean connectionStalecheck
@Nullable private String connectionProxyHost
private int connectionProxyPort
@Nullable private String connectionProxyUsername
@Nullable private String connectionProxyPassword
private boolean httpFollowRedirects
@Nullable private String httpContentCharSet
@Nullable private org.apache.http.client.HttpRequestRetryHandler retryHandler
private org.apache.http.impl.client.HttpClientBuilder apacheBuilder
public HttpClientBuilder()
public HttpClientBuilder(@Nonnull org.apache.http.impl.client.HttpClientBuilder builder)
builder
- the Apache HttpClientBuilder 4.3+ instance over which to layer this builderpublic void resetDefaults()
public InetAddress getSocketLocalAddress()
public void setSocketLocalAddress(InetAddress address)
address
- local IP address used when making requestspublic void setSocketLocalAddress(String ipOrHost) throws UnknownHostException
ipOrHost
- IP address or hostname, never nullUnknownHostException
- thrown if the given IP or hostname can not be resolvedpublic int getSocketTimeout()
public void setSocketTimeout(int timeout)
timeout
- maximum period inactivity between two consecutive data packets in millisecondspublic int getSocketBufferSize()
public void setSocketBufferSize(int size)
size
- size of the socket buffer, in bytes, used for request/response buffering; must be greater than 0public int getConnectionTimeout()
public void setConnectionTimeout(int timeout)
timeout
- maximum length of time in milliseconds to wait for the connection to be establishedpublic boolean isConnectionDisregardTLSCertificate()
This flag is overridden and ignored if a custom TLS socket factory is specified via
setTLSSocketFactory(org.apache.http.conn.socket.LayeredConnectionSocketFactory)
.
public void setConnectionDisregardTLSCertificate(boolean disregard)
This flag is overridden and ignored if a custom TLS socket factory is specified via
setTLSSocketFactory(org.apache.http.conn.socket.LayeredConnectionSocketFactory)
.
disregard
- whether the responder's SSL/TLS certificate should be ignored@Nullable public org.apache.http.conn.socket.LayeredConnectionSocketFactory getTLSSocketFactory()
public void setTLSSocketFactory(@Nullable org.apache.http.conn.socket.LayeredConnectionSocketFactory factory)
factory
- the new socket factory, may be nullpublic boolean isConnectionCloseAfterResponse()
public void setConnectionCloseAfterResponse(boolean close)
close
- whether to instruct the server to close the connection after it has sent its responsepublic boolean isConnectionStalecheck()
public void setConnectionStalecheck(boolean check)
check
- whether reused connections are checked if they are closed before being used by the client@Nullable public String getConnectionProxyHost()
public void setConnectionProxyHost(@Nullable String host)
host
- hostname of the default proxy used when making connectionpublic int getConnectionProxyPort()
public void setConnectionProxyPort(int port)
port
- port of the default proxy used when making connection; must be greater than 0 and less than 65536@Nullable public String getConnectionProxyUsername()
public void setConnectionProxyUsername(@Nullable String usename)
usename
- username to use when authenticating to the proxy; may be null@Nullable public String getConnectionProxyPassword()
public void setConnectionProxyPassword(@Nullable String password)
password
- password used when authenticating to the proxy; may be nullpublic boolean isHttpFollowRedirects()
public void setHttpFollowRedirects(boolean followRedirects)
followRedirects
- true if redirects are followed, false otherwise@Nullable public String getHttpContentCharSet()
public void setHttpContentCharSet(@Nullable String charSet)
charSet
- character set used with the HTTP entity (body)public void setUserAgent(@Nullable String what)
what
- what to set. If this is null Apache will use the default.@Nullable public org.apache.http.client.HttpRequestRetryHandler getHttpRequestRetryHandler()
public void setHttpRequestRetryHandler(@Nullable org.apache.http.client.HttpRequestRetryHandler handler)
handler
- handler which determines if a request should be retriedpublic org.apache.http.client.HttpClient buildClient() throws Exception
HttpClient
using the settings of this builder.Exception
- if there is any problem building the new client instanceprotected void decorateApacheBuilder() throws Exception
Exception
- if there is a problem decorating the Apache builderprotected org.apache.http.impl.client.HttpClientBuilder getApacheBuilder()
HttpClientBuilder
instance over which this builder will be
layered. Subclasses may override to return a specialized subclass.Copyright © 1999–2018. All rights reserved.