ConnectInterceptor

122 阅读1分钟

源码解析

public Response intercept(Chain chain) throws IOException {
    RealInterceptorChain realChain = (RealInterceptorChain) chain;
    Request request = realChain.request();
    // 1.建立HTTP请求所需要的核心StreamAllocation,在RetryAndFollowUpInterceptor中创建的
    StreamAllocation streamAllocation = realChain.streamAllocation();
    // We need the network to satisfy this request. Possibly for validating a conditional GET.
    boolean doExtensiveHealthChecks = !request.method().equals("GET");
    // 2.初始化HttpCodec
    HttpCodec httpCodec = streamAllocation.newStream(client, chain, doExtensiveHealthChecks);
    // 3.获取RealConnection,非常关键,用来进行实际网络IO传输的
    RealConnection connection = streamAllocation.connection();
    // 注意这里创建出来的三个核心的对象
    return realChain.proceed(request, streamAllocation, httpCodec, connection);
}

总结:

  1. 创建HttpCodec
  2. 创建RealConnection

参考: