【问题标题】:Junk characters in htttps get responseHttps 中的垃圾字符得到响应
【发布时间】:2021-04-17 00:01:58
【问题描述】:

我遇到了一个用 C++ 编写的 Windows 应用程序(OCX 控件)的奇怪情况,该应用程序在 2 天前运行良好。没有向应用程序添加更新。然后突然,它开始在与远程测试服务器的通信中出现错误。基本上,错误是在GET 请求的响应中,我看到这样的垃圾字符:ÑòWo †¶âBA etc

我用于 HTTP 请求的函数是:

TCHAR* openUrlFunction(TCHAR *strServer, TCHAR *strPath, TCHAR *userAgent){

    HINTERNET   httpSession = NULL;
    HINTERNET   httpConnect = NULL;
    HINTERNET   httpRequest = NULL;
    HINTERNET   hFile = NULL;
    BOOL        resDownload = FALSE;
    TCHAR       bufferIdent[60] = { 0 };
    TCHAR       bufferMirror[500] = { 0 };
    TCHAR       szURL[200] = { 0 };
    TCHAR       sError[7] = { 0 };
    DWORD       dwFlags;
    //  DWORD       dwTimeOut = 25000;
    DWORD       dwBuffLen = sizeof(dwFlags);
    int         i = 0;
    DWORD       dwRead, error;
    int         len;
    TCHAR       *res1;
    TCHAR       delimeter[6] = _T("\f\n\r\t\v");

    HRESULT resCat;

    
    bufferIdent[0] = '\0';
    bufferMirror[0] = '\0';
    szURL[0] = '\0';

    std::string ss;

    httpSession = InternetOpen(strAgent, INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0); // header 

    if (httpSession)
    {
        httpConnect = InternetConnect(httpSession, strServer, INTERNET_DEFAULT_HTTPS_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0); 

        if (httpConnect)
        {
            httpRequest = HttpOpenRequest(httpConnect, "GET", strPath, NULL, NULL, NULL, INTERNET_FLAG_SECURE, 0);  parametros.

            if (HttpSendRequest(httpRequest, NULL, 0, NULL, 0)) {  
                if (httpRequest)
                {
                    while (InternetReadFile(httpRequest, bufferMirror, 499, &dwRead)) // Se lee el retorno
                    {
                        if (dwRead == 0)
                            break;

                        StrTrim(bufferMirror, delimeter);

                        while (bufferMirror[i] != NULL){  

                            if (bufferMirror[i] != char(10))
                                ss = ss + bufferMirror[i];
                            i++;
                            if (bufferMirror[i] == char(10))  
                                break;
                        }
                        i = 0;
                    }
                }
            }
            else{

                error = GetLastError();
                _stprintf_s(sError, 7, _T("%d"), error);
                InternetCloseHandle(httpRequest);
                InternetCloseHandle(httpConnect);
                InternetCloseHandle(httpSession);
                return sError;
            }
            InternetCloseHandle(httpRequest);  
        }
        InternetCloseHandle(httpConnect);
    }
    InternetCloseHandle(httpSession);

 

    len = ss.length();

    if (len == 0){
        return NULL;
    }
    res1 = new TCHAR[len + 1];

    resCat = StringCchCopyN(res1, len + 1, ss.c_str(), len);

    if (!SUCCEEDED(resCat))
        return NULL;

    return res1;
}

这里有趣的是我决定监控流量,所以我在发生问题的同一台机器(Windows 10)上安装了 Fiddler。启动 Fiddler 后,我用应用程序进行了测试,瞧,响应清晰且格式正确,没有垃圾字符。

我怀疑安装的 Fiddler Everywhere 证书与结果有关。因此,如果我关闭 Fiddler 并进行新的测试,我会再次收到垃圾字符。

我检查了计算机中的 TLS 配置,没有任何问题。我不确定 Windows 更新是否会导致这种情况。

我们有 3 台机器,问题出现在 3 台机器中的 2 台上。它们都是 Windows 10,但可能更新不同。

这里还有一个重要的事情是服务器是用来测试的,客户端没有安装任何SSL证书。

【问题讨论】:

  • 您只是检查dwRead 是否为零,然后假设缓冲区中不是\0 的所有其他内容都是数据,但如果例如dwRead == 1 只有缓冲区中的第一个字节是实际读取的数据,其他 499 - dwRead 字节是垃圾。您需要正确使用dwRead 来确定每次调用InternetReadFile 时将多少字节读入缓冲区。

标签: c++ windows https fiddler


【解决方案1】:

好吧,在研究了这个问题之后,我发现问题的根源更多地与一个变量没有正确初始化并且在 http 请求之前,这个变量可能包含垃圾字符,所以接下来的步骤的进程因变量包含的垃圾而受到影响。另一件事是在安装正确的 SSL 证书后解决了与 WININET 的连接问题。即使设置了一些标志以绕过 SSL 验证,也没有工作。只有在服务器配置了 SSL 证书时才能进行连接。

【讨论】:

    猜你喜欢
    • 2019-09-25
    • 2020-08-30
    • 1970-01-01
    • 2015-01-28
    • 1970-01-01
    • 2023-03-10
    • 2016-09-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多