public static final class OkHttpClient.Builder extends Object
Constructor and Description |
---|
Builder() |
Modifier and Type | Method and Description |
---|---|
OkHttpClient.Builder |
addInterceptor(Interceptor interceptor) |
OkHttpClient.Builder |
addNetworkInterceptor(Interceptor interceptor) |
OkHttpClient.Builder |
authenticator(Authenticator authenticator)
Sets the authenticator used to respond to challenges from origin servers.
|
OkHttpClient |
build() |
OkHttpClient.Builder |
cache(Cache cache)
Sets the response cache to be used to read and write cached responses.
|
OkHttpClient.Builder |
callTimeout(java.time.Duration duration)
Sets the default timeout for complete calls.
|
OkHttpClient.Builder |
callTimeout(long timeout,
TimeUnit unit)
Sets the default timeout for complete calls.
|
OkHttpClient.Builder |
certificatePinner(CertificatePinner certificatePinner)
Sets the certificate pinner that constrains which certificates are trusted.
|
OkHttpClient.Builder |
connectionPool(ConnectionPool connectionPool)
Sets the connection pool used to recycle HTTP and HTTPS connections.
|
OkHttpClient.Builder |
connectionSpecs(List<ConnectionSpec> connectionSpecs) |
OkHttpClient.Builder |
connectTimeout(java.time.Duration duration)
Sets the default connect timeout for new connections.
|
OkHttpClient.Builder |
connectTimeout(long timeout,
TimeUnit unit)
Sets the default connect timeout for new connections.
|
OkHttpClient.Builder |
cookieJar(CookieJar cookieJar)
Sets the handler that can accept cookies from incoming HTTP responses and provides cookies to
outgoing HTTP requests.
|
OkHttpClient.Builder |
dispatcher(Dispatcher dispatcher)
Sets the dispatcher used to set policy and execute asynchronous requests.
|
OkHttpClient.Builder |
dns(Dns dns)
Sets the DNS service used to lookup IP addresses for hostnames.
|
OkHttpClient.Builder |
eventListener(EventListener eventListener)
Configure a single client scoped listener that will receive all analytic events
for this client.
|
OkHttpClient.Builder |
eventListenerFactory(EventListener.Factory eventListenerFactory)
Configure a factory to provide per-call scoped listeners that will receive analytic events
for this client.
|
OkHttpClient.Builder |
followRedirects(boolean followRedirects)
Configure this client to follow redirects.
|
OkHttpClient.Builder |
followSslRedirects(boolean followProtocolRedirects)
Configure this client to follow redirects from HTTPS to HTTP and from HTTP to HTTPS.
|
OkHttpClient.Builder |
hostnameVerifier(HostnameVerifier hostnameVerifier)
Sets the verifier used to confirm that response certificates apply to requested hostnames for
HTTPS connections.
|
List<Interceptor> |
interceptors()
Returns a modifiable list of interceptors that observe the full span of each call: from
before the connection is established (if any) until after the response source is selected
(either the origin server, cache, or both).
|
List<Interceptor> |
networkInterceptors()
Returns a modifiable list of interceptors that observe a single network request and response.
|
OkHttpClient.Builder |
pingInterval(java.time.Duration duration)
Sets the interval between HTTP/2 and web socket pings initiated by this client.
|
OkHttpClient.Builder |
pingInterval(long interval,
TimeUnit unit)
Sets the interval between HTTP/2 and web socket pings initiated by this client.
|
OkHttpClient.Builder |
protocols(List<Protocol> protocols)
Configure the protocols used by this client to communicate with remote servers.
|
OkHttpClient.Builder |
proxy(Proxy proxy)
Sets the HTTP proxy that will be used by connections created by this client.
|
OkHttpClient.Builder |
proxyAuthenticator(Authenticator proxyAuthenticator)
Sets the authenticator used to respond to challenges from proxy servers.
|
OkHttpClient.Builder |
proxySelector(ProxySelector proxySelector)
Sets the proxy selection policy to be used if no
proxy is specified
explicitly. |
OkHttpClient.Builder |
readTimeout(java.time.Duration duration)
Sets the default read timeout for new connections.
|
OkHttpClient.Builder |
readTimeout(long timeout,
TimeUnit unit)
Sets the default read timeout for new connections.
|
OkHttpClient.Builder |
retryOnConnectionFailure(boolean retryOnConnectionFailure)
Configure this client to retry or not when a connectivity problem is encountered.
|
OkHttpClient.Builder |
socketFactory(SocketFactory socketFactory)
Sets the socket factory used to create connections.
|
OkHttpClient.Builder |
sslSocketFactory(SSLSocketFactory sslSocketFactory)
Deprecated.
SSLSocketFactory does not expose its X509TrustManager , which is
a field that OkHttp needs to build a clean certificate chain. This method instead must
use reflection to extract the trust manager. Applications should prefer to call sslSocketFactory(SSLSocketFactory, X509TrustManager) , which avoids such reflection. |
OkHttpClient.Builder |
sslSocketFactory(SSLSocketFactory sslSocketFactory,
X509TrustManager trustManager)
Sets the socket factory and trust manager used to secure HTTPS connections.
|
OkHttpClient.Builder |
writeTimeout(java.time.Duration duration)
Sets the default write timeout for new connections.
|
OkHttpClient.Builder |
writeTimeout(long timeout,
TimeUnit unit)
Sets the default write timeout for new connections.
|
public OkHttpClient.Builder callTimeout(long timeout, TimeUnit unit)
Integer.MAX_VALUE
when converted to milliseconds.
The call timeout spans the entire call: resolving DNS, connecting, writing the request body, server processing, and reading the response body. If the call requires redirects or retries all must complete within one timeout period.
@IgnoreJRERequirement public OkHttpClient.Builder callTimeout(java.time.Duration duration)
Integer.MAX_VALUE
when converted to milliseconds.
The call timeout spans the entire call: resolving DNS, connecting, writing the request body, server processing, and reading the response body. If the call requires redirects or retries all must complete within one timeout period.
public OkHttpClient.Builder connectTimeout(long timeout, TimeUnit unit)
Integer.MAX_VALUE
when converted to
milliseconds.
The connect timeout is applied when connecting a TCP socket to the target host. The default value is 10 seconds.
@IgnoreJRERequirement public OkHttpClient.Builder connectTimeout(java.time.Duration duration)
Integer.MAX_VALUE
when converted to
milliseconds.
The connect timeout is applied when connecting a TCP socket to the target host. The default value is 10 seconds.
public OkHttpClient.Builder readTimeout(long timeout, TimeUnit unit)
Integer.MAX_VALUE
when converted to milliseconds.
The read timeout is applied to both the TCP socket and for individual read IO operations
including on Source
of the Response
. The default value is 10 seconds.
Socket.setSoTimeout(int)
,
Source.timeout()
@IgnoreJRERequirement public OkHttpClient.Builder readTimeout(java.time.Duration duration)
Integer.MAX_VALUE
when converted to milliseconds.
The read timeout is applied to both the TCP socket and for individual read IO operations
including on Source
of the Response
. The default value is 10 seconds.
Socket.setSoTimeout(int)
,
Source.timeout()
public OkHttpClient.Builder writeTimeout(long timeout, TimeUnit unit)
Integer.MAX_VALUE
when converted to milliseconds.
The write timeout is applied for individual write IO operations. The default value is 10 seconds.
Sink.timeout()
@IgnoreJRERequirement public OkHttpClient.Builder writeTimeout(java.time.Duration duration)
Integer.MAX_VALUE
when converted to milliseconds.
The write timeout is applied for individual write IO operations. The default value is 10 seconds.
Sink.timeout()
public OkHttpClient.Builder pingInterval(long interval, TimeUnit unit)
If the server does not respond to each ping with a pong within interval
, this
client will assume that connectivity has been lost. When this happens on a web socket the
connection is canceled and its listener is notified
of the failure. When it happens on an HTTP/2 connection the connection is closed and any
calls it is carrying will fail with an IOException.
The default value of 0 disables client-initiated pings.
@IgnoreJRERequirement public OkHttpClient.Builder pingInterval(java.time.Duration duration)
If the server does not respond to each ping with a pong within interval
, this
client will assume that connectivity has been lost. When this happens on a web socket the
connection is canceled and its listener is notified
of the failure. When it happens on an HTTP/2 connection the connection is closed and any
calls it is carrying will fail with an IOException.
The default value of 0 disables client-initiated pings.
public OkHttpClient.Builder proxy(@Nullable Proxy proxy)
proxySelector
, which is only honored when this proxy is null (which
it is by default). To disable proxy use completely, call proxy(Proxy.NO_PROXY)
.public OkHttpClient.Builder proxySelector(ProxySelector proxySelector)
proxy
is specified
explicitly. The proxy selector may return multiple proxies; in that case they will be tried
in sequence until a successful connection is established.
If unset, the system-wide default
proxy selector will
be used.
public OkHttpClient.Builder cookieJar(CookieJar cookieJar)
If unset, no cookies will be accepted nor provided.
public OkHttpClient.Builder cache(@Nullable Cache cache)
public OkHttpClient.Builder dns(Dns dns)
If unset, the system-wide default
DNS will be used.
public OkHttpClient.Builder socketFactory(SocketFactory socketFactory)
createSocket()
method to create unconnected sockets. Overriding
this method, e. g., allows the socket to be bound to a specific local address.
If unset, the system-wide default
socket factory will
be used.
public OkHttpClient.Builder sslSocketFactory(SSLSocketFactory sslSocketFactory)
SSLSocketFactory
does not expose its X509TrustManager
, which is
a field that OkHttp needs to build a clean certificate chain. This method instead must
use reflection to extract the trust manager. Applications should prefer to call sslSocketFactory(SSLSocketFactory, X509TrustManager)
, which avoids such reflection.public OkHttpClient.Builder sslSocketFactory(SSLSocketFactory sslSocketFactory, X509TrustManager trustManager)
Most applications should not call this method, and instead use the system defaults. Those classes include special optimizations that can be lost if the implementations are decorated.
If necessary, you can create and configure the defaults yourself with the following code:
TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(
TrustManagerFactory.getDefaultAlgorithm());
trustManagerFactory.init((KeyStore) null);
TrustManager[] trustManagers = trustManagerFactory.getTrustManagers();
if (trustManagers.length != 1 || !(trustManagers[0] instanceof X509TrustManager)) {
throw new IllegalStateException("Unexpected default trust managers:"
+ Arrays.toString(trustManagers));
}
X509TrustManager trustManager = (X509TrustManager) trustManagers[0];
SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, new TrustManager[] { trustManager }, null);
SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
OkHttpClient client = new OkHttpClient.Builder()
.sslSocketFactory(sslSocketFactory, trustManager)
.build();
public OkHttpClient.Builder hostnameVerifier(HostnameVerifier hostnameVerifier)
If unset, a default hostname verifier will be used.
public OkHttpClient.Builder certificatePinner(CertificatePinner certificatePinner)
SSL socket factory
to establish trust.
Pinning certificates avoids the need to trust certificate authorities.public OkHttpClient.Builder authenticator(Authenticator authenticator)
proxyAuthenticator
to set the authenticator for proxy servers.
If unset, the no authentication will be attempted.
public OkHttpClient.Builder proxyAuthenticator(Authenticator proxyAuthenticator)
authenticator
to set the authenticator for origin servers.
If unset, the no authentication will be attempted.
public OkHttpClient.Builder connectionPool(ConnectionPool connectionPool)
If unset, a new connection pool will be used.
public OkHttpClient.Builder followSslRedirects(boolean followProtocolRedirects)
If unset, protocol redirects will be followed. This is different than the built-in HttpURLConnection
's default.
public OkHttpClient.Builder followRedirects(boolean followRedirects)
public OkHttpClient.Builder retryOnConnectionFailure(boolean retryOnConnectionFailure)
ConnectionPool
reuses sockets
to decrease request latency, but these connections will occasionally time out.
ProxySelector
can be used to
attempt multiple proxy servers in sequence, eventually falling back to a direct
connection.
public OkHttpClient.Builder dispatcher(Dispatcher dispatcher)
public OkHttpClient.Builder protocols(List<Protocol> protocols)
The following protocols are currently supported:
This is an evolving set. Future releases include support for transitional protocols. The http/1.1 transport will never be dropped.
If multiple protocols are specified, ALPN will be used to negotiate a transport. Protocol negotiation is only attempted for HTTPS URLs.
Protocol.HTTP_1_0
is not supported in this set. Requests are initiated with HTTP/1.1
. If the server responds with HTTP/1.0
, that will be exposed by Response.protocol()
.
protocols
- the protocols to use, in order of preference. If the list contains Protocol.H2_PRIOR_KNOWLEDGE
then that must be the only protocol and HTTPS URLs will not
be supported. Otherwise the list must contain Protocol.HTTP_1_1
. The list must
not contain null or Protocol.HTTP_1_0
.public OkHttpClient.Builder connectionSpecs(List<ConnectionSpec> connectionSpecs)
public List<Interceptor> interceptors()
public OkHttpClient.Builder addInterceptor(Interceptor interceptor)
public List<Interceptor> networkInterceptors()
Interceptor.Chain.proceed(okhttp3.Request)
exactly once: it is an error
for a network interceptor to short-circuit or repeat a network request.public OkHttpClient.Builder addNetworkInterceptor(Interceptor interceptor)
public OkHttpClient.Builder eventListener(EventListener eventListener)
public OkHttpClient.Builder eventListenerFactory(EventListener.Factory eventListenerFactory)
public OkHttpClient build()
Copyright © 2018. All rights reserved.