这边对接steam的一个接口出现返回数据乱码,封装的HttpURLConnection请求库是做了数据请求和返回格式指定的,都是 Utf-8,大概请求如下:
QyReqRequester.getInstance().execReqApiRequest("https://store.steampowered.com/api/addtowishlist", QyReqRequester.ReqNetType.PostByForm,
hashMapOf("ajax" to "YES", "appid" to "xxx", "sessionid" to "xxx"), hashMapOf(
"Accept" to "*/*",
"Accept-Encoding" to "gzip, deflate, br",
"Accept-Language" to "zh-CN,zh-Hans;q=0.9",
"Connection" to "keep-alive",
"Content-Type" to "application/x-www-form-urlencoded; charset=UTF-8",
"Cookie" to "timezoneOffset=28800,0; sessionid=xxx; steamCountry=xxxx; steamLoginSecure=xxxx; recentapps=null; browserid=xxx",
"Host" to "store.steampowered.com",
"Origin" to "https://store.steampowered.com",
"Referer" to "https://store.steampowered.com/account/licenses/",
"Sec-Fetch-Dest" to "empty",
"Sec-Fetch-Mode" to "cors",
"Sec-Fetch-Site" to "same-origin",
"User-Agent" to "V2338A/android/14/xxx/accelerator/3.6.9/xxxx"
), isSelfLoadingShow = true, curReqRequesterResultCallback = object : QyReqRequesterAllCallback {
override fun onSuccess(data: String?, reqFlagParam: Any?, resExtendData: Any?) {
showMsgInfo("=========QyReqRequester========>onSuccess:==>$data==>$reqFlagParam==>$resExtendData")
}
override fun onFailure(errMsg: String?, httpCode: Int, reqFlagParam: Any?): Boolean {
showMsgInfo("=========QyReqRequester========>onFailure:==>$httpCode====>$errMsg")
return true
}
})
返回的乱码大概如下:
=======QyReqRequester==Log==>=====doApi===>成功数据(请求Url:https://store.steampowered.com/api/addtowishlist):�V*.MNN-.V�JK�)N�Q*�,���,.q�/�+Q�2��??K� M#??????
排查完 utf-8 相关,无果,搜索了哈相关的,这个大哥的文章靠谱:https://www.cnblogs.com/zjiacun/p/7736486.html,原因就是请求的 header 字段"Accept-Encoding"传了指定 gzip 传输,所以返回的数据流也需要 做 gzip 解析才行,我这边的私有库代码就不贴了,其实核心就是GZIPInputStream,只不过注意需要判断请求头指定了 gzip 才去做 gzip 解析,否则你会收到
java.util.zip.ZipException: Not in GZIP format
异常,直接截图参考这个文章,避免他内容看不到了!