okhttp学习系列之CallServerInterceptor拦截器

这个拦截器是干嘛的

前面我们已经通过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用的(存疑)
     
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值