public class ClientBuilder extends Object
CloseableHttpClient
.
HttpClients are heavy-weight objects that manage the client-side communication infrastructure.
Initialization as well as disposal of a CloseableHttpClient
instance may be a rather expensive operation.
It is therefore advised to construct only a small number of CloseableHttpClient
instances in the application.
Modifier and Type | Method and Description |
---|---|
ClientBuilder |
addContentType(org.apache.http.entity.ContentType contentType)
Sets content type to header
|
ClientBuilder |
addDefaultHeader(org.apache.http.Header header)
Header needs to be the same for all requests which go through the built CloseableHttpClient
|
ClientBuilder |
addDefaultHeader(String name,
String value)
Header needs to be the same for all requests which go through the built CloseableHttpClient
|
ClientBuilder |
addDefaultHeaders(Collection<? extends org.apache.http.Header> headers)
Headers need to be the same for all requests which go through the built CloseableHttpClient
|
ClientBuilder |
addDefaultHeaders(org.apache.http.Header... headers)
Headers need to be the same for all requests which go through the built CloseableHttpClient
|
ClientBuilder |
addDefaultRequestConfigCustomizer(Consumer<org.apache.http.client.config.RequestConfig.Builder> defaultRequestConfigBuilderConsumer) |
ClientBuilder |
addHttpClientCustomizer(Consumer<org.apache.http.impl.client.HttpClientBuilder> httpClientCustomizer)
The method takes the
Consumer instance which gives the HttpClientBuilder instance to customize
the CloseableHttpClient before the http-request is built |
org.apache.http.impl.client.CloseableHttpClient |
build()
Build CloseableHttpClient
|
ClientBuilder |
connectionRequestTimeout(int connectionRequestTimeout)
The timeout in milliseconds used when requesting a connection
from the connection manager.
|
static ClientBuilder |
create() |
ClientBuilder |
enableDefaultRedirectStrategy()
By default, only GET requests resulting in a redirect are automatically followed.
|
ClientBuilder |
enableLaxRedirectStrategy()
By default, only GET requests resulting in a redirect are automatically followed.
|
ClientBuilder |
hostnameVerifier(HostnameVerifier hostnameVerifier)
Sets
HostnameVerifier |
ClientBuilder |
proxy(org.apache.http.HttpHost proxy)
Added proxy host.
|
ClientBuilder |
proxy(String host,
int port) |
ClientBuilder |
proxy(URI proxyUri)
Added proxy by proxyUri.
|
ClientBuilder |
redirectStrategy(org.apache.http.client.RedirectStrategy redirectStrategy)
By default disabled.
|
ClientBuilder |
setConnectTimeout(int connectTimeout)
Determines the timeout in milliseconds until a connection is established.
|
ClientBuilder |
setDefaultMaxPoolSizePerRoute(int defaultMaxPoolSizePerRoute) |
ClientBuilder |
setMaxPoolSize(int maxPoolSize) |
ClientBuilder |
setMaxPoolSizePerRoute(org.apache.http.HttpHost httpHost,
int maxRoutePoolSize)
Set the connection pool default max size of concurrent connections to a specific route
|
ClientBuilder |
socketTimeOut(int socketTimeOut)
Defines the socket timeout (
SO_TIMEOUT ) in milliseconds,
which is the timeout for waiting for data or, put differently,
a maximum period inactivity between two consecutive data packets). |
ClientBuilder |
sslContext(SSLContext sslContext)
Sets
SSLContext |
ClientBuilder |
trustAllCertificates()
Accept all certificates
|
ClientBuilder |
trustAllHosts()
Accept all hosts
|
ClientBuilder |
useDefaultProxy()
Instruct HttpClient to use the standard JRE proxy selector to obtain proxy.
|
public ClientBuilder setConnectTimeout(int connectTimeout)
A timeout value of zero is interpreted as an infinite timeout. A negative value is interpreted as undefined (system default).
Default: 5000ms
connectTimeout
- The Connection Timeout (http.connection.timeout) – the time to establish the connection with the remote host.RequestConfig.Builder#setConnectTimeout
public ClientBuilder socketTimeOut(int socketTimeOut)
SO_TIMEOUT
) in milliseconds,
which is the timeout for waiting for data or, put differently,
a maximum period inactivity between two consecutive data packets).
A timeout value of zero is interpreted as an infinite timeout. A negative value is interpreted as undefined (system default).
Default: 30000ms
socketTimeOut
- The Socket Timeout (http.socket.timeout) – the time waiting for data – after the connection was established;
maximum time of inactivity between two data packets.RequestConfig.Builder#setSocketTimeout
public ClientBuilder connectionRequestTimeout(int connectionRequestTimeout)
A timeout value of zero is interpreted as an infinite timeout. A negative value is interpreted as undefined (system default).
Default: 30000ms
Note: Can be overridden by addDefaultRequestConfigCustomizer(java.util.function.Consumer<org.apache.http.client.config.RequestConfig.Builder>)
connectionRequestTimeout
- The Connection Manager Timeout (http.connection-manager.timeout) –
the time to wait for a connection from the connection manager/pool.
By default 30000msRequestConfig.Builder#setConnectionRequestTimeout
,
addDefaultRequestConfigCustomizer(java.util.function.Consumer<org.apache.http.client.config.RequestConfig.Builder>)
public ClientBuilder addDefaultRequestConfigCustomizer(Consumer<org.apache.http.client.config.RequestConfig.Builder> defaultRequestConfigBuilderConsumer)
defaultRequestConfigBuilderConsumer
- the consumer instance which provides RequestConfig.Builder
to customize default request configpublic ClientBuilder addHttpClientCustomizer(Consumer<org.apache.http.impl.client.HttpClientBuilder> httpClientCustomizer)
Consumer
instance which gives the HttpClientBuilder
instance to customize
the CloseableHttpClient
before the http-request is builthttpClientCustomizer
- consumer instancepublic ClientBuilder setMaxPoolSize(int maxPoolSize)
maxPoolSize
- see documentation of HostPoolConfig.setMaxPoolSize(int)
public ClientBuilder setDefaultMaxPoolSizePerRoute(int defaultMaxPoolSizePerRoute)
defaultMaxPoolSizePerRoute
- see documentation of HostPoolConfig.setDefaultMaxPoolSizePerRoute(int)
public ClientBuilder setMaxPoolSizePerRoute(org.apache.http.HttpHost httpHost, int maxRoutePoolSize)
httpHost
- httpHostmaxRoutePoolSize
- maxRoutePoolSizepublic ClientBuilder enableLaxRedirectStrategy()
If the 301 status code is received in response to a request other than GET or HEAD, the user agent MUST NOT automatically redirect the request unless it can be confirmed by the user, since this might change the conditions under which the request was issued.
By default disabled.
LaxRedirectStrategy
public ClientBuilder enableDefaultRedirectStrategy()
If the 301 status code is received in response to a request other than GET or HEAD, the user agent MUST NOT automatically redirect the request unless it can be confirmed by the user, since this might change the conditions under which the request was issued.
By default disabled.
DefaultRedirectStrategy
public ClientBuilder redirectStrategy(org.apache.http.client.RedirectStrategy redirectStrategy)
By default disabled.
redirectStrategy
- RedirectStrategy instanceRedirectStrategy
public ClientBuilder addDefaultHeader(String name, String value)
name
- name of header. Can't be nullvalue
- value of headerpublic ClientBuilder addDefaultHeader(org.apache.http.Header header)
header
- header instance. Can't be nullpublic ClientBuilder addDefaultHeaders(org.apache.http.Header... headers)
headers
- varargs of headers. Can't be nullpublic ClientBuilder addDefaultHeaders(Collection<? extends org.apache.http.Header> headers)
headers
- collections of headerspublic ClientBuilder addContentType(org.apache.http.entity.ContentType contentType)
contentType
- content type of request headerpublic ClientBuilder proxy(org.apache.http.HttpHost proxy)
useDefaultProxy()
will be ignoredproxy
- HttpHost
instance to proxypublic ClientBuilder proxy(URI proxyUri)
useDefaultProxy()
will be ignored.proxyUri
- URI
instance to proxypublic ClientBuilder proxy(String host, int port)
host
- host of proxyport
- port of proxypublic ClientBuilder useDefaultProxy()
public ClientBuilder sslContext(SSLContext sslContext)
SSLContext
sslContext
- SSLContext instancepublic ClientBuilder hostnameVerifier(HostnameVerifier hostnameVerifier)
HostnameVerifier
hostnameVerifier
- HostnameVerifier instancepublic ClientBuilder trustAllCertificates()
HttpRequestBuildException
- when can't build ssl.public ClientBuilder trustAllHosts()
public org.apache.http.impl.client.CloseableHttpClient build()
CloseableHttpClient
instance by build parameterspublic static ClientBuilder create()
Copyright © 2020. All rights reserved.