public class OkHttpClient
extends java.lang.Object
implements java.lang.Cloneable
Instances of OkHttpClient are intended to be fully configured before they're
shared - once shared they should be treated as immutable and can safely be used
to concurrently open new connections. If required, threads can call
clone()
to make a shallow copy of the OkHttpClient that can be
safely modified with further configuration changes.
Constructor and Description |
---|
OkHttpClient() |
Modifier and Type | Method and Description |
---|---|
OkHttpClient |
cancel(java.lang.Object tag)
Cancels all scheduled or in-flight calls tagged with
tag . |
OkHttpClient |
clone()
Returns a shallow copy of this OkHttpClient.
|
Authenticator |
getAuthenticator() |
Cache |
getCache() |
CertificatePinner |
getCertificatePinner() |
ConnectionPool |
getConnectionPool() |
java.util.List<ConnectionSpec> |
getConnectionSpecs() |
int |
getConnectTimeout()
Default connect timeout (in milliseconds).
|
java.net.CookieHandler |
getCookieHandler() |
Dispatcher |
getDispatcher() |
boolean |
getFollowRedirects() |
boolean |
getFollowSslRedirects() |
javax.net.ssl.HostnameVerifier |
getHostnameVerifier() |
java.util.List<Protocol> |
getProtocols() |
java.net.Proxy |
getProxy() |
java.net.ProxySelector |
getProxySelector() |
int |
getReadTimeout()
Default read timeout (in milliseconds).
|
javax.net.SocketFactory |
getSocketFactory() |
javax.net.ssl.SSLSocketFactory |
getSslSocketFactory() |
int |
getWriteTimeout()
Default write timeout (in milliseconds).
|
Call |
newCall(Request request)
Prepares the
request to be executed at some point in the future. |
OkHttpClient |
setAuthenticator(Authenticator authenticator)
Sets the authenticator used to respond to challenges from the remote web
server or proxy server.
|
OkHttpClient |
setCache(Cache cache) |
OkHttpClient |
setCertificatePinner(CertificatePinner certificatePinner)
Sets the certificate pinner that constrains which certificates are trusted.
|
OkHttpClient |
setConnectionPool(ConnectionPool connectionPool)
Sets the connection pool used to recycle HTTP and HTTPS connections.
|
OkHttpClient |
setConnectionSpecs(java.util.List<ConnectionSpec> connectionSpecs) |
void |
setConnectTimeout(long timeout,
java.util.concurrent.TimeUnit unit)
Sets the default connect timeout for new connections.
|
OkHttpClient |
setCookieHandler(java.net.CookieHandler cookieHandler)
Sets the cookie handler to be used to read outgoing cookies and write
incoming cookies.
|
OkHttpClient |
setDispatcher(Dispatcher dispatcher)
Sets the dispatcher used to set policy and execute asynchronous requests.
|
void |
setFollowRedirects(boolean followRedirects)
Configure this client to follow redirects.
|
OkHttpClient |
setFollowSslRedirects(boolean followProtocolRedirects)
Configure this client to follow redirects from HTTPS to HTTP and from HTTP
to HTTPS.
|
OkHttpClient |
setHostnameVerifier(javax.net.ssl.HostnameVerifier hostnameVerifier)
Sets the verifier used to confirm that response certificates apply to
requested hostnames for HTTPS connections.
|
OkHttpClient |
setProtocols(java.util.List<Protocol> protocols)
Configure the protocols used by this client to communicate with remote
servers.
|
OkHttpClient |
setProxy(java.net.Proxy proxy)
Sets the HTTP proxy that will be used by connections created by this
client.
|
OkHttpClient |
setProxySelector(java.net.ProxySelector proxySelector)
Sets the proxy selection policy to be used if no
proxy
is specified explicitly. |
void |
setReadTimeout(long timeout,
java.util.concurrent.TimeUnit unit)
Sets the default read timeout for new connections.
|
OkHttpClient |
setSocketFactory(javax.net.SocketFactory socketFactory)
Sets the socket factory used to create connections.
|
OkHttpClient |
setSslSocketFactory(javax.net.ssl.SSLSocketFactory sslSocketFactory)
Sets the socket factory used to secure HTTPS connections.
|
void |
setWriteTimeout(long timeout,
java.util.concurrent.TimeUnit unit)
Sets the default write timeout for new connections.
|
public final void setConnectTimeout(long timeout, java.util.concurrent.TimeUnit unit)
URLConnection.setConnectTimeout(int)
public final int getConnectTimeout()
public final void setReadTimeout(long timeout, java.util.concurrent.TimeUnit unit)
URLConnection.setReadTimeout(int)
public final int getReadTimeout()
public final void setWriteTimeout(long timeout, java.util.concurrent.TimeUnit unit)
public final int getWriteTimeout()
public final OkHttpClient setProxy(java.net.Proxy proxy)
setProxySelector(java.net.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 final java.net.Proxy getProxy()
public final OkHttpClient setProxySelector(java.net.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 final java.net.ProxySelector getProxySelector()
public final OkHttpClient setCookieHandler(java.net.CookieHandler cookieHandler)
If unset, the system-wide default
cookie handler will be used.
public final java.net.CookieHandler getCookieHandler()
public final OkHttpClient setCache(Cache cache)
public final Cache getCache()
public final OkHttpClient setSocketFactory(javax.net.SocketFactory socketFactory)
If unset, the system-wide default
socket factory will be used.
public final javax.net.SocketFactory getSocketFactory()
public final OkHttpClient setSslSocketFactory(javax.net.ssl.SSLSocketFactory sslSocketFactory)
If unset, a lazily created SSL socket factory will be used.
public final javax.net.ssl.SSLSocketFactory getSslSocketFactory()
public final OkHttpClient setHostnameVerifier(javax.net.ssl.HostnameVerifier hostnameVerifier)
If unset, a default hostname verifier will be used.
public final javax.net.ssl.HostnameVerifier getHostnameVerifier()
public final OkHttpClient setCertificatePinner(CertificatePinner certificatePinner)
SSL socket factory
to establish trust. Pinning certificates avoids the
need to trust certificate authorities.public final CertificatePinner getCertificatePinner()
public final OkHttpClient setAuthenticator(Authenticator authenticator)
If unset, the system-wide default
authenticator will be used.
public final Authenticator getAuthenticator()
public final OkHttpClient setConnectionPool(ConnectionPool connectionPool)
If unset, the system-wide
default
connection pool will be used.
public final ConnectionPool getConnectionPool()
public final OkHttpClient setFollowSslRedirects(boolean followProtocolRedirects)
If unset, protocol redirects will be followed. This is different than
the built-in HttpURLConnection
's default.
public final boolean getFollowSslRedirects()
public final void setFollowRedirects(boolean followRedirects)
public final boolean getFollowRedirects()
public final OkHttpClient setDispatcher(Dispatcher dispatcher)
public final Dispatcher getDispatcher()
public final OkHttpClient setProtocols(java.util.List<Protocol> protocols)
The following protocols are currently supported:
This is an evolving set. Future releases may drop support for transitional protocols (like h2-14), in favor of their successors (h2). The http/1.1 transport will never be dropped.
If multiple protocols are specified, NPN or ALPN will be used to negotiate a transport.
Protocol.HTTP_1_0
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.protocol()
.
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 final java.util.List<Protocol> getProtocols()
public final OkHttpClient setConnectionSpecs(java.util.List<ConnectionSpec> connectionSpecs)
public final java.util.List<ConnectionSpec> getConnectionSpecs()
public Call newCall(Request request)
request
to be executed at some point in the future.public OkHttpClient cancel(java.lang.Object tag)
tag
. Requests
that are already complete cannot be canceled.public final OkHttpClient clone()
clone
in class java.lang.Object
Copyright © 2014. All Rights Reserved.