HarmonyOS 鸿蒙Next网络请求使用gzip压缩数据
HarmonyOS 鸿蒙Next网络请求使用gzip压缩数据
咨询描述:
在使用原生网络库进行网络请求需要对发送数据进行gzip压缩,如何实现?
关于HarmonyOS 鸿蒙Next网络请求使用gzip压缩数据的问题,您也可以访问:https://www.itying.com/category-93-b0.html 联系官网客服。
更多关于HarmonyOS 鸿蒙Next网络请求使用gzip压缩数据的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
解决方案:
网络请求可支持使用gzip传输数据
如使用axios可参考如下:
参数定义时无法使用’-'符号传递,可使用Record泛式设置参数发起请求,可参考如下: let import axios, { AxiosError, AxiosRequestConfig, AxiosResponse } from ‘[@ohos](/user/ohos)/axios’ const config: AxiosRequestConfig = { url: ‘https://test.com’, //域名地址 method: ‘POST’, params: { accessToken: ‘ZrsMkU01wR1nonbyMvNYecTa3HFNXseUT2hZ/2eL4fCaiiGxf1OXkGyU5RpcuBriqRX7pbsW8ImrVU4mVAXQ8/3WjJ1/9FfcoESizAk8cl0=’, //压缩数据 appName: ‘ygw’, // body: {“latitude”:"",“longitude”:"",“addressCode”:""} }, headers: { ‘Content-Type’: ‘text/plain’, Accept: ‘/’, ‘Accept-Encoding’: ‘gzip, deflate, br’, Connection: ‘keep-alive’ }, data: ‘{“latitude”:"",“longitude”:"",“addressCode”:""}’ }
const Request = axios(config) .then((response: AxiosResponse<string>) => { console.info(hutegfei-----------res1-----------${JSON.stringify(response)}); }) .catch((error: AxiosError) => { console.log(hutegfei-----------err1-----------${JSON.stringify(error)}) }); 发送gzip类型数据时需要确认使用服务器支持 GZIP类型,方法内设置指定为gzip类型并在请求体中携带上压缩数据,请求头部分设置可参考如下: headers : { “Content-Encoding”: “gzip”, “Content-Type”: “application/json” }
<button style="position: absolute; padding: 4px 8px 0px; cursor: pointer; top: 8px; right: 8px; font-size: 14px;">复制</button>
更多关于HarmonyOS 鸿蒙Next网络请求使用gzip压缩数据的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
找HarmonyOS工作还需要会Flutter的哦,有需要Flutter教程的可以学学大地老师的教程,很不错,B站免费学的哦:https://www.bilibili.com/video/BV1S4411E7LY/?p=17