public static final class OkHttpClient.Builder extends Object
| Constructor and Description |
|---|
Builder()
|
| Modifier and Type | Method and Description |
|---|---|
OkHttpClient |
addInterceptor(Interceptor
|
OkHttpClient |
addNetworkInterceptor(Interceptor
|
OkHttpClient |
authenticator(Authenticator
Sets the authenticator used to respond to challenges from origin servers.
|
OkHttpClient |
build()
|
OkHttpClient |
cache(Cache
|
OkHttpClient |
certificatePinner(CertificatePinner
Sets the certificate pinner that constrains which certificates are trusted.
|
OkHttpClient |
connectionPool(ConnectionPool
Sets the connection pool used to recycle HTTP and HTTPS connections.
|
OkHttpClient |
connectionSpecs(List
|
OkHttpClient |
connectTimeout(long timeout, TimeUnit
Sets the default connect timeout for new connections.
|
OkHttpClient |
cookieJar(CookieJar
Sets the handler that can accept cookies from incoming HTTP responses and provides cookies to outgoing HTTP requests.
|
OkHttpClient |
dispatcher(Dispatcher
Sets the dispatcher used to set policy and execute asynchronous requests.
|
OkHttpClient |
dns(Dns
Sets the DNS service used to lookup IP addresses for hostnames.
|
OkHttpClient |
followRedirects(boolean followRedirects)
Configure this client to follow redirects.
|
OkHttpClient |
followSslRedirects(boolean followProtocolRedirects)
Configure this client to follow redirects from HTTPS to HTTP and from HTTP to HTTPS.
|
OkHttpClient |
hostnameVerifier(HostnameVerifier
Sets the verifier used to confirm that response certificates apply to requested hostnames for HTTPS connections.
|
List |
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 |
networkInterceptors()
Returns a modifiable list of interceptors that observe a single network request and response.
|
OkHttpClient |
protocols(List
Configure the protocols used by this client to communicate with remote servers.
|
OkHttpClient |
proxy(Proxy
Sets the HTTP proxy that will be used by connections created by this client.
|
OkHttpClient |
proxyAuthenticator(Authenticator
Sets the authenticator used to respond to challenges from proxy servers.
|
OkHttpClient |
proxySelector(ProxySelector
Sets the proxy selection policy to be used if no
proxy is specified explicitly.
|
OkHttpClient |
readTimeout(long timeout, TimeUnit
Sets the default read timeout for new connections.
|
OkHttpClient |
retryOnConnectionFailure(boolean retryOnConnectionFailure)
Configure this client to retry or not when a connectivity problem is encountered.
|
OkHttpClient |
socketFactory(SocketFactory
Sets the socket factory used to create connections.
|
OkHttpClient |
sslSocketFactory(SSLSocketFactory
Sets the socket factory used to secure HTTPS connections.
|
OkHttpClient |
writeTimeout(long timeout, TimeUnit
Sets the default write timeout for new connections.
|
public OkHttpClient.Builder connectTimeout(long timeout, TimeUnit unit)
Integer.MAX_VALUE when converted to milliseconds.
public OkHttpClient.Builder readTimeout(long timeout, TimeUnit unit)
Integer.MAX_VALUE when converted to milliseconds.
public OkHttpClient.Builder writeTimeout(long timeout, TimeUnit unit)
Integer.MAX_VALUE when converted to milliseconds.
public OkHttpClient.Builder proxy(Proxy proxy)
proxySelector, which is only honored when this proxy is null (which it is by default). To disable proxy use completely, call
setProxy(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(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)
If unset, a lazily created SSL socket factory will be used.
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 is not supported in this set. Requests are initiated with HTTP/1.1 only. If the server responds with HTTP/1.0, that will be exposed by Response.
protocols - the protocols to use, in order of preference. The list must contain
Protocol.HTTP_1_1 . It 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 OkHttpClientbuild()