OkHttp 的拦截器有哪些

OkHttp 的拦截器主要分为两大类:内置拦截器自定义拦截器。以下是详细的分类和说明:


一、内置拦截器(OkHttp 自动添加)

这些拦截器由 OkHttp 内部自动管理,按照固定顺序执行:

  1. RetryAndFollowUpInterceptor

    • 处理请求失败后的重试和 HTTP 重定向(如 301/302 状态码)
    • 自动处理认证挑战(如 401 认证失败)
  2. BridgeInterceptor

    • 补全缺失的请求头(如 Content-TypeHostUser-Agent
    • 自动处理 Gzip 压缩(添加 Accept-Encoding 头,解压响应体)
  3. CacheInterceptor

    • 根据缓存策略管理本地缓存
    • 处理条件请求(如 If-Modified-Since 头)
  4. ConnectInterceptor

    • 建立与服务器的 TCP/TLS 连接
    • 管理连接池(复用已有连接)
  5. CallServerInterceptor

    • 最终向服务器发送请求并读取响应
    • 处理请求体和响应体的读写

二、自定义拦截器(开发者手动添加)

通过 OkHttpClient.Builder 添加,分为两种类型:

1. 应用拦截器(Application Interceptors)
  • 添加方式.addInterceptor()
  • 特点
    • 最先执行,最后获得响应
    • 能拦截所有请求(包括缓存响应)
    • 无法访问 Connection 对象(如 TLS 信息)
  • 典型用途
    • 统一添加请求头(如 Token)
    • 全局日志记录
    • 请求/响应数据加解密
kotlin 复制代码
val client = OkHttpClient.Builder()
    .addInterceptor(LoggingInterceptor()) // 应用拦截器
    .build()
2. 网络拦截器(Network Interceptors)
  • 添加方式.addNetworkInterceptor()
  • 特点
    • 仅在建立网络连接时触发(不拦截缓存响应)
    • 可以访问 Connection 对象(如服务器 IP、TLS 协议)
    • 会跟随重定向和重试
  • 典型用途
    • 网络层监控(如统计请求耗时)
    • 修改重试策略
    • 调试网络细节
kotlin 复制代码
val client = OkHttpClient.Builder()
    .addNetworkInterceptor(StethoInterceptor()) // 网络拦截器
    .build()

三、常用自定义拦截器示例

1. 日志拦截器
kotlin 复制代码
class LoggingInterceptor : Interceptor {
    override fun intercept(chain: Interceptor.Chain): Response {
        val request = chain.request()
        // 记录请求信息
        val response = chain.proceed(request)
        // 记录响应信息
        return response
    }
}
2. 认证拦截器
kotlin 复制代码
class AuthInterceptor(private val token: String) : Interceptor {
    override fun intercept(chain: Interceptor.Chain): Response {
        val newRequest = chain.request()
            .newBuilder()
            .header("Authorization", "Bearer $token")
            .build()
        return chain.proceed(newRequest)
    }
}
3. 缓存控制拦截器
kotlin 复制代码
class CacheControlInterceptor : Interceptor {
    override fun intercept(chain: Interceptor.Chain): Response {
        val request = chain.request()
        val modifiedRequest = request.newBuilder()
            .header("Cache-Control", "public, max-age=60")
            .build()
        return chain.proceed(modifiedRequest)
    }
}
4. 请求重试拦截器
kotlin 复制代码
class RetryInterceptor(private val maxRetries: Int) : Interceptor {
    override fun intercept(chain: Interceptor.Chain): Response {
        var lastException: IOException? = null
        for (i in 1..maxRetries) {
            try {
                return chain.proceed(chain.request())
            } catch (e: IOException) {
                lastException = e
                Thread.sleep(i * 1000L) // 指数退避
            }
        }
        throw lastException!!
    }
}

四、拦截器执行顺序图示

复制代码
应用拦截器(开发者添加)
↓
RetryAndFollowUpInterceptor(内置)
↓
BridgeInterceptor(内置)
↓
CacheInterceptor(内置)
↓
ConnectInterceptor(内置)
↓
网络拦截器(开发者添加)
↓
CallServerInterceptor(内置)

五、如何选择拦截器类型?

场景 推荐类型
统一添加请求头 应用拦截器
全局日志记录 应用拦截器
监控实际网络请求 网络拦截器
调试 TLS/连接信息 网络拦截器
修改重定向行为 重写 RetryAndFollowUpInterceptor

六、注意事项

  1. 性能影响:拦截器中的耗时操作会阻塞网络请求
  2. 修改请求体:需谨慎处理,避免破坏数据一致性
  3. 错误处理:拦截器中的异常会直接中断请求链
  4. 线程安全:拦截器可能被多个线程同时调用

OkHttp 的拦截器机制是其强大灵活性的核心,合理使用可以高效实现统一认证、日志、缓存等全局功能。

相关推荐
fLDiSQV1W7 小时前
springMVC-HTTP消息转换器与文件上传、下载、异常处理
网络协议·http·okhttp
Ttang234 天前
Java爬虫:Jsoup+OkHttp实战指南
java·爬虫·okhttp
李庆政3704 天前
OkHttp的基本使用 实现GET/POST请求 authenticator自动认证 Cookie管理 请求头设置
java·网络协议·http·okhttp·ssl
无名-CODING5 天前
Java 爬虫进阶:动态网页、多线程与 WebMagic 框架实战
java·爬虫·okhttp
小李云雾6 天前
零基础-从ESS6基础到前后端联通实战
前端·python·okhttp·中间件·eclipse·html·fastapi
亿牛云爬虫专家6 天前
爬虫踩坑实录:OkHttp 接入爬虫代理报 Too many tunnel connections attempted 深度解析
爬虫·okhttp·https·爬虫代理·connect·隧道代理·ip 切换
Dxy123931021612 天前
Ajax如何发送列表数据
前端·ajax·okhttp
967713 天前
AJAX和Axios理解和关系
前端·ajax·okhttp
必胜刻15 天前
AJAX 请求理解
前端·ajax·okhttp·前后端交互