分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > IT知识

Okhttp3请求网络开启Gzip压缩

发布时间:2023-09-06 01:55责任编辑:彭小芳关键词:http

前沿

首先OkHttp3是支持Gzip解压缩的,不过我们要明白,它是支持我们在发起请求的时候自动加入header,Accept-Encoding: gzip,而我们的服务器返回的时候header中有Content-Encoding: gzip
关于更多深入的内容呢,可以参考阅读下面这篇文章,讲的非常好!
聊聊HTTP gzip压缩与常见的Android网络框架

那么,我们在向服务器提交大量数据的时候,希望对post的数据进行gzip压缩,改怎么办?
下边给出方案!

方案

官方采用的是自定义拦截器的方式!
源码在:
okhttp/samples/guide/src/main/java/okhttp3/recipes/RequestBodyCompression.java
废话不多说,直接上代码:

 1 import java.io.IOException; 2 ?3 import okhttp3.Interceptor; 4 import okhttp3.MediaType; 5 import okhttp3.Request; 6 import okhttp3.RequestBody; 7 import okhttp3.Response; 8 import okio.BufferedSink; 9 import okio.GzipSink;10 import okio.Okio;11 12 public class GzipRequestInterceptor implements Interceptor {13 ????@Override14 ????public Response intercept(Chain chain) throws IOException {15 ????????Request originalRequest = chain.request();16 ????????if (originalRequest.body() == null || originalRequest.header("Content-Encoding") != null) {17 ????????????return chain.proceed(originalRequest);18 ????????}19 20 ????????Request compressedRequest = originalRequest.newBuilder()21 ????????????????.header("Content-Encoding", "gzip")22 ????????????????.method(originalRequest.method(), gzip(originalRequest.body()))23 ????????????????.build();24 ????????return chain.proceed(compressedRequest);25 ????}26 27 ????private RequestBody gzip(final RequestBody body) {28 ????????return new RequestBody() {29 ????????????@Override30 ????????????public MediaType contentType() {31 ????????????????return body.contentType();32 ????????????}33 34 ????????????@Override35 ????????????public long contentLength() {36 ????????????????return -1; // 无法提前知道压缩后的数据大小37 ????????????}38 39 ????????????@Override40 ????????????public void writeTo(BufferedSink sink) throws IOException {41 ????????????????BufferedSink gzipSink = Okio.buffer(new GzipSink(sink));42 ????????????????body.writeTo(gzipSink);43 ????????????????gzipSink.close();44 ????????????}45 ????????};46 ????}47 }

然后构建OkhttpClient的时候,添加拦截器:

OkHttpClient okHttpClient = new OkHttpClient.Builder() ????.addInterceptor(new GzipRequestInterceptor())//开启Gzip压缩 ???... ???.build();

后记

如果需要带有内容长度content-length的,可以查看这个issue:
Here’s the full gzip interceptor with content length, to whom it may concern:

参考:https://blog.csdn.net/tq08g2z/article/details/77311579

Okhttp3请求网络开启Gzip压缩

原文地址:https://www.cnblogs.com/ganchuanpu/p/9062788.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved