亲宝软件园·资讯

展开

Android OkHttp执行流程

Delusion 人气:0

前言

OkHttp 是一套处理 HTTP 网络请求的依赖库,由 Square 公司设计研发并开源,目前可以在 Java Kotlin 中使用。

对于 Android App 来说,OkHttp 现在几乎已经占据了所有的网络请求操作让,我们了解其内部实现原理可以更好地进行功能扩展、封装以及优化。

本文基于OkHttp 4.11.0 进行分析

OkHttp的具体使用可以参考官网,这里不做具体的说明,本文主要从OkHttp的使用入手,来具体分析OkHttp的实现原理。

介绍

OkHttp是通过socket和okio进行交换数据的

val client = OkHttpClient() 
val request = Request.Builder().get().url("http://xxx").build()
client.newCall(request).enqueue(object : Callback {
    override fun onFailure(call: Call, e: IOException) {
    }
    override fun onResponse(call: Call, response: Response) {
    }
})

从上面我们可以看到几个OkHttp重要的组成部分

执行流程

OkHttpClient

class Builder constructor() {
   //Okhttp 请求分发器,是整个OkhttpClient的执行核心
  internal var dispatcher: Dispatcher = Dispatcher()
  //Okhttp连接池,不过会把任务委托给RealConnectionPool处理
  internal var connectionPool: ConnectionPool = ConnectionPool()
  //用户定义的拦截器,在重试拦截器之前执行
  internal val interceptors: MutableList<Interceptor> = mutableListOf()
  //用户定义的网络拦截器,在CallServerInterceptor(执行网络请求拦截器)之前运行。
  internal val networkInterceptors: MutableList<Interceptor> = mutableListOf()
  //流程监听器
  internal var eventListenerFactory: EventListener.Factory = EventListener.NONE.asFactory()
  //连接失败时是否重连
  internal var retryOnConnectionFailure = true
  //服务器认证设置
  internal var authenticator: Authenticator = Authenticator.NONE
  //是否重定向
  internal var followRedirects = true
  //是否重定向到https
  internal var followSslRedirects = true
  //cookie持久化的设置
  internal var cookieJar: CookieJar = CookieJar.NO_COOKIES
  //缓存设置
  internal var cache: Cache? = null
  //DNS设置
  internal var dns: Dns = Dns.SYSTEM
  //代理设置
  internal var proxy: Proxy? = null
  internal var proxySelector: ProxySelector? = null
  internal var proxyAuthenticator: Authenticator = Authenticator.NONE
  //默认的socket连接池
  internal var socketFactory: SocketFactory = SocketFactory.getDefault()
  //用于https的socket连接池
  internal var sslSocketFactoryOrNull: SSLSocketFactory? = null
  //用于信任Https证书的对象
  internal var x509TrustManagerOrNull: X509TrustManager? = null
  internal var connectionSpecs: List<ConnectionSpec> = DEFAULT_CONNECTION_SPECS
  //http协议集合
  internal var protocols: List<Protocol> = DEFAULT_PROTOCOLS
  //https对host的检验
  internal var hostnameVerifier: HostnameVerifier = OkHostnameVerifier
  internal var certificatePinner: CertificatePinner = CertificatePinner.DEFAULT
  internal var certificateChainCleaner: CertificateChainCleaner? = null
  //请求超时
  internal var callTimeout = 0
  //连接超时
  internal var connectTimeout = 10_000
  //读取超时
  internal var readTimeout = 10_000
  //写入超时
  internal var writeTimeout = 10_000
  internal var pingInterval = 0
  internal var minWebSocketMessageToCompress = RealWebSocket.DEFAULT_MINIMUM_DEFLATE_SIZE
  internal var routeDatabase: RouteDatabase? = null
  }

client.newCall(request):

override fun newCall(request: Request): Call = RealCall(this, request, forWebSocket = false)

在这里生成一个RealCall对象,这里第三个参数是否为websocket,默认是false。 在拿到RealCall对象之后,这里有两种方式起发送网络请求:

RealCall.enqueue()

override fun enqueue(responseCallback: Callback) {
  check(executed.compareAndSet(false, true)) { "Already Executed" }
  callStart()
  client.dispatcher.enqueue(AsyncCall(responseCallback))
}
private fun callStart() {
  this.callStackTrace = Platform.get().getStackTraceForCloseable("response.body().close()")
  eventListener.callStart(this)
}

这里主要做了一下几步

