Summary of OkHttp source code interpretation (VIII) -- > BridgeInterceptor

Keywords: network encoding OkHttp

Summary of OkHttp source code interpretation (VIII) - > bridgeinterceptor

Tags (space separated): learning notes of OkHttp source code


  • The following summary of relevant knowledge is based on the relevant learning and opinions of If you need to check the relevant teaching of, you can still feel it.

Official website introduction

Bridges from application code to network code. First it builds a network request from a user
 * request. Then it proceeds to call the network. Finally it builds a user response from the network
 * response.

Mainly responsible for setting content length, encoding method, gzip compression, adding request headers, cookie s, etc

Main method (mainly to add header information)

@Override public Response intercept(Chain chain) throws IOException {
    Request userRequest = chain.request();
    Request.Builder requestBuilder = userRequest.newBuilder();

    RequestBody body = userRequest.body();
    if (body != null) {
      MediaType contentType = body.contentType();
      if (contentType != null) {
        requestBuilder.header("Content-Type", contentType.toString());

     //Response length
      long contentLength = body.contentLength();
      if (contentLength != -1) {
        requestBuilder.header("Content-Length", Long.toString(contentLength));
      } else {
        //transform method 
        requestBuilder.header("Transfer-Encoding", "chunked");
   //Host host
    if (userRequest.header("Host") == null) {
      requestBuilder.header("Host", hostHeader(userRequest.url(), false));

    //Keep alive stay connected
    if (userRequest.header("Connection") == null) {
      requestBuilder.header("Connection", "Keep-Alive");

    // If we add an "Accept-Encoding: gzip" header field we're responsible for also decompressing
    // the transfer stream.
    boolean transparentGzip = false;
    if (userRequest.header("Accept-Encoding") == null && userRequest.header("Range") == null) {
      transparentGzip = true;
      requestBuilder.header("Accept-Encoding", "gzip");

    List<Cookie> cookies = cookieJar.loadForRequest(userRequest.url());
    if (!cookies.isEmpty()) {
      requestBuilder.header("Cookie", cookieHeader(cookies));

    if (userRequest.header("User-Agent") == null) {
      requestBuilder.header("User-Agent", Version.userAgent());
    //The most important thing is that the proceed() method sends the request server response to the server and returns the response
    Response networkResponse = chain.proceed(;
    //Convert the response of network request and server response to the response that our users can use
    HttpHeaders.receiveHeaders(cookieJar, userRequest.url(), networkResponse.headers());

    Response.Builder responseBuilder = networkResponse.newBuilder()
    //Convert the response returned by the network request
    //transparentGzip==true support gzip compression content encoding support gzip judge whether http header has body
    if (transparentGzip
        && "gzip".equalsIgnoreCase(networkResponse.header("Content-Encoding"))
        && HttpHeaders.hasBody(networkResponse)) {
      //Convert the response's body() input stream to the GzipSource type
      GzipSource responseBody = new GzipSource(networkResponse.body().source());
      Headers strippedHeaders = networkResponse.headers().newBuilder()
      String contentType = networkResponse.header("Content-Type");
      //Direct read
      responseBuilder.body(new RealResponseBody(contentType, -1L, Okio.buffer(responseBody)));


The above methods mainly set related request header information, gzip compression, cookie related

major function

  • 1. Be responsible for converting a Request built by the user into a Request capable of network access
  • 2. Make a network Request for the Request that meets the network Request
  • 3. Convert the Response from the network request to the Response that users can use (gzip compression / decompression)

Posted by bobthebuilder on Mon, 04 May 2020 08:25:20 -0700