BridgeInterceptor

//从url里面获取cookie
      //这个cookieJar是Okhttp上面设置的那个cookieJar
List<Cookie> cookies = cookieJar.loadForRequest(userRequest.url());
if (!cookies.isEmpty()) {
  //将所有的cookie添加到cookie头
  requestBuilder.header("Cookie", cookieHeader(cookies));
}
public final class BridgeInterceptor implements Interceptor {
  /**
   * 用来处理Cookie的保存和读取
   */
  private final CookieJar cookieJar;

可以看到是在构造方法里面传入的
  public BridgeInterceptor(CookieJar cookieJar) {
    this.cookieJar = cookieJar;
  }

在这里插入图片描述
可以看到是在RealCall中调用了这个构造方法

在这里插入图片描述
点击里面的cookieHeader看看

/** Returns a 'Cookie' HTTP request header with all cookies, like {@code a=b; c=d}. */
  //用来将所有的Coookie转换成,key1=value1; key2=value2,例如:a=b; c=d
  private String cookieHeader(List<Cookie> cookies) {
    StringBuilder cookieHeader = new StringBuilder();
    //遍历cookies列表
      //相当于这个种形式 name=value;key=value
    for (int i = 0, size = cookies.size(); i < size; i++) {
      if (i > 0) {
        cookieHeader.append("; ");
      }
      Cookie cookie = cookies.get(i);
      cookieHeader.append(cookie.name()).append('=').append(cookie.value());
    }
    return cookieHeader.toString();
  }
    if (userRequest.header("User-Agent") == null) {
      requestBuilder.header("User-Agent", Version.userAgent());
    }

    //这里调用下一个拦截器(最后一个拦截器返回回来的网络数据了)
      //这段代码上面是处理器,下面是处理响应Response
    Response networkResponse = chain.proceed(requestBuilder.build());

//从网络的相应中获取Cookie,这个方法里面或保存
HttpHeaders.receiveHeaders(cookieJar, userRequest.url(), networkResponse.headers());

-->点击跳转:
	public static void receiveHeaders(CookieJar cookieJar, HttpUrl url, Headers headers) {
	  //如果没有设置cookie的代理,就直接返回
	  if (cookieJar == CookieJar.NO_COOKIES) return;
	
	  //从header中解析出cookie
	  List<Cookie> cookies = Cookie.parseAll(url, headers);
	  if (cookies.isEmpty()) return;
	
	  //然后调用保存方法
	  cookieJar.saveFromResponse(url, cookies);
	}
-->点击跳转:查看Cookie.parseAll(url, headers)
	
	  public static List<Cookie> parseAll(HttpUrl url, Headers headers) {
	    List<String> cookieStrings = headers.values("Set-Cookie");
	    List<Cookie> cookies = null;
	
	    for (int i = 0, size = cookieStrings.size(); i < size; i++) {
	      Cookie cookie = Cookie.parse(url, cookieStrings.get(i));
	      if (cookie == null) continue;
	      if (cookies == null) cookies = new ArrayList<>();
	      cookies.add(cookie);
	    }
	
	    return cookies != null
	        ? Collections.unmodifiableList(cookies)
	        : Collections.<Cookie>emptyList();
	  }

============================================
    //根据网络的响应创建一个Builder,这和前面从请求体创建一个Builder类似
    Response.Builder responseBuilder = networkResponse.newBuilder()
        .request(userRequest);

    if (transparentGzip
        && "gzip".equalsIgnoreCase(networkResponse.header("Content-Encoding"))
        && HttpHeaders.hasBody(networkResponse)) {
      //在这里处理响应体是gzip类型的工作
      //networkResponse.body().source():可以理解为就是输入流
      //这里其实是一个包装设计模式
      GzipSource responseBody = new GzipSource(networkResponse.body().source());
      Headers strippedHeaders = networkResponse.headers().newBuilder()
          .removeAll("Content-Encoding")
          .removeAll("Content-Length")
          .build();
      responseBuilder.headers(strippedHeaders);
      //这里用了一个包装类RealResponseBody,用来转换gzip响应体,这个后面再分析
      //Okio.buffer可以理解为包装后的输入流
      responseBuilder.body(new RealResponseBody(strippedHeaders, Okio.buffer(responseBody)));
    }

