类 OkHttpClient.Builder
- 封闭类:
OkHttpClient
-
构造器概要
构造器 -
方法概要
修饰符和类型方法说明addInterceptor(Interceptor interceptor) addNetworkInterceptor(Interceptor interceptor) authenticator(Authenticator authenticator) Sets the authenticator used to respond to challenges from origin servers.build()Sets the response cache to be used to read and write cached responses.callTimeout(long timeout, TimeUnit unit) Sets the default timeout for complete calls.callTimeout(Duration duration) Sets the default timeout for complete calls.certificatePinner(CertificatePinner certificatePinner) Sets the certificate pinner that constrains which certificates are trusted.connectionPool(ConnectionPool connectionPool) Sets the connection pool used to recycle HTTP and HTTPS connections.connectionSpecs(List<ConnectionSpec> connectionSpecs) connectTimeout(long timeout, TimeUnit unit) Sets the default connect timeout for new connections.connectTimeout(Duration duration) Sets the default connect timeout for new connections.Sets the handler that can accept cookies from incoming HTTP responses and provides cookies to outgoing HTTP requests.dispatcher(Dispatcher dispatcher) Sets the dispatcher used to set policy and execute asynchronous requests.Sets the DNS service used to lookup IP addresses for hostnames.eventListener(EventListener eventListener) Configure a single client scoped listener that will receive all analytic events for this client.eventListenerFactory(EventListener.Factory eventListenerFactory) Configure a factory to provide per-call scoped listeners that will receive analytic events for this client.followRedirects(boolean followRedirects) Configure this client to follow redirects.followSslRedirects(boolean followProtocolRedirects) Configure this client to follow redirects from HTTPS to HTTP and from HTTP to HTTPS.hostnameVerifier(HostnameVerifier hostnameVerifier) Sets the verifier used to confirm that response certificates apply to requested hostnames for HTTPS connections.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).Returns a modifiable list of interceptors that observe a single network request and response.pingInterval(long interval, TimeUnit unit) Sets the interval between HTTP/2 and web socket pings initiated by this client.pingInterval(Duration duration) Sets the interval between HTTP/2 and web socket pings initiated by this client.Configure the protocols used by this client to communicate with remote servers.Sets the HTTP proxy that will be used by connections created by this client.proxyAuthenticator(Authenticator proxyAuthenticator) Sets the authenticator used to respond to challenges from proxy servers.proxySelector(ProxySelector proxySelector) Sets the proxy selection policy to be used if noproxyis specified explicitly.readTimeout(long timeout, TimeUnit unit) Sets the default read timeout for new connections.readTimeout(Duration duration) Sets the default read timeout for new connections.retryOnConnectionFailure(boolean retryOnConnectionFailure) Configure this client to retry or not when a connectivity problem is encountered.socketFactory(SocketFactory socketFactory) Sets the socket factory used to create connections.sslSocketFactory(SSLSocketFactory sslSocketFactory) 已过时。sslSocketFactory(SSLSocketFactory sslSocketFactory, X509TrustManager trustManager) Sets the socket factory and trust manager used to secure HTTPS connections.writeTimeout(long timeout, TimeUnit unit) Sets the default write timeout for new connections.writeTimeout(Duration duration) Sets the default write timeout for new connections.
-
构造器详细资料
-
Builder
public Builder()
-
-
方法详细资料
-
callTimeout
Sets the default timeout for complete calls. A value of 0 means no timeout, otherwise values must be between 1 andInteger.MAX_VALUEwhen 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.
The default value is 0 which imposes no timeout.
-
callTimeout
Sets the default timeout for complete calls. A value of 0 means no timeout, otherwise values must be between 1 andInteger.MAX_VALUEwhen 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.
The default value is 0 which imposes no timeout.
-
connectTimeout
Sets the default connect timeout for new connections. A value of 0 means no timeout, otherwise values must be between 1 andInteger.MAX_VALUEwhen converted to milliseconds.The connect timeout is applied when connecting a TCP socket to the target host. The default value is 10 seconds.
-
connectTimeout
Sets the default connect timeout for new connections. A value of 0 means no timeout, otherwise values must be between 1 andInteger.MAX_VALUEwhen converted to milliseconds.The connect timeout is applied when connecting a TCP socket to the target host. The default value is 10 seconds.
-
readTimeout
Sets the default read timeout for new connections. A value of 0 means no timeout, otherwise values must be between 1 andInteger.MAX_VALUEwhen converted to milliseconds.The read timeout is applied to both the TCP socket and for individual read IO operations including on
Sourceof theResponse. The default value is 10 seconds.- 另请参阅:
-
readTimeout
Sets the default read timeout for new connections. A value of 0 means no timeout, otherwise values must be between 1 andInteger.MAX_VALUEwhen converted to milliseconds.The read timeout is applied to both the TCP socket and for individual read IO operations including on
Sourceof theResponse. The default value is 10 seconds.- 另请参阅:
-
writeTimeout
Sets the default write timeout for new connections. A value of 0 means no timeout, otherwise values must be between 1 andInteger.MAX_VALUEwhen converted to milliseconds.The write timeout is applied for individual write IO operations. The default value is 10 seconds.
- 另请参阅:
-
writeTimeout
Sets the default write timeout for new connections. A value of 0 means no timeout, otherwise values must be between 1 andInteger.MAX_VALUEwhen converted to milliseconds.The write timeout is applied for individual write IO operations. The default value is 10 seconds.
- 另请参阅:
-
pingInterval
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.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.
-
pingInterval
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.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.
-
proxy
Sets the HTTP proxy that will be used by connections created by this client. This takes precedence overproxySelector, which is only honored when this proxy is null (which it is by default). To disable proxy use completely, callproxy(Proxy.NO_PROXY). -
proxySelector
Sets the proxy selection policy to be used if noproxyis 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 defaultproxy selector will be used. -
cookieJar
Sets the handler that can accept cookies from incoming HTTP responses and provides cookies to outgoing HTTP requests.If unset, no cookies will be accepted nor provided.
-
cache
Sets the response cache to be used to read and write cached responses. -
dns
Sets the DNS service used to lookup IP addresses for hostnames.If unset, the
system-wide defaultDNS will be used. -
socketFactory
Sets the socket factory used to create connections. OkHttp only uses the parameterlesscreateSocket()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 defaultsocket factory will be used. -
sslSocketFactory
已过时。SSLSocketFactorydoes not expose itsX509TrustManager, 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 callsslSocketFactory(SSLSocketFactory, X509TrustManager), which avoids such reflection.Sets the socket factory used to secure HTTPS connections. If unset, the system default will be used. -
sslSocketFactory
public OkHttpClient.Builder sslSocketFactory(SSLSocketFactory sslSocketFactory, X509TrustManager trustManager) Sets the socket factory and trust manager used to secure HTTPS connections. If unset, the system defaults will be used.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(); -
hostnameVerifier
Sets the verifier used to confirm that response certificates apply to requested hostnames for HTTPS connections.If unset, a default hostname verifier will be used.
-
certificatePinner
Sets the certificate pinner that constrains which certificates are trusted. By default HTTPS connections rely on only theSSL socket factoryto establish trust. Pinning certificates avoids the need to trust certificate authorities. -
authenticator
Sets the authenticator used to respond to challenges from origin servers. UseproxyAuthenticatorto set the authenticator for proxy servers.If unset, the no authentication will be attempted.
-
proxyAuthenticator
Sets the authenticator used to respond to challenges from proxy servers. Useauthenticatorto set the authenticator for origin servers.If unset, the no authentication will be attempted.
-
connectionPool
Sets the connection pool used to recycle HTTP and HTTPS connections.If unset, a new connection pool will be used.
-
followSslRedirects
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. -
followRedirects
Configure this client to follow redirects. If unset, redirects will be followed. -
retryOnConnectionFailure
Configure this client to retry or not when a connectivity problem is encountered. By default, this client silently recovers from the following problems:- Unreachable IP addresses. If the URL's host has multiple IP addresses, failure to reach any individual IP address doesn't fail the overall request. This can increase availability of multi-homed services.
- Stale pooled connections. The
ConnectionPoolreuses sockets to decrease request latency, but these connections will occasionally time out. - Unreachable proxy servers. A
ProxySelectorcan be used to attempt multiple proxy servers in sequence, eventually falling back to a direct connection.
Set this to false to avoid retrying requests when doing so is destructive. In this case the calling application should do its own recovery of connectivity failures.
-
dispatcher
Sets the dispatcher used to set policy and execute asynchronous requests. Must not be null. -
protocols
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.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_0is not supported in this set. Requests are initiated withHTTP/1.1. If the server responds withHTTP/1.0, that will be exposed byResponse.protocol().- 参数:
protocols- the protocols to use, in order of preference. If the list containsProtocol.H2_PRIOR_KNOWLEDGEthen that must be the only protocol and HTTPS URLs will not be supported. Otherwise the list must containProtocol.HTTP_1_1. The list must not contain null orProtocol.HTTP_1_0.
-
connectionSpecs
-
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). -
addInterceptor
-
networkInterceptors
Returns a modifiable list of interceptors that observe a single network request and response. These interceptors must callInterceptor.Chain.proceed(com.lark.oapi.okhttp.Request)exactly once: it is an error for a network interceptor to short-circuit or repeat a network request. -
addNetworkInterceptor
-
eventListener
Configure a single client scoped listener that will receive all analytic events for this client.- 另请参阅:
-
eventListenerFactory
Configure a factory to provide per-call scoped listeners that will receive analytic events for this client.- 另请参阅:
-
build
-
SSLSocketFactorydoes not expose itsX509TrustManager, which is a field that OkHttp needs to build a clean certificate chain.