Dispatcher.enqueue()

class Dispatcher constructor() {
    ......
//按运行顺序准备异步调用的队列
private val readyAsyncCalls = ArrayDeque<AsyncCall>()
//正在运行的异步请求队列, 包含取消但是还未finish的AsyncCall
private val runningAsyncCalls = ArrayDeque<AsyncCall>()
//正在运行的同步请求队列, 包含取消但是还未finish的RealCall
private val runningSyncCalls = ArrayDeque<RealCall>()
    ......
internal fun enqueue(call: AsyncCall) {
  synchronized(this) {
    readyAsyncCalls.add(call)
    if (!call.call.forWebSocket) {
      val existingCall = findExistingCallWithHost(call.host)
      if (existingCall != null) call.reuseCallsPerHostFrom(existingCall)
    }
  }
  promoteAndExecute()
}
private fun findExistingCallWithHost(host: String): AsyncCall? {
  for (existingCall in runningAsyncCalls) {
    if (existingCall.host == host) return existingCall
  }
  for (existingCall in readyAsyncCalls) {
    if (existingCall.host == host) return existingCall
  }
  return null
}
private fun promoteAndExecute(): Boolean {
  this.assertThreadDoesntHoldLock()
  val executableCalls = mutableListOf<AsyncCall>()
    //判断是否有请求正在执行
  val isRunning: Boolean
   //加锁,保证线程安全
  synchronized(this) {
   //遍历 readyAsyncCalls 队列
    val i = readyAsyncCalls.iterator()
    while (i.hasNext()) {
      val asyncCall = i.next()
       //runningAsyncCalls的数量不能大于最大并发请求数 64
      if (runningAsyncCalls.size >= this.maxRequests) break // Max capacity.
      //同一Host的最大数是5
      if (asyncCall.callsPerHost.get() >= this.maxRequestsPerHost) continue // Host max capacity.
       //从readyAsyncCalls队列中移除并加入到executableCalls和runningAsyncCalls中
      i.remove()
      asyncCall.callsPerHost.incrementAndGet()
      executableCalls.add(asyncCall)
      runningAsyncCalls.add(asyncCall)
    }
    isRunning = runningCallsCount() > 0
  }
    //遍历executableCalls 执行asyncCall
  for (i in 0 until executableCalls.size) {
    val asyncCall = executableCalls[i]
    asyncCall.executeOn(executorService)
  }
  return isRunning
}

在这里遍历readyAsyncCalls队列,判断runningAsyncCalls的数量是否大于最大并发请求数64, 判断同一Host的请求是否大于5,然后将AsyncCall从readyAsyncCalls队列中移除,并加入到executableCalls和runningAsyncCalls中,遍历executableCalls 执行asyncCall.

internal inner class AsyncCall(
  private val responseCallback: Callback
) : Runnable {
     ......
  fun executeOn(executorService: ExecutorService) {
    client.dispatcher.assertThreadDoesntHoldLock()
    var success = false
    try {
       //执行AsyncCall 的run方法
      executorService.execute(this)
      success = true
    } catch (e: RejectedExecutionException) {
      val ioException = InterruptedIOException("executor rejected")
      ioException.initCause(e)
      noMoreExchanges(ioException)
      responseCallback.onFailure(this@RealCall, ioException)
    } finally {
      if (!success) {
        client.dispatcher.finished(this) // This call is no longer running!
      }
    }
  }
  override fun run() {
    threadName("OkHttp ${redactedUrl()}") {
      var signalledCallback = false
      timeout.enter()
      try {
         //执行OkHttp的拦截器  获取response对象
        val response = getResponseWithInterceptorChain()
        signalledCallback = true
        //通过该方法将response对象回调出去
        responseCallback.onResponse(this@RealCall, response)
      } catch (e: IOException) {
        if (signalledCallback) {
          Platform.get().log("Callback failure for ${toLoggableString()}", Platform.INFO, e)
        } else {
            //遇到IO异常  回调失败方法
          responseCallback.onFailure(this@RealCall, e)
        }
      } catch (t: Throwable) {
         //遇到其他异常  回调失败方法
        cancel()
        if (!signalledCallback) {
          val canceledException = IOException("canceled due to $t")
          canceledException.addSuppressed(t)
          responseCallback.onFailure(this@RealCall, canceledException)
        }
        throw t
      } finally {
        client.dispatcher.finished(this)
      }
    }
  }
}

这里可以看到AsyncCall就是一个Runable对象,线程执行就会调用该对象的run方法,而executeOn方法就是执行runable对象. 在run方法中主要执行了以下几步:

@Throws(IOException::class)
internal fun getResponseWithInterceptorChain(): Response {
  // 拦截器集合
  val interceptors = mutableListOf<Interceptor>()
  //添加用户自定义集合
  interceptors += client.interceptors
  interceptors += RetryAndFollowUpInterceptor(client)
  interceptors += BridgeInterceptor(client.cookieJar)
  interceptors += CacheInterceptor(client.cache)
  interceptors += ConnectInterceptor
  //如果不是sockect 添加newtwork拦截器
  if (!forWebSocket) {
    interceptors += client.networkInterceptors
  }
  interceptors += CallServerInterceptor(forWebSocket)
    //构建拦截器责任链
  val chain = RealInterceptorChain(
      call = this,
      interceptors = interceptors,
      index = 0,
      exchange = null,
      request = originalRequest,
      connectTimeoutMillis = client.connectTimeoutMillis,
      readTimeoutMillis = client.readTimeoutMillis,
      writeTimeoutMillis = client.writeTimeoutMillis
  )
  var calledNoMoreExchanges = false
  try {
     //执行拦截器责任链获取Response
    val response = chain.proceed(originalRequest)
    //如果取消了  则抛出异常
    if (isCanceled()) {
      response.closeQuietly()
      throw IOException("Canceled")
    }
    return response
  } catch (e: IOException) {
    calledNoMoreExchanges = true
    throw noMoreExchanges(e) as Throwable
  } finally {
    if (!calledNoMoreExchanges) {
      noMoreExchanges(null)
    }
  }
}

在这里主要执行了以下几步操作

简单的总结一下:这里才用了责任链设计模式,构建RealInterceptorChain对象,然后执行proceed方法获取response对象

fun interface Interceptor {
    //拦截方法
  @Throws(IOException::class)
  fun intercept(chain: Chain): Response
  companion object {
    inline operator fun invoke(crossinline block: (chain: Chain) -> Response): Interceptor =
      Interceptor { block(it) }
  }
  interface Chain {
     //获取Request对象
    fun request(): Request
    //处理请求获取Reponse
    @Throws(IOException::class)
    fun proceed(request: Request): Response
    ......
  }
}
class RealInterceptorChain(
  internal val call: RealCall,
  private val interceptors: List<Interceptor>,
  private val index: Int,
  internal val exchange: Exchange?,
  internal val request: Request,
  internal val connectTimeoutMillis: Int,
  internal val readTimeoutMillis: Int,
  internal val writeTimeoutMillis: Int
) : Interceptor.Chain {
  internal fun copy(
    index: Int = this.index,
    exchange: Exchange? = this.exchange,
    request: Request = this.request,
    connectTimeoutMillis: Int = this.connectTimeoutMillis,
    readTimeoutMillis: Int = this.readTimeoutMillis,
    writeTimeoutMillis: Int = this.writeTimeoutMillis
  ) = RealInterceptorChain(call, interceptors, index, exchange, request, connectTimeoutMillis,
      readTimeoutMillis, writeTimeoutMillis)
      ......
  override fun call(): Call = call
  override fun request(): Request = request
  @Throws(IOException::class)
  override fun proceed(request: Request): Response {
    check(index < interceptors.size)
     ......
    val next = copy(index = index + 1, request = request)
    val interceptor = interceptors[index]
    @Suppress("USELESS_ELVIS")
    val response = interceptor.intercept(next) ?: throw NullPointerException(
        "interceptor $interceptor returned null")
     ......
    return response
  }
}

这里看一看到copy()方法就是创建了一个RealInterceptorChain()对象,不过需要注意的是index在创建对象时是index = index + 1,这样就会执行index对应下标的拦截器,不断的调用下一个拦截器,直到有response对象返回,也就是chain.proceed(originalRequest)结束。

Interceptor

下面我们来具体分析一下拦截器

RetryAndFollowUpInterceptor

主要处理了如下几个方向的问题:

@Throws(IOException::class)
override fun intercept(chain: Interceptor.Chain): Response {
val realChain = chain as RealInterceptorChain
var request = chain.request
val call = realChain.call
var followUpCount = 0
var priorResponse: Response? = null
var newExchangeFinder = true
var recoveredFailures = listOf<IOException>()
while (true) {
  //这里会新建一个ExchangeFinder,ConnectInterceptor会使用到
  call.enterNetworkInterceptorExchange(request, newExchangeFinder)
  var response: Response
  var closeActiveExchange = true
  try {
    if (call.isCanceled()) {
      throw IOException("Canceled")
    }
    try {
      response = realChain.proceed(request)
      newExchangeFinder = true
    } catch (e: RouteException) {
      //尝试通过路由连接失败。该请求将不会被发送。
      if (!recover(e.lastConnectException, call, request, requestSendStarted = false)) {
        throw e.firstConnectException.withSuppressed(recoveredFailures)
      } else {
        recoveredFailures += e.firstConnectException
      }
      newExchangeFinder = false
      continue
    } catch (e: IOException) {
      //尝试与服务器通信失败。该请求可能已发送。
      if (!recover(e, call, request, requestSendStarted = e !is ConnectionShutdownException)) {
        throw e.withSuppressed(recoveredFailures)
      } else {
        recoveredFailures += e
      }
      newExchangeFinder = false
      continue
    }
   //尝试关联上一个response,注意:body是为null
    if (priorResponse != null) {
      response = response.newBuilder()
          .priorResponse(priorResponse.newBuilder()
              .body(null)
              .build())
          .build()
    }
    val exchange = call.interceptorScopedExchange
    //会根据 responseCode 来判断,构建一个新的request并返回来重试或者重定向
    val followUp = followUpRequest(response, exchange)
    if (followUp == null) {
      if (exchange != null && exchange.isDuplex) {
        call.timeoutEarlyExit()
      }
      closeActiveExchange = false
      return response
    }
    //如果请求体是一次性的,不需要再次重试
    val followUpBody = followUp.body
    if (followUpBody != null && followUpBody.isOneShot()) {
      closeActiveExchange = false
      return response
    }
    response.body?.closeQuietly()
    //最大重试次数,不同的浏览器是不同的,比如:Chrome为21,Safari则是16
    if (++followUpCount > MAX_FOLLOW_UPS) {
        throw ProtocolException("Too many follow-up requests: $followUpCount")
    }
    request = followUp
    priorResponse = response
  } finally {
    call.exitNetworkInterceptorExchange(closeActiveExchange)
  }
}
}
private fun recover(
  e: IOException,
  call: RealCall,
  userRequest: Request,
  requestSendStarted: Boolean
): Boolean {
  //禁止重连
  if (!client.retryOnConnectionFailure) return false
  // 不能再次发送请求体
  if (requestSendStarted && requestIsOneShot(e, userRequest)) return false
  // 致命异常
  if (!isRecoverable(e, requestSendStarted)) return false
  // 没有更多线路可以重连
  if (!call.retryAfterFailure()) return false
  // 对于故障恢复,将相同的路由选择器与新连接一起使用
  return true
}

BridgeInterceptor

主要处理了如下几个问题:

@Throws(IOException::class)
override fun intercept(chain: Interceptor.Chain): Response {
  //获取原始请求数据
  val userRequest = chain.request()
  val requestBuilder = userRequest.newBuilder()
   //重新构建请求 添加一些必要的请求头信息
  val body = userRequest.body
  if (body != null) {
    val contentType = body.contentType()
    if (contentType != null) {
      requestBuilder.header("Content-Type", contentType.toString())
    }
    val contentLength = body.contentLength()
    if (contentLength != -1L) {
      requestBuilder.header("Content-Length", contentLength.toString())
      requestBuilder.removeHeader("Transfer-Encoding")
    } else {
      requestBuilder.header("Transfer-Encoding", "chunked")
      requestBuilder.removeHeader("Content-Length")
    }
  }
  if (userRequest.header("Host") == null) {
    requestBuilder.header("Host", userRequest.url.toHostHeader())
  }
  if (userRequest.header("Connection") == null) {
    requestBuilder.header("Connection", "Keep-Alive")
  }
  var transparentGzip = false
  if (userRequest.header("Accept-Encoding") == null && userRequest.header("Range") == null) {
    transparentGzip = true
    requestBuilder.header("Accept-Encoding", "gzip")
  }
  val cookies = cookieJar.loadForRequest(userRequest.url)
  if (cookies.isNotEmpty()) {
    requestBuilder.header("Cookie", cookieHeader(cookies))
  }
  if (userRequest.header("User-Agent") == null) {
    requestBuilder.header("User-Agent", userAgent)
  }
    //执行下一个拦截器
  val networkResponse = chain.proceed(requestBuilder.build())
  cookieJar.receiveHeaders(userRequest.url, networkResponse.headers)
  //创建一个新的responseBuilder,目的是将原始请求数据构建到response中
  val responseBuilder = networkResponse.newBuilder()
      .request(userRequest)
  if (transparentGzip &&
      "gzip".equals(networkResponse.header("Content-Encoding"), ignoreCase = true) &&
      networkResponse.promisesBody()) {
    val responseBody = networkResponse.body
    if (responseBody != null) {
      val gzipSource = GzipSource(responseBody.source())
      val strippedHeaders = networkResponse.headers.newBuilder()
          .removeAll("Content-Encoding")
          .removeAll("Content-Length")
          .build()
       //修改response header信息,移除Content-Encoding,Content-Length信息
      responseBuilder.headers(strippedHeaders)
      val contentType = networkResponse.header("Content-Type"
     //修改response body信息
      responseBuilder.body(RealResponseBody(contentType, -1L, gzipSource.buffer()))
    }
  }
  return responseBuilder.build()
}

CacheInterceptor

用户通过OkHttpClient.cache来配置缓存,缓存拦截器通过CacheStrategy来判断是使用网络还是缓存来构建response

@Throws(IOException::class)
override fun intercept(chain: Interceptor.Chain): Response {
  val call = chain.call()
  //通过request从OkHttpClient.cache中获取缓存
  val cacheCandidate = cache?.get(chain.request())
  val now = System.currentTimeMillis()
 //创建缓存策略
  val strategy = CacheStrategy.Factory(now, chain.request(), cacheCandidate).compute()
    //为空表示不使用网络,反之,则表示使用网络
    val networkRequest = strategy.networkRequest
    //为空表示不使用缓存,反之,则表示使用缓存
    val cacheResponse = strategy.cacheResponse
    //追踪网络与缓存的使用情况
    cache?.trackResponse(strategy)
    val listener = (call as? RealCall)?.eventListener ?: EventListener.NONE
    //有缓存但不适用,关闭它
    if (cacheCandidate != null && cacheResponse == null) {
      cacheCandidate.body?.closeQuietly()
    }
    //如果网络被禁止,但是缓存又是空的,构建一个code为504的response,并返回
  if (networkRequest == null && cacheResponse == null) {
    return Response.Builder()
        .request(chain.request())
        .protocol(Protocol.HTTP_1_1)
        .code(HTTP_GATEWAY_TIMEOUT)
        .message("Unsatisfiable Request (only-if-cached)")
        .body(EMPTY_RESPONSE)
        .sentRequestAtMillis(-1L)
        .receivedResponseAtMillis(System.currentTimeMillis())
        .build().also {
          listener.satisfactionFailure(call, it)
        }
  }
 //如果我们禁用了网络不使用网络,且有缓存,直接根据缓存内容构建并返回response
  if (networkRequest == null) {
    return cacheResponse!!.newBuilder()
        .cacheResponse(stripBody(cacheResponse))
        .build().also {
          listener.cacheHit(call, it)
        }
  }
   //为缓存添加监听
  if (cacheResponse != null) {
    listener.cacheConditionalHit(call, cacheResponse)
  } else if (cache != null) {
    listener.cacheMiss(call)
  }
  var networkResponse: Response? = null
  try {
      //执行下一个拦截器
    networkResponse = chain.proceed(networkRequest)
  } finally {
    //捕获I/O或其他异常,请求失败,networkResponse为空,且有缓存的时候,不暴露缓存内容
    if (networkResponse == null && cacheCandidate != null) {
       //否则关闭缓存响应体
      cacheCandidate.body?.closeQuietly()
    }
  }
    //如果有缓存
  if (cacheResponse != null) {
    //且网络返回response code为304的时候,使用缓存内容新构建一个Response返回。
    if (networkResponse?.code == HTTP_NOT_MODIFIED) {
      val response = cacheResponse.newBuilder()
          .headers(combine(cacheResponse.headers, networkResponse.headers))
          .sentRequestAtMillis(networkResponse.sentRequestAtMillis)
          .receivedResponseAtMillis(networkResponse.receivedResponseAtMillis)
          .cacheResponse(stripBody(cacheResponse))
          .networkResponse(stripBody(networkResponse))
          .build()
      networkResponse.body!!.close()
      cache!!.trackConditionalCacheHit()
      cache.update(cacheResponse, response)
      return response.also {
        listener.cacheHit(call, it)
      }
    } else {
    //否则关闭缓存响应体
      cacheResponse.body?.closeQuietly()
    }
  }
  //构建网络请求的response
  val response = networkResponse!!.newBuilder()
      .cacheResponse(stripBody(cacheResponse))
      .networkResponse(stripBody(networkResponse))
      .build()
    //如果cache不为null,即用户在OkHttpClient中配置了缓存,则将上一步新构建的网络请求response存到cache中
  if (cache != null) {
    //根据response的code,header以及CacheControl.noStore来判断是否可以缓存
    if (response.promisesBody() && CacheStrategy.isCacheable(response, networkRequest)) {
     // 将该response存入缓存
      val cacheRequest = cache.put(response)
      return cacheWritingResponse(cacheRequest, response).also {
        if (cacheResponse != null) {
          listener.cacheMiss(call)
        }
      }
    }
    //根据请求方法来判断缓存是否有效,只对Get请求进行缓存,其它方法的请求则移除
    if (HttpMethod.invalidatesCache(networkRequest.method)) {
      try {
         //缓存无效,将该请求缓存从client缓存配置中移除
        cache.remove(networkRequest)
      } catch (_: IOException) {
      }
    }
  }
  return response
}

网络请求前:

网络请求后:

从LruCache中获取缓存

val cacheCandidate = cache?.get(chain.request())
internal fun get(request: Request): Response? {
  val key = key(request.url)
  val snapshot: DiskLruCache.Snapshot = try {
    cache[key] ?: return null
  } catch (_: IOException) {
    return null // Give up because the cache cannot be read.
  }
  val entry: Entry = try {
    Entry(snapshot.getSource(ENTRY_METADATA))
  } catch (_: IOException) {
    snapshot.closeQuietly()
    return null
  }
  val response = entry.response(snapshot)
  if (!entry.matches(request, response)) {
    response.body?.closeQuietly()
    return null
  }
  return response
}
@JvmStatic
fun key(url: HttpUrl): String = url.toString().encodeUtf8().md5().hex()

DiskLruCache:

@Synchronized @Throws(IOException::class)
operator fun get(key: String): Snapshot? {
  initialize()
  checkNotClosed()
  validateKey(key)
  val entry = lruEntries[key] ?: return null
  val snapshot = entry.snapshot() ?: return null
  redundantOpCount++
  journalWriter!!.writeUtf8(READ)
      .writeByte(' '.toInt())
      .writeUtf8(key)
      .writeByte('\n'.toInt())
  if (journalRebuildRequired()) {
    cleanupQueue.schedule(cleanupTask)
  }
  return snapshot
}

再来看看那些响应体需要缓存:

这里是网络请求回来,判断是否需要缓存的处理

if (cache != null) {
  if (response.promisesBody() && CacheStrategy.isCacheable(response, networkRequest)) {
    val cacheRequest = cache.put(response)
    return cacheWritingResponse(cacheRequest, response).also {
      if (cacheResponse != null) {
        listener.cacheMiss(call)
      }
    }
  }
  if (HttpMethod.invalidatesCache(networkRequest.method)) {
    try {
      cache.remove(networkRequest)
    } catch (_: IOException) {
    }
  }
}
fun isCacheable(response: Response, request: Request): Boolean {
  when (response.code) {
    HTTP_OK,
    HTTP_NOT_AUTHORITATIVE,
    HTTP_NO_CONTENT,
    HTTP_MULT_CHOICE,
    HTTP_MOVED_PERM,
    HTTP_NOT_FOUND,
    HTTP_BAD_METHOD,
    HTTP_GONE,
    HTTP_REQ_TOO_LONG,
    HTTP_NOT_IMPLEMENTED,
    StatusLine.HTTP_PERM_REDIRECT -> {
    }
    HTTP_MOVED_TEMP,
    StatusLine.HTTP_TEMP_REDIRECT -> {
      if (response.header("Expires") == null &&
          response.cacheControl.maxAgeSeconds == -1 &&
          !response.cacheControl.isPublic &&
          !response.cacheControl.isPrivate) {
        return false
      }
    }
    else -> {
      return false
    }
  }
  return !response.cacheControl.noStore && !request.cacheControl.noStore
}

如果状态码为200203204301404405410414501308 都可以缓存,其他则返回false 不进行缓存

加载全部内容

相关教程
猜你喜欢
用户评论