okhttp详解系列五:连接拦截器 ConnectInterceptor
- okhttp详解系列一:开篇
- okhttp详解系列二:重试重定向拦截器
- okhttp详解系列三:桥拦截器 BridgeInterceptor
- okhttp详解系列四:缓存拦截器
- okhttp详解系列五:连接拦截器 ConnectInterceptor
- okhttp详解系列六:服务请求拦截器 CallServerInterceptor
连接拦截器(ConnectInterceptor)负责与服务器建立网络连接,连接缓存池、dns解析、https证书校验等都在连接拦截器阶段来执行。连接拦截器类中的代码非常少,主要逻辑都在Exchange的初始化中:
1 | object ConnectInterceptor : Interceptor { |
连接拦截器的主要时序:
zenuml
ConnectInterceptor.intercept {
RealCall.initExchange {
ExchangeCodec = ExchangeFinder.find {
RealConnection = findHealthyConnection {
findConnection
}
ExchangeCodec = RealConnection.newCodec {
if (http2) {
"new Http2ExchangeCodec()"
} else {
"new Http1ExchangeCodec()"
}
}
}
new Exchange
}
RealInterceptorChain."copy(传入Exchange)"
RealInterceptorChain."proceed(把网络请求交给下一个拦截器)"
}
连接池查找可用连接
下面流程图是从okhttp3.internal.connection.ExchangeFinder#findConnection开始进行分析:
连接拦截器类图结构
备注
https证书校验在okhttp3.internal.tls.OkHostnameVerifier#verify(java.lang.String, javax.net.ssl.SSLSession)