1.OkHttp请求流程图
OkHttp作为Android目前最流行的网络请求库,今天来分析一下它的源码,学习学习。废话不多说,咱们先上图。看不清的请单击图片
2.OkHttp相关类
- OkHttpClent
用于创建Call对象的工厂。尽量只创建一个client实例,并复用它去进行所有的Http请求。因为每个client都拥有自己的连接池和线程池。复用这些连接池和线程池可以减少延迟并节省内存。简单示例:
OkHttpClient.Builder()
.readTimeout()//设置读取超时时间
.connectTimeout()//设置连接超时时间
.writeTimeout()//设置写入超时时间
.cache()//设置缓存
.addInterceptor()//添加自定义拦截器
.build()
复制代码
- Interceptor-拦截器
作用:添加、删除或转换请求上的标头或响应。在发送网络请求的时候,系统会自己创建5个拦截器。 分别是:
- RetryAndFollowUpInterceptor-重定向拦截器
负责请求失败重连
/**
* How many redirects and auth challenges should we attempt? Chrome follows 21 redirects; Firefox,
* curl, and wget follow 20; Safari follows 16; and HTTP/1.0 recommends 5.
*/
//Chrome遵循21重定向;Firefox、curl和wget是20;Safari是16;HTTP/1.0建议5。
private static final int MAX_FOLLOW_UPS = 20;//最大重定向次数
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();//获取Request对象
RealInterceptorChain realChain = (RealInterceptorChain) chain;//拦截器链
Call call = realChain.call();//获取Call对象
EventListener eventListener = realChain.eventListener();
//创建StreamAllocation实例,它OkHttp网络请求的元组件
StreamAllocation streamAllocation = new StreamAllocation(client.connectionPool(),
createAddress(request.url()), call, eventListener, callStackTrace);
this.streamAllocation = streamAllocation;
int followUpCount = 0;//重定向计数
Response priorResponse = null;
while (true) {//通过死循环进行重定向
if (canceled) {//如果已经取消了
streamAllocation.release();//释放streamAllocation对象
throw new IOException("Canceled");
}
Response response;
boolean releaseConnection = true;
try {
//通过拦截器链执行下一个拦截器
response = realChain.proceed(request, streamAllocation, null, null);
releaseConnection = false;
} catch (RouteException e) {
// The attempt to connect via a route failed. The request will not have been sent.
if (!recover(e.getLastConnectException(), streamAllocation, false, request)) {
throw e.getLastConnectException();
}
releaseConnection = false;
continue;
} catch (IOException e) {
// An attempt to communicate with a server failed. The request may have been sent.
boolean requestSendStarted = !(e instanceof ConnectionShutdownException);
if (!recover(e, streamAllocation, requestSendStarted, request)) throw e;
releaseConnection = false;
continue;
} finally {
// We're throwing an unchecked exception. Release any resources.
if (releaseConnection) {
streamAllocation.streamFailed(null);
streamAllocation.release();
}
}
**************省略代码****************
if (++followUpCount > MAX_FOLLOW_UPS) {//判断重定向的次数
streamAllocation.release();
throw new ProtocolException("Too many follow-up requests: " + followUpCount);
}
**************省略代码****************
}
}
复制代码
- BridgeInterceptor-桥接拦截器
从应用程序代码到网络代码之间的桥梁。负责设置内容长度、编码方式、设置gzip压缩、添加请求头、cokie等相关功能。
@Override
public Response intercept(Chain chain) throws IOException {
Request userRequest = chain.request();//获取Request对象
//创建一个新的Request.Builder对象,用以构造一个新的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());
}
long contentLength = body.contentLength();//获取内容长度
if (contentLength != -1) {
requestBuilder.header("Content-Length", Long.toString(contentLength));
requestBuilder.removeHeader("Transfer-Encoding");
} else {
requestBuilder.header("Transfer-Encoding", "chunked");
requestBuilder.removeHeader("Content-Length");
}
}
//设置Host
if (userRequest.header("Host") == null) {
requestBuilder.header("Host", hostHeader(userRequest.url(), false));
}
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;
//设置Gzip压缩
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()) {//设置cookies
requestBuilder.header("Cookie", cookieHeader(cookies));
}
//这里设置的是Okhttp的版本
if (userRequest.header("User-Agent") == null) {
requestBuilder.header("User-Agent", Version.userAgent());
}
//通过拦截器链执行下一个拦截器
Response networkResponse = chain.proceed(requestBuilder.build());
HttpHeaders.receiveHeaders(cookieJar, userRequest.url(), networkResponse.headers());
Response.Builder responseBuilder = networkResponse.newBuilder()
.request(userRequest);
if (transparentGzip
&& "gzip".equalsIgnoreCase(networkResponse.header("Content-Encoding"))
&& HttpHeaders.hasBody(networkResponse)) {
GzipSource responseBody = new GzipSource(networkResponse.body().source());
Headers strippedHeaders = networkResponse.headers().newBuilder()
.removeAll("Content-Encoding")
.removeAll("Content-Length")
.build();
responseBuilder.headers(strippedHeaders);
String contentType = networkResponse.header("Content-Type");
responseBuilder.body(new RealResponseBody(contentType, -1L, Okio.buffer(responseBody)));
}
return responseBuilder.build();
}
复制代码
- CacheInterceptor-缓存拦截器
提供来自缓存的请求,并向缓存写入响应
- ConnectInterceptor-连接拦截器
开启到目标服务器的连接并继续到下一个拦截器。
@Override
public Response intercept(Chain chain) throws IOException {
RealInterceptorChain realChain = (RealInterceptorChain) chain;
Request request = realChain.request();
//获取在重定向拦截器中创建的网络元组件StreamAllocation
StreamAllocation streamAllocation = realChain.streamAllocation();
// We need the network to satisfy this request. Possibly for validating a conditional GET.
boolean doExtensiveHealthChecks = !request.method().equals("GET");
//创建HttpCodec实例,HttpCodec用来编码HTTP请求并解码HTTP响应。
HttpCodec httpCodec = streamAllocation.newStream(client, chain, doExtensiveHealthChecks);
/**RealConnection:HTTP、HTTPS或HTTPS+HTTP/2连接的sockets和流。
*可用于多个HTTP请求/响应交换。连接可以直接到源服务器,也可以通过代理。
*通常,此类的实例由HTTP客户机自动创建、连接和执行。
*应用程序可以使用该类作为ConnectionPool连接池的成员监视HTTP连接。
*/
RealConnection connection = streamAllocation.connection();
//执行下一个拦截器
return realChain.proceed(request, streamAllocation, httpCodec, connection);
}
复制代码
- CallServerInterceptor-呼叫服务拦截器
这是拦截器链中最后一个拦截器。它对服务器进行网络调用
@Override public Response intercept(Chain chain) throws IOException {
RealInterceptorChain realChain = (RealInterceptorChain) chain;
HttpCodec httpCodec = realChain.httpStream();//获取在连接拦截器中生成的HttpCodec
StreamAllocation streamAllocation = realChain.streamAllocation();
RealConnection connection = (RealConnection) realChain.connection();
Request request = realChain.request();
long sentRequestMillis = System.currentTimeMillis();
//监听开始调用请求头
realChain.eventListener().requestHeadersStart(realChain.call());
//写入请求头,这将更新HTTP引擎的sentRequestMillis字段
httpCodec.writeRequestHeaders(request);
//监听调用请求头的结束
realChain.eventListener().requestHeadersEnd(realChain.call(), request);
Response.Builder responseBuilder = null;
//请求类型既不是GET也不是HEAD,同时请求体为空
if (HttpMethod.permitsRequestBody(request.method()) && request.body() != null) {
// If there's a "Expect: 100-continue" header on the request, wait for a "HTTP/1.1 100
// Continue" response before transmitting the request body. If we don't get that, return
// what we did get (such as a 4xx response) without ever transmitting the request body.
//如果请求上有一个“Expect: 100- Continue”报头,在发送请求主体之前等待一个“HTTP/1.1 100 //Continue”响应。如果我们没有得到响应,返回我们得到的(例如4xx响应),而不发送请求主体。
if ("100-continue".equalsIgnoreCase(request.header("Expect"))) {
//将请求刷新到scoket
httpCodec.flushRequest();
realChain.eventListener().responseHeadersStart(realChain.call());
responseBuilder = httpCodec.readResponseHeaders(true);//读取响应请求头
}
if (responseBuilder == null) {
// Write the request body if the "Expect: 100-continue" expectation was met.
realChain.eventListener().requestBodyStart(realChain.call());
long contentLength = request.body().contentLength();
//返回请求体的流
CountingSink requestBodyOut =
new CountingSink(httpCodec.createRequestBody(request, contentLength));
BufferedSink bufferedRequestBody = Okio.buffer(requestBodyOut);
request.body().writeTo(bufferedRequestBody);//写入请求体
bufferedRequestBody.close();//关闭buffer
realChain.eventListener()
.requestBodyEnd(realChain.call(), requestBodyOut.successfulCount);
} 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.
//如果没有满足“Expect:100-continue”的期望,则阻止HTTP/1连接被重用。否则,我们仍然有///义务发送请求主体,使连接保持一致状态
streamAllocation.noNewStreams();
}
}
//结束请求
httpCodec.finishRequest();
if (responseBuilder == null) {
realChain.eventListener().responseHeadersStart(realChain.call());
/**
* 从HTTP传输解析响应头的字节。
*
* @param expectContinue为null,如果这是一个中间响应,带有“100”
*响应代码。否则这个方法永远不会返回null。
* /
responseBuilder = httpCodec.readResponseHeaders(false);
}
Response response = responseBuilder
.request(request)//请求的相关参数
.handshake(streamAllocation.connection().handshake())
.sentRequestAtMillis(sentRequestMillis)//设置请求时间
.receivedResponseAtMillis(System.currentTimeMillis())//接收到响应的时间
.build();
int code = response.code();//获取响应码
if (code == 100) {
// server sent a 100-continue even though we did not request one.
// try again to read the actual response
//服务器发送了一个100-continue,尽管我们没有请求一个。再次尝试读取实际的响应
responseBuilder = httpCodec.readResponseHeaders(false);
response = responseBuilder
.request(request)
.handshake(streamAllocation.connection().handshake())
.sentRequestAtMillis(sentRequestMillis)
.receivedResponseAtMillis(System.currentTimeMillis())
.build();
code = response.code();
}
realChain.eventListener()
.responseHeadersEnd(realChain.call(), response);
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();
}
if ("close".equalsIgnoreCase(response.request().header("Connection"))
|| "close".equalsIgnoreCase(response.header("Connection"))) {
streamAllocation.noNewStreams();
}
//204、205抛出异常
if ((code == 204 || code == 205) && response.body().contentLength() > 0) {
throw new ProtocolException(
"HTTP " + code + " had non-zero Content-Length: " + response.body().contentLength());
}
return response;
}
复制代码
NOTE:上述五个拦截器在执行请求的时候,会通过RealInterceptorChain按顺序依次执行。在RetryAndFollowUpInterceptor执行的时候会创建StreamAllocation,它网络请求的元组件。
- StreamAllocation-协调Connections、Stream、Calls
- Connections: 到远程服务器的物理Socket连接。这些设置可能很慢,所以有必要取消当前正在连接的连接。
- Streams: 逻辑HTTP请求/响应对,它们在连接上分层。每个连接都有自己的分配限制,这定义了连接可以承载多少并发流。HTTP / 1。x连接一次可以携带一个流,HTTP/2通常携带多个。
- Calls: 流的逻辑序列,通常是初始请求及其后续请求。OkHttp更喜欢将单个调用的所有流保持在同一个连接上,以获得更好的行为和位置。
另外,当我们同时有自定义的拦截器和网络拦截器时,会先执行自定义拦截器,然后才是网络拦截器(具体是在CallServerInterceptor拦截器之前执行)
- Call
发送请求的核心类,具体是通过Call的实现类RealCall来发送请求的。RealCall持有一个AsyncCall(内部类,该类是Runnable的子类。通过Callback接口回调请求的成功和失败)。在AsyncCall.execute()方法中通过RealInterceptorChain来依次执行拦截器发送请求。
----------RealCall类-------------
Response getResponseWithInterceptorChain() throws IOException {
// Build a full stack of interceptors.
List<Interceptor> interceptors = new ArrayList<>();
interceptors.addAll(client.interceptors());//先添加通过OkHttpClent配置的拦截器
//根据需要
interceptors.add(retryAndFollowUpInterceptor);//添加重定向拦截
/**
*从应用程序代码到网络代码之间的桥梁。首先,它从用户请求构建网络请求。然后它继续调用网络。
*最后,从网络响应构建用户响应。
*主要负责设置内容长度、编码方式、设置gzip压缩、添加请求头、cookie等相关功能
*/
interceptors.add(new BridgeInterceptor(client.cookieJar()));//添加桥接拦截器
interceptors.add(new CacheInterceptor(client.internalCache()));
interceptors.add(new ConnectInterceptor(client));
if (!forWebSocket) {
interceptors.addAll(client.networkInterceptors());
}
interceptors.add(new CallServerInterceptor(forWebSocket));
Interceptor.Chain chain = new RealInterceptorChain(interceptors, null, null, null, 0,
originalRequest, this, eventListener, client.connectTimeoutMillis(),
client.readTimeoutMillis(), client.writeTimeoutMillis());
return chain.proceed(originalRequest);
}
final class AsyncCall extends NamedRunnable {
private final Callback responseCallback;//请求回调接口
//该方法在run()中已经调用
@Override protected void execute() {
boolean signalledCallback = false;
try {
//通过拦截器链的执行获取请求响应
Response response = getResponseWithInterceptorChain();
if (retryAndFollowUpInterceptor.isCanceled()) {//
signalledCallback = true;
//回调失败
responseCallback.onFailure(RealCall.this, new IOException("Canceled"));
} else {
signalledCallback = true;
responseCallback.onResponse(RealCall.this, response);//回调成功
}
} catch (IOException e) {
if (signalledCallback) {
// Do not signal the callback twice!
Platform.get().log(INFO, "Callback failure for " + toLoggableString(), e);
} else {
eventListener.callFailed(RealCall.this, e);
responseCallback.onFailure(RealCall.this, e);
}
} finally {
//把AsyncCall移除请求队列
client.dispatcher().finished(this);
}
}
}
复制代码
- Request
该对象通过建造者模式配置Http请求参数。Call对象持有一个Request,通过它来获取每一个请求的相关信息。简单示例
val mediaType = MediaType.parse("application/json; charset=utf-8")
Request.Builder()
.url("请求链接")
.post(RequestBody.create(mediaType,"json字符串"))
.build()
复制代码
public final class Request {
final HttpUrl url;//请求链接
final String method;//请求方式,如GET、POST等。默认是GET
final Headers headers;//请求头
final @Nullable RequestBody body;//请求实体
final Object tag;
//缓存控制
private volatile CacheControl cacheControl; // Lazily initialized.
Request(Builder builder) {
this.url = builder.url;
this.method = builder.method;
this.headers = builder.headers.build();
this.body = builder.body;
this.tag = builder.tag != null ? builder.tag : this;
}
public Request build() {
if (url == null) throw new IllegalStateException("url == null");
return new Request(this);
}
}
复制代码
- Dispatcher
执行异步请求的策略。该对象持有三个队列(Deque<AsyncCall> readyAsyncCalls、Deque<AsyncCall> runningAsyncCalls、Deque<RealCall> runningSyncCalls,其中AsyncCall是Runnable的子类)、一个线程池ExecutorService(该对象是Executor的实现类,提供了终止线程的方法,并且能够追踪一个多个异步任务的过程)。
public final class Dispatcher {
private int maxRequests = 64;//最大请求数
private int maxRequestsPerHost = 5;//请求链接Host的最大数
private @Nullable Runnable idleCallback;
/** Executes calls. Created lazily. */
private @Nullable ExecutorService executorService;//线程池
//核心线程数为0,线程池最大容量为Integer.MAX_VALUE,线程存活时间为1分钟,同步队列
public synchronized ExecutorService executorService() {
if (executorService == null) {
executorService = new ThreadPoolExecutor(0, Integer.MAX_VALUE, 60, TimeUnit.SECONDS,
new SynchronousQueue<Runnable>(), Util.threadFactory("OkHttp Dispatcher", false));
}
return executorService;
}
/** Ready async calls in the order they'll be run. */
//准备运行的Runnable异步队列
private final Deque<AsyncCall> readyAsyncCalls = new ArrayDeque<>();
/** Running asynchronous calls. Includes canceled calls that haven't finished yet. */
//正在运行的Runnable异步队列,包括还未结束的
private final Deque<AsyncCall> runningAsyncCalls = new ArrayDeque<>();
/** Running synchronous calls. Includes canceled calls that haven't finished yet. */
//正在运行的Call同步队列,包括还未结束的
private final Deque<RealCall> runningSyncCalls = new ArrayDeque<>();
}
复制代码
2.网络请求分析
- 异步请求
Call.enqueue(Callback responseCallback)
------------RealCall类-------------
@Override
public void enqueue(Callback responseCallback) {
synchronized (this) {//判断RealCall是否正在执行
if (executed) throw new IllegalStateException("Already Executed");
executed = true;
}
**省略代码**
//调用OkHttpClient的Dispatcher对象执行enqueue()方法
client.dispatcher().enqueue(new AsyncCall(responseCallback));
}
------------Dispatcher类-------------
synchronized void enqueue(AsyncCall call) {
//判断runningAsyncCalls大小是否小于最大请求数,网络请求链接的host小于5
if (runningAsyncCalls.size() < maxRequests &&
runningCallsForHost(call) < maxRequestsPerHost) {
runningAsyncCalls.add(call);//把要执行AsyncCall加入队列中
executorService().execute(call);//调用ExecutorService线程池执行AsyncCall
} else {//否则加入readyAsyncCalls队列中,等待执行
readyAsyncCalls.add(call);
}
}
复制代码
- 同步请求
Call.execute()
------------RealCall类-------------
@Override public Response execute() throws IOException {
synchronized (this) {
if (executed) throw new IllegalStateException("Already Executed");
executed = true;
}
captureCallStackTrace();
eventListener.callStart(this);
try {
client.dispatcher().executed(this);//调用Dispatcher.executed(RealCall)
Response result = getResponseWithInterceptorChain();//同上分析
if (result == null) throw new IOException("Canceled");
return result;
} catch (IOException e) {
eventListener.callFailed(this, e);
throw e;
} finally {
client.dispatcher().finished(this);
}
}
------------Dispatcher类-------------
/** Used by {@code Call#execute} to signal it is in-flight. */
synchronized void executed(RealCall call) {
runningSyncCalls.add(call);//加入到队列中
}
复制代码