OkHttp源码
OkHttp
OkhttpClient.Java OkHttp的总管理类
//负责线程切换
@get:JvmName("dispatcher") val dispatcher: Dispatcher = builder.dispatcher
//连接池 内部维护了一批连接,每次请求会查看连接池中是否已有,没有再创建新连接,使用过的连接也会进入连接池,池内连接根据配置回收或继续存储,用完的连接可以重用,正在使用的也可以(http2多路复用)
@get:JvmName("connectionPool") val connectionPool: ConnectionPool = builder.connectionPoo
/**
* Returns an immutable 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).
*/
@get:JvmName("interceptors") val interceptors: List<Interceptor> =
builder.interceptors.toImmutableList(
/**
* Returns an immutable 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.
*/
@get:JvmName("networkInterceptors") val networkInterceptors: List<Interceptor> =
builder.networkInterceptors.toImmutableList(
//连接过程事件监听器
@get:JvmName("eventListenerFactory") val eventListenerFactory: EventListener.Factory =
builder.eventListenerFactor
//连接或请求失败是否重试,默认true
@get:JvmName("retryOnConnectionFailure") val retryOnConnectionFailure: Boolean =
builder.retryOnConnectionFailure
//具体实例9.2 9:57,用于token过期,刷新后重发请求
@get:JvmName("authenticator") val authenticator: Authenticator = builder.authenticator
//是否接受重定向
@get:JvmName("followRedirects") val followRedirects: Boolean = builder.followRedirects
//重定向时协议切换是否继续,比如http变https
@get:JvmName("followSslRedirects") val followSslRedirects: Boolean = builder.followSslRedirects
//存储cookie
@get:JvmName("cookieJar") val cookieJar: CookieJar = builder.cookieJar
//缓存
@get:JvmName("cache") val cache: Cache? = builder.cache
//domain name system,这是一个Java原生方法,可以解析域名
@get:JvmName("dns") val dns: Dns = builder.dns
//网络代理
@get:JvmName("proxy") val proxy: Proxy? = builder.proxy
//网络代理详细
@get:JvmName("proxySelector") val proxySelector: ProxySelector =
when {
// Defer calls to ProxySelector.getDefault() because it can throw a SecurityException.
builder.proxy != null -> NullProxySelector
else -> builder.proxySelector ?: ProxySelector.getDefault() ?: NullProxySelector
}
//代理的认证处理
@get:JvmName("proxyAuthenticator") val proxyAuthenticator: Authenticator =
builder.proxyAuthenticator
//创建socket
@get:JvmName("socketFactory") val socketFactory: SocketFactory = builder.socketFactory
//ssl的socket
private val sslSocketFactoryOrNull: SSLSocketFactory?
@get:JvmName("sslSocketFactory") val sslSocketFactory: SSLSocketFactory
get() = sslSocketFactoryOrNull ?: throw IllegalStateException("CLEARTEXT-only client")
// 验证ssl证书的类,x509是证书格式
@get:JvmName("x509TrustManager") val x509TrustManager: X509TrustManager?
//连接标准,也就是tls版本和加密套件
@get:JvmName("connectionSpecs") val connectionSpecs: List<ConnectionSpec> =
builder.connectionSpecs
//支持的http协议
@get:JvmName("protocols") val protocols: List<Protocol> = builder.protocols
//验证证书与host是否对应
@get:JvmName("hostnameVerifier") val hostnameVerifier: HostnameVerifier = builder.hostnameVerifier
//指定网站证书配置一个签名,连接时会校验
@get:JvmName("certificatePinner") val certificatePinner: CertificatePinner
//操作x509验证证书
@get:JvmName("certificateChainCleaner") val certificateChainCleaner: CertificateChainCleaner?
/**
* Default call timeout (in milliseconds). By default there is no timeout for complete calls, but
* there is for the connect, write, and read actions within a call.
*/
@get:JvmName("callTimeoutMillis")
val callTimeoutMillis: Int = builder.callTimeou
/** Default connect timeout (in milliseconds). The default is 10 seconds. */
@get:JvmName("connectTimeoutMillis")
val connectTimeoutMillis: Int = builder.connectTimeou
/** Default read timeout (in milliseconds). The default is 10 seconds. */
@get:JvmName("readTimeoutMillis")
val readTimeoutMillis: Int = builder.readTimeou
/** Default write timeout (in milliseconds). The default is 10 seconds. */
@get:JvmName("writeTimeoutMillis")
val writeTimeoutMillis: Int = builder.writeTimeou
/**心跳间隔 Web socket and HTTP/2 ping interval (in milliseconds). By default pings are not sent. */
@get:JvmName("pingIntervalMillis")
val pingIntervalMillis: Int = builder.pingInterva
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 炎武的学习笔记!