    //然后根据响应体Builder创建一个Response
    return responseBuilder.build();

-->点击跳转:RealResponseBody

	public final class RealResponseBody extends ResponseBody {
	  private final Headers headers;
	  private final BufferedSource source;
	
	  public RealResponseBody(Headers headers, BufferedSource source) {
	    this.headers = headers;
	    this.source = source;
	  }
	
	  @Override public MediaType contentType() {
	    String contentType = headers.get("Content-Type");
	    return contentType != null ? MediaType.parse(contentType) : null;
	  }
	
	  @Override public long contentLength() {
	    return HttpHeaders.contentLength(headers);
	  }
	
	  @Override public BufferedSource source() {
	    return source;
	  }
	}
responseBody-->new GzipSource(networkResponse.body().source());
也就是我们本地在读取这个Gzip响应的时候会调用这个GzipSource的read方法
在这个方法里面吧真实的流给拆出来了(原来的流是GZip的流,这里把它拆成真实的流了)
	 public long read(Buffer sink, long byteCount) throws IOException {
        if (byteCount < 0L) {
            throw new IllegalArgumentException("byteCount < 0: " + byteCount);
        } else if (byteCount == 0L) {
            return 0L;
        } else {
            if (this.section == 0) {
                this.consumeHeader();
                this.section = 1;
            }

            if (this.section == 1) {
                long offset = sink.size;
                long result = this.inflaterSource.read(sink, byteCount);
                if (result != -1L) {
                    this.updateCrc(sink, offset, result);
                    return result;
                }

                this.section = 2;
            }

            if (this.section == 2) {
                this.consumeTrailer();
                this.section = 3;
                if (!this.source.exhausted()) {
                    throw new IOException("gzip finished without exhausting source");
                }
            }

            return -1L;
        }
    }




E/AndroidRuntime: FATAL EXCEPTION: Thread-18 Process: com.example.read, PID: 22568 java.lang.RuntimeException: java.net.UnknownServiceException: CLEARTEXT communication to 192.168.210.113 not permitted by network security policy at com.example.read.upload_serverActivity$1.run(upload_serverActivity.java:111) at java.lang.Thread.run(Thread.java:920) Caused by: java.net.UnknownServiceException: CLEARTEXT communication to 192.168.210.113 not permitted by network security policy at okhttp3.internal.connection.RealRoutePlanner.planConnectToRoute$okhttp(RealRoutePlanner.kt:195) at okhttp3.internal.connection.RealRoutePlanner.planConnect(RealRoutePlanner.kt:152) at okhttp3.internal.connection.RealRoutePlanner.plan(RealRoutePlanner.kt:67) at okhttp3.internal.connection.FastFallbackExchangeFinder.launchTcpConnect(FastFallbackExchangeFinder.kt:118) at okhttp3.internal.connection.FastFallbackExchangeFinder.find(FastFallbackExchangeFinder.kt:62) at okhttp3.internal.connection.RealCall.initExchange$okhttp(RealCall.kt:267) at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.kt:32) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109) at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.kt:95) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109) at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.kt:84) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109) at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.kt:65) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109) at okhttp3.internal.connection.RealCall.getResponseWithInterceptorChain$okhttp(RealCall.kt:205) at okhttp3.internal.connection.RealCall.execute(RealCall.kt:158) at com.example.read.upload_serverActivity$1.run(upload_serverActivity.java:106) at java.lang.Thread.run(Thread.java:920) 怎么解决
05-29
E/AndroidRuntime: FATAL EXCEPTION: main Process: com.bawei.xuhe, PID: 22989 java.net.UnknownServiceException: CLEARTEXT communication to 10.59.9.18 not permitted by network security policy at okhttp3.internal.connection.RealConnection.connect(RealConnection.kt:188) at okhttp3.internal.connection.ExchangeFinder.findConnection(ExchangeFinder.kt:226) at okhttp3.internal.connection.ExchangeFinder.findHealthyConnection(ExchangeFinder.kt:106) at okhttp3.internal.connection.ExchangeFinder.find(ExchangeFinder.kt:74) at okhttp3.internal.connection.RealCall.initExchange$okhttp(RealCall.kt:255) at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.kt:32) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109) at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.kt:95) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109) at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.kt:83) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109) at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.kt:76) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109) at okhttp3.logging.HttpLoggingInterceptor.intercept(HttpLoggingInterceptor.kt:221) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109) at okhttp3.internal.connection.RealCall.getResponseWithInterceptorChain$okhttp(RealCall.kt:201) at okhttp3.internal.connection.RealCall.execute(RealCall.kt:154) at retrofit2.OkHttpCall.execute(OkHttpCall.java:204) at retrofit2.adapter.rxjava2.CallExecuteObservable.subscribeActual(CallExecuteObservable.java:41) at io.reactivex.Observable.subscribe(Observable.java:10179) at retrofit2.adapter.rxjava2.BodyObservable.subscribeActual(BodyObservable.java:34) at io.reactivex.Observable.subscribe(Observable.java:10179) at io.reactivex.internal.operators.observable.ObservableSubscribeOn$1.run(ObservableSubscribeOn.java:39) at io.reactivex.Scheduler$1.run(Scheduler.java:134) at io.reactivex.internal.schedulers.ScheduledRunnable.run(ScheduledRunnable.java:59) at io.reactivex.internal.schedulers.ScheduledRunnable.call(ScheduledRunnable.java:51) at java.util.concurrent.FutureTask.run(FutureTask.java:266) at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:301) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641) at java.lang.Thread.run(Thread.java:764) I/Process: Sending signal. PID: 22989 SIG: 9
06-10
请检查 以下错误W/System.err: java.io.FileNotFoundException: /storage/emulated/0/Pictures/Screenshots/Screenshot_20230622_152002.jpg: open failed: EACCES (Permission denied) W/System.err: at libcore.io.IoBridge.open(IoBridge.java:575) W/System.err: at java.io.FileInputStream.<init>(FileInputStream.java:160) W/System.err: at okio.Okio__JvmOkioKt.source(JvmOkio.kt:178) W/System.err: at okio.Okio.source(Unknown Source:1) W/System.err: at okhttp3.RequestBody$Companion$asRequestBody$1.writeTo(RequestBody.kt:167) W/System.err: at okhttp3.MultipartBody.writeOrCountBytes(MultipartBody.kt:157) W/System.err: at okhttp3.MultipartBody.writeTo(MultipartBody.kt:93) W/System.err: at okhttp3.internal.http.CallServerInterceptor.intercept(CallServerInterceptor.kt:59) W/System.err: at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109) W/System.err: at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.kt:34) W/System.err: at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109) W/System.err: at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.kt:95) W/System.err: at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109) W/System.err: at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.kt:83) W/System.err: at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109) W/System.err: at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.kt:76) W/System.err: at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109) W/System.err: at okhttp3.internal.connection.RealCall.getResponseWithInterceptorChain$okhttp(RealCall.kt:201) W/System.err: at okhttp3.internal.connection.RealCall$AsyncCall.run(RealCall.kt:517) W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167) W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641) W/System.err: at java.lang.Thread.run(Thread.java:930) W/System.err: Caused by: android.system.ErrnoException: open failed: EACCES (Permission denied) W/System.err: at libcore.io.Linux.open(Native Method) W/System.err: at libcore.io.ForwardingOs.open(ForwardingOs.java:567) W/System.err: at libcore.io.BlockGuardOs.open(BlockGuardOs.java:273) W/System.err: at libcore.io.ForwardingOs.open(ForwardingOs.java:567) W/System.err: at android.app.ActivityThread$AndroidOs.open(ActivityThread.java:8807) W/System.err: at libcore.io.IoBridge.open(IoBridge.java:561) W/System.err: ... 21 more
最新发布
07-10
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值