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 interceptor和network interceptor的区别就很明显了,应用拦截器不会关注重定向和错误重试等操作,并且获取到的 request和response并不是网络层的。而网络拦截器则与之相反,如果发生了重定向和重试,那么会被调用多次,获取的request和response是完整的。

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中返回给客户端

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


目录
相关文章
|
网络协议 Android开发 Python
Android 抓包工具r0capture使用
Android 抓包工具r0capture使用
2339 1
mac zsh: command not found: pip解决方法
mac zsh: command not found: pip解决方法
2246 0
|
数据安全/隐私保护 iOS开发
使用HBuilder平台打包并上传iOS应用到App Store的完整教程
使用HBuilder平台打包并上传iOS应用到App Store的完整教程
|
Android开发
KernelSU基于内核的 SU。它通过自定义内核,直接在内核中赋予目标进程 root 权限。
KernelSU基于内核的 SU。它通过自定义内核,直接在内核中赋予目标进程 root 权限。
8986 0
|
8月前
|
数据采集 网络协议 算法
IPv6地理库如何建设?从被动探测到主动订阅的实践解析
全球IPv4枯竭加速IPv6部署,我国IPv6活跃用户已超8亿。本文解析IPv6地理定位三大挑战,揭示IP数据云等平台如何依托Geofeed订阅、BGP解析等主动溯源技术,构建高精度、日更级IPv6地理库。
|
10月前
|
人工智能 安全 应用服务中间件
OpenClaw(Clawdbot)攻略:零基础阿里云镜像一键部署+安全加固与技能拓展实战
2026年,AI Agent技术进入全民普及阶段,OpenClaw(曾用名Clawdbot、Moltbot)凭借“开源可控、本地优先、任务驱动”的核心优势,迅速成为个人与轻量团队搭建专属AI助手的首选方案。这款由奥地利开发者Peter Steinberger打造的工具,彻底打破了传统AI“只会说不会做”的局限,通过集成海量Skills插件,可完成文件管理、代码开发、会议纪要整理、多模态解析等实际工作,堪称“7×24小时不下班的数字员工”。
1533 14
|
缓存 数据处理 Android开发
Android经典实战之Kotlin常用的 Flow 操作符
本文介绍 Kotlin 中 `Flow` 的多种实用操作符,包括转换、过滤、聚合等,通过简洁易懂的例子展示了每个操作符的功能,如 `map`、`filter` 和 `fold` 等,帮助开发者更好地理解和运用 `Flow` 来处理异步数据流。
868 4
|
Android开发 数据安全/隐私保护
安卓手机修改设备id, 安卓硬改一键新机,手机机型修改(伪装)
提供了完整的设备信息修改功能,包含设备模板配置、基础信息修改、网络信息修改、模拟器检测绕
|
XML 搜索推荐 Android开发
Android改变进度条控件progressbar的样式(根据源码修改)
本文介绍了如何基于Android源码自定义ProgressBar样式。首先分析了系统源码中ProgressBar样式的定义,发现其依赖一张旋转图片实现动画效果。接着分两步指导开发者实现自定义:1) 模仿源码创建一个旋转动画XML文件(放置在drawable文件夹),修改图片为自定义样式;2) 在UI控件中通过`indeterminateDrawable`属性应用该动画。最终实现简单且个性化的ProgressBar效果,附带效果图展示。
827 2
|
缓存 应用服务中间件 Apache
HTTP 范围Range请求
HTTP范围请求是一种强大的技术,允许客户端请求资源的部分内容,提高了传输效率和用户体验。通过正确配置服务器和实现范围请求,可以在视频流、断点续传下载等场景中发挥重要作用。希望本文提供的详细介绍和示例代码能帮助您更好地理解和应用这一技术。
1422 19