okhttp / okhttp3 / OkHttpClient / Builder

Builder

class Builder

Constructors

<init>

Builder()

Functions

addInterceptor

fun addInterceptor(interceptor: Interceptor): OkHttpClient.Builder
fun addInterceptor(interceptor: (chain: Interceptor.Chain) -> Response): OkHttpClient.Builder

addNetworkInterceptor

fun addNetworkInterceptor(interceptor: Interceptor): OkHttpClient.Builder
fun addNetworkInterceptor(interceptor: (chain: Interceptor.Chain) -> Response): OkHttpClient.Builder

authenticator

fun authenticator(authenticator: Authenticator): OkHttpClient.Builder

Sets the authenticator used to respond to challenges from origin servers. Use proxyAuthenticator to set the authenticator for proxy servers.

build

fun build(): OkHttpClient

cache

fun cache(cache: Cache?): OkHttpClient.Builder

Sets the response cache to be used to read and write cached responses.

callTimeout

fun callTimeout(timeout: Long, unit: TimeUnit): OkHttpClient.Builder
fun callTimeout(duration: Duration): OkHttpClient.Builder

Sets the default timeout for complete calls. A value of 0 means no timeout, otherwise values must be between 1 and Integer.MAX_VALUE when converted to milliseconds.

certificatePinner

fun certificatePinner(certificatePinner: CertificatePinner): OkHttpClient.Builder

Sets the certificate pinner that constrains which certificates are trusted. By default HTTPS connections rely on only the SSL socket factory to establish trust. Pinning certificates avoids the need to trust certificate authorities.

connectionPool

fun connectionPool(connectionPool: ConnectionPool): OkHttpClient.Builder

Sets the connection pool used to recycle HTTP and HTTPS connections.

connectionSpecs

fun connectionSpecs(connectionSpecs: List<ConnectionSpec>): OkHttpClient.Builder

connectTimeout

fun connectTimeout(timeout: Long, unit: TimeUnit): OkHttpClient.Builder
fun connectTimeout(duration: Duration): OkHttpClient.Builder

Sets the default connect timeout for new connections. A value of 0 means no timeout, otherwise values must be between 1 and Integer.MAX_VALUE when converted to milliseconds.

cookieJar

fun cookieJar(cookieJar: CookieJar): OkHttpClient.Builder

Sets the handler that can accept cookies from incoming HTTP responses and provides cookies to outgoing HTTP requests.

dispatcher

fun dispatcher(dispatcher: Dispatcher): OkHttpClient.Builder

Sets the dispatcher used to set policy and execute asynchronous requests. Must not be null.

dns

fun dns(dns: Dns): OkHttpClient.Builder

Sets the DNS service used to lookup IP addresses for hostnames.

eventListener

fun eventListener(eventListener: EventListener): OkHttpClient.Builder

Configure a single client scoped listener that will receive all analytic events for this client.

eventListenerFactory

fun eventListenerFactory(eventListenerFactory: EventListener.Factory): OkHttpClient.Builder

Configure a factory to provide per-call scoped listeners that will receive analytic events for this client.

fun eventListenerFactory(block: (call: Call) -> EventListener): OkHttpClient.Builder

followRedirects

fun followRedirects(followRedirects: Boolean): OkHttpClient.Builder

Configure this client to follow redirects. If unset, redirects will be followed.

followSslRedirects

fun followSslRedirects(followProtocolRedirects: Boolean): OkHttpClient.Builder

Configure this client to follow redirects from HTTPS to HTTP and from HTTP to HTTPS.

hostnameVerifier

fun hostnameVerifier(hostnameVerifier: HostnameVerifier): OkHttpClient.Builder

Sets the verifier used to confirm that response certificates apply to requested hostnames for HTTPS connections.

interceptors

fun interceptors(): MutableList<Interceptor>

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).

networkInterceptors

fun networkInterceptors(): MutableList<Interceptor>

Returns a modifiable list of interceptors that observe a single network request and response. These interceptors must call Interceptor.Chain.proceed exactly once: it is an error for a network interceptor to short-circuit or repeat a network request.

pingInterval

fun pingInterval(interval: Long, unit: TimeUnit): OkHttpClient.Builder
fun pingInterval(duration: Duration): OkHttpClient.Builder

Sets the interval between HTTP/2 and web socket pings initiated by this client. Use this to automatically send ping frames until either the connection fails or it is closed. This keeps the connection alive and may detect connectivity failures.

protocols

fun protocols(protocols: List<Protocol>): OkHttpClient.Builder

Configure the protocols 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 protocols. Applications should only call this method to avoid specific compatibility problems, such as web servers that behave incorrectly when HTTP/2 is enabled.

proxy

fun proxy(proxy: Proxy?): OkHttpClient.Builder

Sets the HTTP proxy that will be used by connections created by this client. This takes precedence over 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).

proxyAuthenticator

fun proxyAuthenticator(proxyAuthenticator: Authenticator): OkHttpClient.Builder

Sets the authenticator used to respond to challenges from proxy servers. Use authenticator to set the authenticator for origin servers.

proxySelector

fun proxySelector(proxySelector: ProxySelector): OkHttpClient.Builder

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.

readTimeout

fun readTimeout(timeout: Long, unit: TimeUnit): OkHttpClient.Builder
fun readTimeout(duration: Duration): OkHttpClient.Builder

Sets the default read timeout for new connections. A value of 0 means no timeout, otherwise values must be between 1 and Integer.MAX_VALUE when converted to milliseconds.

retryOnConnectionFailure

fun retryOnConnectionFailure(retryOnConnectionFailure: Boolean): OkHttpClient.Builder

Configure this client to retry or not when a connectivity problem is encountered. By default, this client silently recovers from the following problems:

socketFactory

fun socketFactory(socketFactory: SocketFactory): OkHttpClient.Builder

Sets the socket factory used to create connections. OkHttp only uses the parameterless SocketFactory.createSocket method to create unconnected sockets. Overriding this method, e. g., allows the socket to be bound to a specific local address.

sslSocketFactory

fun sslSocketFactory(sslSocketFactory: SSLSocketFactory, trustManager: X509TrustManager): OkHttpClient.Builder

Sets the socket factory and trust manager used to secure HTTPS connections. If unset, the system defaults will be used.

writeTimeout

fun writeTimeout(timeout: Long, unit: TimeUnit): OkHttpClient.Builder
fun writeTimeout(duration: Duration): OkHttpClient.Builder

Sets the default write timeout for new connections. A value of 0 means no timeout, otherwise values must be between 1 and Integer.MAX_VALUE when converted to milliseconds.