当前位置: 代码迷 >> 综合 >> OkHttp如何移除User-Agent,Accept-Encoding等框架自动添加的请求头参数
  详细解决方案

OkHttp如何移除User-Agent,Accept-Encoding等框架自动添加的请求头参数

热度:38   发布时间:2023-12-09 06:32:22.0

使用OkHttp网络框架在进行网络请求时会发现,传到后台的请求头中会比我们自己添加的参数多出几个额外参数。查看源码会发现

 private Response getResponseWithInterceptorChain() throws IOException {// Build a full stack of interceptors.List<Interceptor> interceptors = new ArrayList<>();interceptors.addAll(client.interceptors());interceptors.add(retryAndFollowUpInterceptor);interceptors.add(new BridgeInterceptor(client.cookieJar()));interceptors.add(new CacheInterceptor(client.internalCache()));interceptors.add(new ConnectInterceptor(client));if (!retryAndFollowUpInterceptor.isForWebSocket()) {interceptors.addAll(client.networkInterceptors());}interceptors.add(new CallServerInterceptor(retryAndFollowUpInterceptor.isForWebSocket()));Interceptor.Chain chain = new RealInterceptorChain(interceptors, null, null, null, 0, originalRequest);return chain.proceed(originalRequest);}

OkHttp会默认添加一个桥接拦

  相关解决方案