Class OkHttpClient

java.lang.Object
com.squareup.okhttp.OkHttpClient
All Implemented Interfaces:
URLStreamHandlerFactory

public final class OkHttpClient
extends Object
implements URLStreamHandlerFactory
Configures and creates HTTP connections.
  • Constructor Details

    • OkHttpClient

      public OkHttpClient()
  • Method Details

    • setConnectTimeout

      public void setConnectTimeout​(long timeout, TimeUnit unit)
      Sets the default connect timeout for new connections. A value of 0 means no timeout.
      See Also:
      URLConnection.setConnectTimeout(int)
    • getConnectTimeout

      public int getConnectTimeout()
      Default connect timeout (in milliseconds).
    • setReadTimeout

      public void setReadTimeout​(long timeout, TimeUnit unit)
      Sets the default read timeout for new connections. A value of 0 means no timeout.
      See Also:
      URLConnection.setReadTimeout(int)
    • getReadTimeout

      public int getReadTimeout()
      Default read timeout (in milliseconds).
    • setProxy

      public OkHttpClient setProxy​(Proxy proxy)
      Sets the HTTP proxy that will be used by connections created by this client. This takes precedence over 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).
    • getProxy

      public Proxy getProxy()
    • setProxySelector

      public OkHttpClient setProxySelector​(ProxySelector proxySelector)
      Sets the proxy selection policy to be used if no 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.

    • getProxySelector

      public ProxySelector getProxySelector()
    • setCookieHandler

      public OkHttpClient setCookieHandler​(CookieHandler cookieHandler)
      Sets the cookie handler to be used to read outgoing cookies and write incoming cookies.

      If unset, the system-wide default cookie handler will be used.

    • getCookieHandler

      public CookieHandler getCookieHandler()
    • setResponseCache

      public OkHttpClient setResponseCache​(ResponseCache responseCache)
      Sets the response cache to be used to read and write cached responses.

      If unset, the system-wide default response cache will be used.

    • getResponseCache

      public ResponseCache getResponseCache()
    • getOkResponseCache

      public OkResponseCache getOkResponseCache()
    • setSslSocketFactory

      public OkHttpClient setSslSocketFactory​(SSLSocketFactory sslSocketFactory)
      Sets the socket factory used to secure HTTPS connections.

      If unset, the system-wide default SSL socket factory will be used.

    • getSslSocketFactory

      public SSLSocketFactory getSslSocketFactory()
    • setHostnameVerifier

      public OkHttpClient setHostnameVerifier​(HostnameVerifier hostnameVerifier)
      Sets the verifier used to confirm that response certificates apply to requested hostnames for HTTPS connections.

      If unset, the system-wide default hostname verifier will be used.

    • getHostnameVerifier

      public HostnameVerifier getHostnameVerifier()
    • setAuthenticator

      public OkHttpClient setAuthenticator​(OkAuthenticator authenticator)
      Sets the authenticator used to respond to challenges from the remote web server or proxy server.

      If unset, the system-wide default authenticator will be used.

    • getAuthenticator

      public OkAuthenticator getAuthenticator()
    • setConnectionPool

      public OkHttpClient setConnectionPool​(ConnectionPool connectionPool)
      Sets the connection pool used to recycle HTTP and HTTPS connections.

      If unset, the system-wide default connection pool will be used.

    • getConnectionPool

      public ConnectionPool getConnectionPool()
    • setFollowProtocolRedirects

      public OkHttpClient setFollowProtocolRedirects​(boolean followProtocolRedirects)
      Configure this client to follow redirects from HTTPS to HTTP and from HTTP to HTTPS.

      If unset, protocol redirects will be followed. This is different than the built-in HttpURLConnection's default.

    • getFollowProtocolRedirects

      public boolean getFollowProtocolRedirects()
    • getRoutesDatabase

      public RouteDatabase getRoutesDatabase()
    • setTransports

      public OkHttpClient setTransports​(List<String> transports)
      Configure the transports used by this client to communicate with remote servers. By default this client will prefer the most efficient transport available, falling back to more ubiquitous transports. Applications should only call this method to avoid specific compatibility problems, such as web servers that behave incorrectly when SPDY is enabled.

      The following transports are currently supported:

      This is an evolving set. Future releases may drop support for transitional transports (like spdy/3), in favor of their successors (spdy/4 or http/2.0). The http/1.1 transport will never be dropped.

      If multiple protocols are specified, NPN will be used to negotiate a transport. Future releases may use another mechanism (such as ALPN) to negotiate a transport.

      Parameters:
      transports - the transports to use, in order of preference. The list must contain "http/1.1". It must not contain null.
    • getTransports

      public List<String> getTransports()
    • enqueue

      public void enqueue​(Request request, Response.Receiver responseReceiver)
      Schedules request to be executed.
    • cancel

      public void cancel​(Object tag)
      Cancels all scheduled tasks tagged with tag. Requests that are already in flight might not be canceled.
    • open

      public HttpURLConnection open​(URL url)
    • createURLStreamHandler

      public URLStreamHandler createURLStreamHandler​(String protocol)
      Creates a URLStreamHandler as a URL.setURLStreamHandlerFactory(java.net.URLStreamHandlerFactory).

      This code configures OkHttp to handle all HTTP and HTTPS connections created with URL.openConnection():

         
      
         OkHttpClient okHttpClient = new OkHttpClient();
         URL.setURLStreamHandlerFactory(okHttpClient);
       
      Specified by:
      createURLStreamHandler in interface URLStreamHandlerFactory
      Parameters:
      protocol - the protocol for which a handler is needed.
      Returns:
      the created handler.