当前位置:网站首页>Flutter实战-请求封装(四)之gzip报文压缩
Flutter实战-请求封装(四)之gzip报文压缩
2022-08-04 17:01:00 【蓝面书生】
用了两年的flutter,有了一些心得,不虚头巴脑,只求实战有用,以供学习或使用flutter的小伙伴参考,学习尚浅,如有不正确的地方还望各路大神指正,以免误人子弟,在此拜谢~(原创不易,转发请标注来源和作者)
注意:无特殊说明,flutter版本为3.0+
上篇我们对flutter的dio请求做了http2的改造,这篇我们来探讨如何进行gzip报文压缩的改造。
一.何为gzip,为什么我们要使用gzip
gzip是一种文件压缩技术,Http上的gzip编码是一种通过对报文压缩来改进web应用程序性能的技术。使用了gzip后,可以节省带宽,加快访问速度。
如果我们要使用gzip,要求服务器端和客户端要同时支持gzip。
工作原理
(图片来源于网络)
二.服务器支持gzip
#gzip模块设置 gzip on; #开启gzip压缩输出 决定是否开启gzip模块,on表示开启,off表示关闭;gzip_min_length 1k; #最小压缩文件大小 gzip_buffers 4 16k; #压缩缓冲区 gzip_http_version 1.1; #压缩版本 gzip_comp_level 2; #压缩等级,等级越底压缩速度越快文件压缩比越1gzip_types text/plain application/x-javascript text/css application/xml; #压缩类型,默认就已经包含text/html,所以下面就不用再写了,写上去也不会有问题,但是会有一个warn。 gzip_vary on; #启用应答头"Vary: Accept-Encoding" #limit_zone crawler $binary_remote_addr 10m; #开启限制IP连接数的时候需要使用
三.客户端支持gzip
这里我只进行了返回参数的压缩
1.添加gzip标识
dio = Dio(BaseOptions(
responseDecoder: gzipDecoder,
headers: {"content-encoding": "gzip", "accept-encoding": "gzip"},
));
2.实现gzipDecoder
String gzipDecoder(List<int> responseBytes, RequestOptions options, ResponseBody responseBody) {
if (isGzipped(responseBytes)) {
return utf8.decode(gzip.decode(responseBytes));
} else {
return utf8.decode(responseBytes);
}
}bool isGzipped(List<int> byte) {
if (byte.length < 2) {
return false;
}
return ((byte[0].toUnsigned(64)) & 0xff | ((byte[1] << 8).toUnsigned(64) & 0xff00)) == 0x8b1f;
}
边栏推荐
- Mobile zte ZXV10 B860AV2. 1 - A_S905L2_MT7668_ wire brush the firmware package
- What does the product system of a digital financial enterprise look like?
- LeetCode Question of the Day - 1403. Minimum Subsequence in Non-Increasing Order
- SAP ABAP SteammPunk 蒸汽朋克的最新进展 - 嵌入式蒸汽朋克
- 智慧场馆的无人值守怎么做?
- JVM内存和垃圾回收-08.方法区
- GraphQL 入门与实践
- 餐饮供应链管理系统
- 数字化金融企业的产品体系长啥样?
- 通关剑指 Offer——剑指 Offer II 010. 和为 k 的子数组
猜你喜欢
随机推荐
SAP 电商云 Spartacus UI 页面布局的设计原理
机器学习(十一):KNN(K近邻)
全球电子产品需求萎靡:三星越南工厂大幅压缩产能,减少工人工作日
Hubei Mobile HG680-LV_S905L3B_wire brush firmware package
智慧场馆的功能有哪些
机器学习(十八):随机搜索和XGBoost
容器化 | 在 NFS 备份恢复 RadonDB MySQL 集群数据
Mobile BesTV_R3300-L_S905L_8189_wire brush firmware package
葫芦娃解析
为什么买域名必须实名认证?这样做什么原因?
华硕win11安全启动如何开启
Boost library study notes (1) Installation and configuration
理财产品买入后份额是固定不变的吗?
dotnet remoting 抛出异常
码蹄集 - MT3029 - 新月轩就餐
《分布式云最佳实践》分论坛,8月11日深圳见
What does the product system of a digital financial enterprise look like?
湖北电信天邑TY1608_S905L3B_MT7668_卡刷固件包
JVM内存和垃圾回收-08.方法区
CSDN21天学习挑战赛——程序流程控制(02)