okhttp Interceptor

简介: okhttp Interceptor

Interceptor介绍


okhttp的拦截器就是将整个请求网络的过程的每一步都封装在不同的Interceptor里,这样说可能有点绕,简单点说就是把一个List里的Interceptor都顺序执行一遍,那么整个网络请求过程就完成了

@Throws(IOException::class)
  internal fun getResponseWithInterceptorChain(): Response {
    // Build a full stack of interceptors.
    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)
    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 {
      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)
      }
    }
  }

 

 

 

  • RetryAndFollowUpInterceptor:用于重定向和发生错误时重试
  • BridgeInterceptor:应用层与网络层的桥梁,从代码中看主要是为request添加请求头,为response去除响应头
  • CacheInterceptor:处理请求与响应缓存
  • ConnectInterceptor:与服务器建立连接
  • CallServerInterceptor:责任链中最后一个拦截器,用最终得到的request发送请求,将获取到response返回给前面的拦截器处理

这样application interceptornetwork interceptor的区别就很明显了,应用拦截器不会关注重定向和错误重试等操作,并且获取到的 requestresponse并不是网络层的。而网络拦截器则与之相反,如果发生了重定向和重试,那么会被调用多次,获取的requestresponse是完整的。

Application interceptors

  • 不用关注重定向、重试等中间响应
  • 只会调用一次
  • 只需关注应用发起请求的意图,不用关心okhttp为其添加的头部信息,比如 If-None-Match
  • 可以不调用Chain.proceed()不发送请求
  • 可以多次调用Chain.proceed()来重试请求

Network Interceptors

  • 可以操作重定向、重试等中间响应
  • 当返回缓存的响应时不会被调用
  • 可以获取到网络传输中真实的请求与响应
  • 可以访问带有请求的连接

Interceptor介绍

我们先看拦截器的接口定义:

public interface Interceptor {
    //拦截处理
    Response intercept(Chain chain) throws IOException;
    interface Chain {
        //获取请求的request
          Request request();
        //处理request获取response
        Response proceed(Request request) throws IOException;
    
        /**
         * Returns the connection the request will be executed on. This is only available in the chains
         * of network interceptors; for application interceptors this is always null.
         */
        @Nullable Connection connection();
    }
}


实例

比如我们现在有这样一个需求,为了保护用户远离甫田系,要将所有请求某竞价排名公司(baidu)的接口地址都直接替换成 www.soso.com

import android.util.Log;
import java.io.IOException;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;
public class KeepAwayBaiduInterceptor implements Interceptor {
    private static final String TAG = "KeepAwayBaiduInterceptor";
    @Override
    public Response intercept(Chain chain) throws IOException {
        Request request = chain.request();
        Log.i(TAG, "intercept host: " + request.url().host());
        if (request.url().host().equals("www.baidu.com")) {
            //如果发现host属于该公司地址,就基于原来的request生成一个新的request,并使用新的url地址,这样之前request里的信息就都保留了
            Request newRequest = request.newBuilder().url("http://www.soso.com").build();
            return chain.proceed(newRequest);
        } else {
            return chain.proceed(request);
        }
    }
}

我们再看一下如何okhttp请求里使用这个自定义的Interceptor

  • okhttp发起请求代码
public void doRequest(){
    //创建okHttpClient对象
    OkHttpClient mOkHttpClient = new OkHttpClient.Builder()
            .addInterceptor(new KeepAwayBaiduInterceptor())//在此处添加我们的拦截器
            .build();
    //创建一个该竞价排名公司的Request
    final Request request = new Request.Builder()
            .url("http://www.baidu.com")
            .build();
    Call call = mOkHttpClient.newCall(request);
    //请求加入调度
    call.enqueue(new Callback()
    {
        @Override
        public void onFailure(@NonNull Call call, @NonNull final IOException e) {
        }
        @Override
        public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException
        {
            final String htmlStr =  response.body().string();
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    textView.setText("response:"+htmlStr);
                }
            });
        }
    });
}

总结

okhttp的拦截器就是在intercept(Chain chain)的回调中对Request和Response进行修改,然后通过chain.proceed(request)调起下一个拦截器。在okhttp中,网络连接也是一个拦截器(CallServerInterceptor),他是最后一个被调用的,负责将request写入网络流中,并从网络流中读取服务器返回的信息写入Response中返回给客户端

okhttpinterceptor采用的是责任链模式,在这条责任链中其中,前面的interceptor根据自己的需求处理request对象,处理完之后将其交给下一个interceptor,也就是上面代码中的chain.proceed(request)方法,然后等待下一个拦截器返回一个response,再对返回的结果进行处理,最终给请求的发起者返回一个响应结果。


目录
相关文章
|
SQL BI 数据库
达梦(DM) SQL日期操作及分析函数
讲述DM 数据库中如何实现各种日期相关的运算以及如何利用分析函数 lead() over() 进行范围问题的处理
mac zsh: command not found: pip解决方法
mac zsh: command not found: pip解决方法
1747 0
Postman - 快速分享接口
Postman - 快速分享接口
2286 0
Postman - 快速分享接口
|
1月前
|
人工智能 安全 应用服务中间件
OpenClaw(Clawdbot)攻略:零基础阿里云镜像一键部署+安全加固与技能拓展实战
2026年,AI Agent技术进入全民普及阶段,OpenClaw(曾用名Clawdbot、Moltbot)凭借“开源可控、本地优先、任务驱动”的核心优势,迅速成为个人与轻量团队搭建专属AI助手的首选方案。这款由奥地利开发者Peter Steinberger打造的工具,彻底打破了传统AI“只会说不会做”的局限,通过集成海量Skills插件,可完成文件管理、代码开发、会议纪要整理、多模态解析等实际工作,堪称“7×24小时不下班的数字员工”。
872 14
|
Android开发
KernelSU基于内核的 SU。它通过自定义内核,直接在内核中赋予目标进程 root 权限。
KernelSU基于内核的 SU。它通过自定义内核,直接在内核中赋予目标进程 root 权限。
4914 0
|
关系型数据库 分布式数据库 数据库
云栖大会|从数据到决策:AI时代数据库如何实现高效数据管理?
在2024云栖大会「海量数据的高效存储与管理」专场,阿里云瑶池讲师团携手AMD、FunPlus、太美医疗科技、中石化、平安科技以及小赢科技、迅雷集团的资深技术专家深入分享了阿里云在OLTP方向的最新技术进展和行业最佳实践。
|
人工智能 搜索推荐 算法
解决方案评测|主动式智能导购AI助手构建
阿里云的主动式智能导购AI助手是电商商家提升用户体验和销量的利器。它能实时分析用户行为,提供个性化推荐,支持多渠道无缝对接,并具备语音和文本交互功能。通过注册阿里云账号、开通服务、配置项目、设置推荐策略、集成到平台并测试优化,商家可以轻松部署这一工具。关键代码示例帮助理解API对接和数据处理。建议增强个性化推荐算法、优化交互体验并增加自定义选项,以进一步提升效果。
926 11
|
安全 网络安全 数据安全/隐私保护
基于springboot项目的钉钉消息发送
基于springboot项目的钉钉消息发送
|
存储 索引 Windows
操作系统(13)-----文件管理2
操作系统(13)-----文件管理
824 0

热门文章

最新文章