originalRequest, this, client.connectTimeoutMillis(),
client.readTimeoutMillis(), client.writeTimeoutMillis());
try {
Response response = chain.proceed(originalRequest);
return response;
} catch (IOException e) {
} finally {
}
}
}
2.ConnectInterceptor#intercept
连接池在ConnectInterceptor
中包装起来了,我们进去瞄一眼:
@Override
public Response intercept(Chain chain) throws IOException {
RealInterceptorChain realChain = (RealInterceptorChain) chain;
Request request = realChain.request();
//拿到chain的Transmitter,里面包装了RealCall、okhttpClient、connectionPool等信息
Transmitter transmitter = realChain.transmitter();
//是否不是get请求
boolean doExtensiveHealthChecks = !request.method().equals(“GET”);
Exchange exchange = transmitter.newExchange(chain, doExtensiveHealthChecks);
return realChain.proceed(request, transmitter, exchange);
}
3.Transmitter#newExchange
拿到chain的Transmitter
对象,该对象是RealCall
、okhttpClient
、connectionPool
等信息的包装类,将是否不是get请求的标识传给了transmitter
的newExchange
方法:
Exchange newExchange(Interceptor.Chain chain, boolean doExtensiveHealthChecks) {
//exchangeFinder是对connectionPool、connectionPool、request等信息的包装,它是在RetryAndFollowUpInterceptor拦截器中初始化出来的
ExchangeCodec codec = exchangeFinder.find(client, chain, doExtensiveHealthChecks);
Exchange result = new Exchange(this, call, eventListener, exchangeFinder, codec);
synchronized (connectionPool) {
this.exchange = result;
return result;
}
}
4.ExchangeCodec#find
该方法中将ExchangeCodec
的获取交给了exchangeFinder
对象,ExchangeCodec
是一个接口,实现类有Http2ExchangeCodec
和Http1ExchangeCodec
,这两个类表示http1和http2的建立连接的类,里面实现了writeRequestHeaders
和createRequestBody
等方法,这两个方法是在CallServerInterceptor
拦截器中使用的。exchangeFinder
是对connectionPool、connectionPool、request等信息的包装,它是在RetryAndFollowUpInterceptor拦截器中初始化出来的。我们接着看exchangeFinder
的find方法:
public ExchangeCodec find(
OkHttpClient client, Interceptor.Chain chain, boolean doExtensiveHealthChecks) {
int connectTimeout = chain.connectT