项目开发中有个需求,将所有接口的值都通过RSA加密传输,服务端返回的所有内容也都需要通过RSA解密后再使用。
参考这篇文章可以实现功能:https://blog.youkuaiyun.com/yuzhiqiang_1993/article/details/90168515
但是在实现功能之后,替换原有response的body之后,接口从onError返回,没有正确的解析成实体类,返回错误信息就如标题中所写。
因为retrofit在数据返回的时候,可能会对数据包进行压缩传输,到客户端在解密:
//读取服务器返回的结果
ResponseBody responseBody = response.newBuilder().build().body();
BufferedSource source = responseBody.source();
source.request(Long.MAX_VALUE); // Buffer the entire body.
Buffer buffer = source.buffer();
//获取content的压缩类型
String encoding = response.headers().get("Content-Encoding");
Charset charset = Charset.forName("UTF-8");
MediaType contentType = responseBody.contentType();
if (contentType != null) {
charset = contentType.charset(charset);
}
Buffer clone = buffer.clone();
if (encoding != null && encoding.equalsIgnoreCase("gzip")) {//content 使用 gzip 压缩
return ZipHelper.decompre

本文探讨了在项目开发中如何使用RSA对接口数据进行加密传输,并处理服务器返回内容的解密问题。重点讲述了Content-Encoding、响应体格式调整、字符编码乱码及FormUrlEncoded注解对中文的影响,提供了相应的解决方法和注意事项。
最低0.47元/天 解锁文章
1280

被折叠的 条评论
为什么被折叠?



