文章目录
这个拦截器是干嘛的
前面我们已经通过ConnectInterceptor拦截器建立了连接(参见ConnectInterceptor拦截器), 那CallServerInterceptor就是发起真正的网络请求, 他是最后一个拦截器
val interceptors = mutableListOf<Interceptor>()
interceptors += client.interceptors
interceptors += RetryAndFollowUpInterceptor(client)
interceptors += BridgeInterceptor(client.cookieJar)
interceptors += CacheInterceptor(client.cache)
interceptors += ConnectInterceptor
if (!forWebSocket) {
interceptors += client.networkInterceptors
}
//偶没有骗你, 他真的是最后一个拦截器
interceptors += CallServerInterceptor(forWebSocket)
主要流程
流程如下, 注意, 我们基于http1.1来进行讨论, 请求头里没有100-continue, 一切都以最简单的来
走读下代码
override fun intercept(chain: Interceptor.Chain): Response {
... 这里不用看了
try {
// 写入http header
exchange.writeRequestHeaders(request)
if (HttpMethod.permitsRequestBody(request.method) && requestBody != null) {
// 只有非GET和Head的请求, 才会写body
if ("100-continue".equals(request.header("Expect"), ignoreCase = true)) {
... 这里流程忽略, 这里是给100-continue用的, 先不看吧
}
if (responseBuilder == null) {
if (requestBody.isDuplex()) {
... 这里也不看, 应该是给http2用的(存疑)