返回

OkHttp源码解析

Android

本文将带您走近OkHttp,探索其强大内核,从开发者的视角剖析源码,带领您开启OkHttp的奇妙之旅。

OkHttp简介

OkHttp是Android平台上最流行的HTTP客户端库,由Square公司开发维护。它提供了丰富的功能,包括:

  • 同步和异步请求
  • 缓存
  • 拦截器
  • 线程池
  • SSL安全
  • 日志配置

OkHttp以其简单易用、功能强大和性能优异而著称,被广泛应用于Android应用程序开发中。

OkHttp核心组件

Request

请求对象是OkHttp的核心组件之一,它封装了HTTP请求的所有信息,包括请求方法、URL、请求头和请求体。

public final class Request {

  final String method;
  final HttpUrl url;
  final Headers headers;
  final RequestBody body;
  final Object tag;
  final MediaType contentType;
  final CacheControl cacheControl;

  Request(Builder builder) {
    this.method = builder.method;
    this.url = builder.url;
    this.headers = builder.headers.build();
    this.body = builder.body;
    this.tag = builder.tag != null ? builder.tag : this;
    this.contentType = builder.contentType;
    this.cacheControl = builder.cacheControl;
  }

  // ...
}

Response

响应对象是OkHttp的核心组件之一,它封装了HTTP响应的所有信息,包括响应代码、响应消息、响应头和响应体。

public final class Response {

  final Request request;
  final Protocol protocol;
  final int code;
  final String message;
  final Headers headers;
  final ResponseBody body;
  final long sentRequestAtMillis;
  final long receivedResponseAtMillis;

  Response(Builder builder) {
    this.request = builder.request;
    this.protocol = builder.protocol;
    this.code = builder.code;
    this.message = builder.message;
    this.headers = builder.headers.build();
    this.body = builder.body;
    this.sentRequestAtMillis = builder.sentRequestAtMillis;
    this.receivedResponseAtMillis = builder.receivedResponseAtMillis;
  }

  // ...
}

Call

调用对象是OkHttp的核心组件之一,它封装了HTTP请求和响应的执行过程。

public abstract class Call {

  final OkHttpClient client;
  final Request originalRequest;

  protected Call(OkHttpClient client, Request originalRequest) {
    this.client = client;
    this.originalRequest = originalRequest;
  }

  // ...
}

OkHttpClient

OkHttpClient 是OkHttp的核心组件,它提供了配置OkHttp客户端的各种方法。

public final class OkHttpClient {

  public static final long DEFAULT_CONNECT_TIMEOUT_MILLIS = 10_000;
  public static final long DEFAULT_READ_TIMEOUT_MILLIS = 10_000;
  public static final long DEFAULT_WRITE_TIMEOUT_MILLIS = 10_000;

  final Dispatcher dispatcher;
  final ConnectionPool connectionPool;
  final List<Interceptor> interceptors;
  final List<Interceptor> networkInterceptors;
  final EventListener.Factory eventListenerFactory;
  final CookieJar cookieJar;
  final Cache cache;
  final InternalCache internalCache;
  final boolean followRedirects;
  final boolean followSslRedirects;
  final boolean retryOnConnectionFailure;
  final SocketFactory socketFactory;
  final SSLSocketFactory sslSocketFactory;
  final CertificatePinner certificatePinner;
  final HostnameVerifier hostnameVerifier;
  final ProxySelector proxySelector;
  final Proxy proxy;
  final Authenticator authenticator;
  final ConnectionSpecSelector connectionSpecSelector;
  final List<ConnectionSpec> connectionSpecs;

  OkHttpClient() {
    dispatcher = new Dispatcher();
    connectionPool = new ConnectionPool();
    interceptors = new ArrayList<>();
    networkInterceptors = new ArrayList<>();
    eventListenerFactory = EventListener.factory(EventListener.NONE);
    cookieJar = CookieJar.NO_COOKIES;
    cache = null;
    internalCache = null;
    followRedirects = true;
    followSslRedirects = true;
    retryOnConnectionFailure = true;
    socketFactory = SocketFactory.getDefault();
    sslSocketFactory = null;
    certificatePinner = CertificatePinner.DEFAULT;
    hostnameVerifier = HostnameVerifier.DEFAULT;
    proxySelector = ProxySelector.getDefault();
    proxy = null;
    authenticator = Authenticator.NONE;
    connectionSpecSelector = ConnectionSpecSelector.DEFAULT;
    connectionSpecs = Lists.newArrayList(ConnectionSpec.MODERN_TLS,
        ConnectionSpec.COMPATIBLE_TLS, ConnectionSpec.CLEARTEXT);
  }

  // ...
}