当前位置:网站首页>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;
}
边栏推荐
- 化学制品制造业数智化供应链管理系统:打造智慧供应体系,赋能企业产效提升
- WEB 渗透之XXE&XML
- Compose 类型稳定性注解:@Stable & @Immutable
- 】 【 LeetCode daily one problem - 540. The order of a single element of the array
- JSP的Web监听器(Listener)
- AtCoder Beginner Contest 262 部分题解
- Catering Supply Chain Management System
- 多线程学习笔记-3.并发容器
- 容器化 | 在 NFS 备份恢复 RadonDB MySQL 集群数据
- 海报 | 夏季高温,危化品安全风险的注意事项必须get!
猜你喜欢
随机推荐
【Gazebo入门教程】第二讲 模型库导入与可视化机器人建模(模型编辑器)
两个对象相同数据赋值
理财产品买入后份额是固定不变的吗?
Mobile magic box CM211-1_YS foundry _S905L3B_RTL8822C_wire brush firmware package
餐饮供应链管理系统
LeetCode Question of the Day - 1403. Minimum Subsequence in Non-Increasing Order
dotnet remoting 抛出异常
15 days to upgrade to fight monsters and become a virtual fashion creator
学习探索-网站中引入百度统计
码蹄集 - MT2142 - 万民堂大厨
Analysis of the gourd baby
Mobile magic box CM201-1_CW_S905L2_MT7668_wire brush firmware package
并发编程原理学习-reentrantlock源码分析
】 【 LeetCode daily one problem - 540. The order of a single element of the array
码蹄集 - MT2165 - 小码哥的抽卡之旅1
How to convert an int attribute into a string in the json format returned by the Go language gin framework?
Hubei Mobile ZTE B860AV2.1_S905L_ flash firmware package
浅谈运用低代码技术如何实现物流企业的降本增效
设置表头颜色
移动魔百盒CM211-1_YS代工_S905L3B_RTL8822C_线刷固件包









