OKHttp源码解析(6)----拦截器CallServerInterceptor

栏目: Java · 发布时间: 7年前

内容简介:This is the last interceptor in the chain. It makes a network call to the server. 这是链中最后一个拦截器,它向服务器发起了一次网络访问请求服务拦截器,负责向服务器发送请求数据、从服务器读取响应数据未完待续

This is the last interceptor in the chain. It makes a network call to the server. 这是链中最后一个拦截器,它向服务器发起了一次网络访问

请求服务拦截器,负责向服务器发送请求数据、从服务器读取响应数据

源码解析

@Override public Response intercept(Chain chain) throws IOException {
    RealInterceptorChain realChain = (RealInterceptorChain) chain;
    HttpCodec httpCodec = realChain.httpStream();
    StreamAllocation streamAllocation = realChain.streamAllocation();
    RealConnection connection = (RealConnection) realChain.connection();
    Request request = realChain.request();

    long sentRequestMillis = System.currentTimeMillis();
     // 整理请求头并写入
    httpCodec.writeRequestHeaders(request);

    Response.Builder responseBuilder = null;
     // 检查是否为有 body 的请求方法
    if (HttpMethod.permitsRequestBody(request.method()) && request.body() != null) {
      // If there is a "Expect: 100-continue" header on the request, wait for a "HTTP/1.1 100
      // Continue" response before transmitting the request body. If we do not get that, return what
      // we did get (such as a 4xx response) without ever transmitting the request body.
      // 如果有 Expect: 100-continue在请求头中,那么要等服务器的响应。100-continue协议需要在post数据前,
      征询服务器情况,看服务器是否处理POST的数据
      if ("100-continue".equalsIgnoreCase(request.header("Expect"))) {
      //发送100-continue的请求
        httpCodec.flushRequest();
        //如果有中间级的响应,返回null
        responseBuilder = httpCodec.readResponseHeaders(true);
      }

      if (responseBuilder == null) {
        // Write the request body if the "Expect: 100-continue" expectation was met.
        // 写入请求体
        Sink requestBodyOut = httpCodec.createRequestBody(request, request.body().contentLength());
        BufferedSink bufferedRequestBody = Okio.buffer(requestBodyOut);
        request.body().writeTo(bufferedRequestBody);
        bufferedRequestBody.close();
      } else if (!connection.isMultiplexed()) {
        // If the "Expect: 100-continue" expectation wasn't met, prevent the HTTP/1 connection from
        // being reused. Otherwise we're still obligated to transmit the request body to leave the
        // connection in a consistent state.
        streamAllocation.noNewStreams();
      }
    }
    //发送最终的请求
    httpCodec.finishRequest();
     // 得到响应头
    if (responseBuilder == null) {
      responseBuilder = httpCodec.readResponseHeaders(false);
    }

    Response response = responseBuilder
        .request(request)
        .handshake(streamAllocation.connection().handshake())
        .sentRequestAtMillis(sentRequestMillis)
        .receivedResponseAtMillis(System.currentTimeMillis())
        .build();

    int code = response.code();
     // 如果为 web socket 且状态码是 101 ,那么 body 为空
    if (forWebSocket && code == 101) {
      // Connection is upgrading, but we need to ensure interceptors see a non-null response body.
      response = response.newBuilder()
          .body(Util.EMPTY_RESPONSE)
          .build();
    } else {
      response = response.newBuilder()
          .body(httpCodec.openResponseBody(response))
          .build();
    }
     // 如果请求头中有 close 那么断开连接
    if ("close".equalsIgnoreCase(response.request().header("Connection"))
        || "close".equalsIgnoreCase(response.header("Connection"))) {
      streamAllocation.noNewStreams();
    }
     // 抛出协议异常
    if ((code == 204 || code == 205) && response.body().contentLength() > 0) {
      throw new ProtocolException(
          "HTTP " + code + " had non-zero Content-Length: " + response.body().contentLength());
    }

    return response;
  }

复制代码

未完待续


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

互联网冲击

互联网冲击

杰伦•拉尼尔 (Jaron Lanier) / 李龙泉、祝朝伟 / 中信出版社 / 2014-5-1 / CNY 65.00

在《互联网冲击》一书中,关于网络技术对经济造成的影响,作者进行了卓有远见的预测。拉尼尔断言,数字网络的崛起会造成我们经济的衰退,并且摧毁中产阶级。如今,科技已经征服了一个又一个行业——从媒体到医药业,再到制造业。我们的就业形势和个人财富都将面临更加严峻的挑战。  但还有另外一种方法,能够让科技掌握我们的未来。在本书中,作者不仅展现了他的雄心壮志,而且也处处体现着他的人文关怀。拉尼尔指明了一条新信息......一起来看看 《互联网冲击》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

图片转BASE64编码
图片转BASE64编码

在线图片转Base64编码工具

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具