微信头像偶现请求失败异常排查

1、发现问题

在使用cocos开发游戏时使用到了微信授权获取到的头像链接(https://thirdwx.qlogo.cn/mmopen/vi_32/kn7KcyKbgXW6A15g0OGQbdOZfTHqIszWr0CZD9wCiaib4nzUseOjmtLO71j8qibfvFlLywDvwPya84ZT6vbKz3Bbw/132)
在请求头像资源的时候出现了报跨域的问题,而且响应报文的长度是0

2、问题猜测

1、出现跨域,尝试用nginx代理解决(最后证实是错误的方向)

3、问题复现

使用node.js脚本触发请求头像链接,将响应日志写入文件中,进行分析

var https = require('https');
var querystring = require('querystring');
var fs = require("fs");

const path = "/mmopen/vi_32/kn7KcyKbgXW6A15g0OGQbdOZfTHqIszWr0CZD9wCiaib4nzUseOjmtLO71j8qibfvFlLywDvwPya84ZT6vbKz3Bbw/132";
var options1 = {
     
    host: 'thirdwx.qlogo.cn', // 这个不用说了, 请求地址
    port: 443,
    path: path, // 具体路径, 必须以'/'开头, 是相对于host而言的
    method: 'GET', // 请求方式, 这里以post为例
    headers: {
      // 必选信息, 如果不知道哪些信息是必须的, 建议用抓包工具看一下, 都写上也无妨...
        "authority": "thirdwx.qlogo.cn",
        "method": "GET",
        "path": "/mmopen/vi_32/Q0j4TwGTfTIhseUSvPCfkkYGEwwCER1OI6yqX9w4s3osWmRnWHrMFtAsOWk839yEdicuWyuJ68ic5wnv595Ay09w/132?1606987705325",
        "scheme": "https",
        "accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
        "accept-encoding": "gzip, deflate, br",
        "accept-language": "zh-CN,zh;q=0.9",
        "cache-control": "no-cache",
        "pragma":"no-cache",
        "sec-fetch-dest": "document",
        "sec-fetch-mode": "navigate",
        "sec-fetch-site": "none",
        "sec-fetch-user": "?1",
        "upgrade-insecure-requests": 1,
        "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36"
    }
};

var options2 = {
     
    host: 'thirdwx.qlogo.cn', // 这个不用说了, 请求地址
    port: 443,
    path: path, // 具体路径, 必须以'/'开头, 是相对于host而言的
    method: 'GET', // 请求方式, 这里以post为例
    headers: {
      // 必选信息, 如果不知道哪些信息是必须的, 建议用抓包工具看一下, 都写上也无妨...
        "authority": "thirdwx.qlogo.cn",
        "method": "GET",
        "path": "/mmopen/vi_32/Q0j4TwGTfTIhseUSvPCfkkYGEwwCER1OI6yqX9w4s3osWmRnWHrMFtAsOWk839yEdicuWyuJ68ic5wnv595Ay09w/132?1606987705325",
        "scheme": "https",
        "accept": "image/avif,image/webp,image/apng,image/*,*/*;q=0.8",
        "accept-encoding": "gzip, deflate, br",
        "accept-language": "zh-CN,zh;q=0.9",
        "origin": "http://172.27.32.1:7456",
        "referer": "http://172.27.32.1:7456/web-mobile/web-mobile/index.html",
        "sec-fetch-dest": "image",
        "sec-fetch-mode": "cors",
        "sec-fetch-site": "cross-site",
        "user-agent": "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.183 Mobile Safari/537.36",
    }
};

// 写入文件
function writeFile(filename, text) {
     
    fs.writeFile(filename, text, {
      'flag': 'a' }, function(err) {
     
        if (err) {
     
            throw err;
        }
    });
}

setInterval(() => {
     
    // https.get(options1, function (res) {
     
    //     if(res.statusCode != 200) {
     
    //         console.log("options1:", res.statusCode);
    //         console.log("options1:", res.headers);
    //     }
    //     var txt = `res statusCode:${res.statusCode} resHeaders:${JSON.stringify(res.headers)}\n`
    //     writeFile('./1.txt',txt)
    //     var resData = "";
    //     res.on("data", function (data) {
     
    //         resData += data;
    //     });
    //     res.on("end", function () {
     
    //         // console.log(resData);
    //     });
    // })
    https.get(options2, function (res) {
     
        if(res.statusCode != 200) {
     
            console.log("options2:", res.statusCode);
            console.log("options2:", res.headers);
        }
        var txt = `res statusCode:${
       res.statusCode} resHeaders:${
       JSON.stringify(res.headers)}\n`
        writeFile('./2.txt',txt)
        var resData = "";
        res.on("data", function (data) {
     
            resData += data;
        });
        res.on("end", function () {
     
            // console.log(resData);
        });
    })
}, 10)

日志片段

res statusCode:200 resHeaders:{
     "server":"NWSs","date":"Thu, 03 Dec 2020 09:33:47 GMT","content-type":"image/jpeg","content-length":"2769","connection":"close","vary":"Accept,Origin","last-modified":"Tue, 12 Jun 2018 14:13:33 GMT","cache-control":"max-age=2592000","x-delay":"16 us","x-info":"real data","x-bcheck":"0_1","x-cpt":"filename=0","user-returncode":"0","x-datasrc":"2","x-reqgue":"0","size":"2769","chid":"0","fid":"0","access-control-allow-methods":"GET,POST,OPTIONS","timing-allow-origin":"*","access-control-allow-origin":"*","access-control-max-age":"2592000","x-nws-log-uuid":"22a89831-dd09-4ec9-9419-c7e41bde0647"}
res statusCode:200 resHeaders:{
     "server":"NWSs","date":"Thu, 03 Dec 2020 09:33:47 GMT","content-type":"image/jpeg","content-length":"2761","connection":"close","vary":"Accept,Origin","last-modified":"Tue, 12 Jun 2018 14:13:33 GMT","cache-control":"max-age=2592000","x-delay":"12 us","x-info":"real data","x-bcheck":"0_1","x-cpt":"filename=0","user-returncode":"0","x-datasrc":"2","x-reqgue":"0","size":"2761","chid":"0","fid":"0","access-control-allow-methods":"GET,POST,OPTIONS","timing-allow-origin":"*","access-control-allow-origin":"*","access-control-max-age":"2592000","x-nws-log-uuid":"fc4a4b4f-569b-43cf-8eec-b1080a2b8332"}
res statusCode:200 resHeaders:{
     "server":"NWSs","date":"Thu, 03 Dec 2020 09:33:47 GMT","content-type":"image/jpeg","content-length":"2766","connection":"close","vary":"Accept,Origin","last-modified":"Tue, 12 Jun 2018 14:13:33 GMT","cache-control":"max-age=2592000","x-delay":"23 us","x-info":"real data","x-bcheck":"0_1","x-cpt":"filename=0","user-returncode":"0","x-datasrc":"2","x-reqgue":"0","size":"2766","chid":"0","fid":"0","access-control-allow-methods":"GET,POST,OPTIONS","timing-allow-origin":"*","access-control-allow-origin":"*","access-control-max-age":"2592000","x-nws-log-uuid":"1a706fe7-9756-4dba-abc7-7e63ac1d5fc9"}
res statusCode:564 resHeaders:{
     "server":"NWSs","date":"Thu, 03 Dec 2020 09:33:47 GMT","content-length":"0","connection":"close","x-nws-log-uuid":"02f924c7-4b45-42ed-99d5-14bfc511cd68"}
res statusCode:564 resHeaders:{
     "server":"NWSs","date":"Thu, 03 Dec 2020 09:33:47 GMT","content-length":"0","connection":"close","x-nws-log-uuid":"fdca96f0-7f36-4965-9c16-bc31ed06c9fe"}
res statusCode:564 resHeaders:{
     "server":"NWSs","date":"Thu, 03 Dec 2020 09:33:47 GMT","content-length":"0","connection":"close","x-nws-log-uuid":"ac990a75-cb99-4bc0-a756-938ac472d095"}
res statusCode:564 resHeaders:{
     "server":"NWSs","date":"Thu, 03 Dec 2020 09:33:47 GMT","content-length":"0","connection":"close","x-nws-log-uuid":"b83aba8e-3b65-459a-b522-3806f795ac01"}
res statusCode:200 resHeaders:{
     "server":"NWSs","date":"Thu, 03 Dec 2020 09:33:47 GMT","content-type":"image/jpeg","content-length":"2767","connection":"close","vary":"Accept,Origin","last-modified":"Tue, 12 Jun 2018 14:13:33 GMT","cache-control":"max-age=2592000","x-delay":"13 us","x-info":"real data","x-bcheck":"0_1","x-cpt":"filename=0","user-returncode":"0","x-datasrc":"2","x-reqgue":"0","size":"2767","chid":"0","fid":"0","access-control-allow-methods":"GET,POST,OPTIONS","timing-allow-origin":"*","access-control-allow-origin":"*","access-control-max-age":"2592000","x-nws-log-uuid":"8fe13cae-29ee-45f9-8df4-afbd8dc95b1a"}
res statusCode:564 resHeaders:{
     "server":"NWSs","date":"Thu, 03 Dec 2020 09:33:47 GMT","content-length":"0","connection":"close","x-nws-log-uuid":"d9b798b3-032f-4495-afdc-e57a70ea5929"}
res statusCode:564 resHeaders:{
     "server":"NWSs","date":"Thu, 03 Dec 2020 09:33:47 GMT","content-length":"0","connection":"close","x-nws-log-uuid":"782ef70a-bbe3-4acf-989e-cb01eda8ffde"}
res statusCode:564 resHeaders:{
     "server":"NWSs","date":"Thu, 03 Dec 2020 09:33:47 GMT","content-length":"0","connection":"close","x-nws-log-uuid":"fbdac928-728c-461c-9d53-1b01fdf5d269"}
res statusCode:564 resHeaders:{
     "server":"NWSs","date":"Thu, 03 Dec 2020 09:33:47 GMT","content-length":"0","connection":"close","x-nws-log-uuid":"98d7adf8-aa3d-4fa3-a5e2-9f56ff521ff9"}
res statusCode:564 resHeaders:{
     "server":"NWSs","date":"Thu, 03 Dec 2020 09:33:47 GMT","content-length":"0","connection":"close","x-nws-log-uuid":"781fb981-b00b-4ca8-a730-a23ac2c07c5f"}
res statusCode:564 resHeaders:{
     "server":"NWSs","date":"Thu, 03 Dec 2020 09:33:47 GMT","content-length":"0","connection":"close","x-nws-log-uuid":"4db4c3c2-dd52-43bb-af9c-863e22f7a0da"}
res statusCode:564 resHeaders:{
     "server":"NWSs","date":"Thu, 03 Dec 2020 09:33:47 GMT","content-length":"0","connection":"close","x-nws-log-uuid":"ab3c42f4-841f-4213-a3d0-5d87a00d6e61"}
res statusCode:564 resHeaders:{
     "server":"NWSs","date":"Thu, 03 Dec 2020 09:33:47 GMT","content-length":"0","connection":"close","x-nws-log-uuid":"34677ecf-a470-4741-af04-123ae7a3c9da"}
res statusCode:564 resHeaders:{
     "server":"NWSs","date":"Thu, 03 Dec 2020 09:33:47 GMT","content-length":"0","connection":"close","x-nws-log-uuid":"472b7e79-71d7-4354-a126-3369a9905471"}
res statusCode:564 resHeaders:{
     "server":"NWSs","date":"Thu, 03 Dec 2020 09:33:47 GMT","content-length":"0","connection":"close","x-nws-log-uuid":"acd8c586-6b55-4b17-b998-fe81722ca8c3"}
res statusCode:564 resHeaders:{
     "server":"NWSs","date":"Thu, 03 Dec 2020 09:33:47 GMT","content-length":"0","connection":"close","x-nws-log-uuid":"3782ab3d-0b6e-49e9-8fcf-ac6317aab469"}
res statusCode:564 resHeaders:{
     "server":"NWSs","date":"Thu, 03 Dec 2020 09:33:47 GMT","content-length":"0","connection":"close","x-nws-log-uuid":"f554a199-fd99-49c6-80a7-3e3b4072e523"}
res statusCode:564 resHeaders:{
     "server":"NWSs","date":"Thu, 03 Dec 2020 09:33:47 GMT","content-length":"0","connection":"close","x-nws-log-uuid":"58b8ac77-b4d1-46a7-b36e-d22b8dc16a45"}
res statusCode:200 resHeaders:{
     "server":"NWSs","date":"Thu, 03 Dec 2020 09:33:47 GMT","content-type":"image/jpeg","content-length":"2769","connection":"close","vary":"Accept,Origin","last-modified":"Tue, 12 Jun 2018 14:13:33 GMT","cache-control":"max-age=2592000","x-delay":"17 us","x-info":"real data","x-bcheck":"0_1","x-cpt":"filename=0","user-returncode":"0","x-datasrc":"2","x-reqgue":"0","size":"2769","chid":"0","fid":"0","access-control-allow-methods":"GET,POST,OPTIONS","timing-allow-origin":"*","access-control-allow-origin":"*","access-control-max-age":"2592000","x-nws-log-uuid":"ca8161a4-ab00-4262-9e53-35e89771964e"}
res statusCode:564 resHeaders:{
     "server":"NWSs","date":"Thu, 03 Dec 2020 09:33:47 GMT","content-length":"0","connection":"close","x-nws-log-uuid":"8f4fe3b5-1ac0-4d6e-82ae-be00d07e510b"}
res statusCode:564 resHeaders:{
     "server":"NWSs","date":"Thu, 03 Dec 2020 09:33:47 GMT","content-length":"0","connection":"close","x-nws-log-uuid":"51dcf7ed-fb3a-46e7-8062-2210dbaee3c3"}
res statusCode:200 resHeaders:{
     "server":"NWSs","date":"Thu, 03 Dec 2020 09:33:47 GMT","content-type":"image/jpeg","content-length":"2769","connection":"close","vary":"Accept,Origin","last-modified":"Tue, 12 Jun 2018 14:13:33 GMT","cache-control":"max-age=2592000","x-delay":"12 us","x-info":"real data","x-bcheck":"0_1","x-cpt":"filename=0","user-returncode":"0","x-datasrc":"2","x-reqgue":"0","size":"2769","chid":"0","fid":"0","access-control-allow-methods":"GET,POST,OPTIONS","timing-allow-origin":"*","access-control-allow-origin":"*","access-control-max-age":"2592000","x-nws-log-uuid":"b1d54bff-8a05-419d-8dd2-49efe3471672"}
res statusCode:564 resHeaders:{
     "server":"NWSs","date":"Thu, 03 Dec 2020 09:33:47 GMT","content-length":"0","connection":"close","x-nws-log-uuid":"4fd1e7ec-738f-4fb2-a4e5-e1e098c0d863"}
res statusCode:200 resHeaders:{
     "server":"NWSs","date":"Thu, 03 Dec 2020 09:33:47 GMT","content-type":"image/jpeg","content-length":"2765","connection":"close","vary":"Accept,Origin","last-modified":"Tue, 12 Jun 2018 14:13:33 GMT","cache-control":"max-age=2592000","x-delay":"18 us","x-info":"real data","x-bcheck":"0_1","x-cpt":"filename=0","user-returncode":"0","x-datasrc":"2","x-reqgue":"0","size":"2765","chid":"0","fid":"0","access-control-allow-methods":"GET,POST,OPTIONS","timing-allow-origin":"*","access-control-allow-origin":"*","access-control-max-age":"2592000","x-nws-log-uuid":"1c58fa94-8542-4e64-b4ec-8f0bb17dc7c0"}

最终锁定是微信服务的问题,在头像资源长度是0的同时,允许跨域的响应头没有了,所以前端报跨域错误。

4、解决方案

前端捕捉到头像资源长度为0,进行重试请求。

你可能感兴趣的