WinHttp空返回(可能是因为抓取数据的速度太快)

时间:2019-06-06 15:58:54

标签: winapi visual-c++

虽然使用WinHttp请求,但是成功进行了调用,但是取决于调用是否花费时间在站点中填充,因此该值将不返回任何内容并且不报告任何错误。我们正在使用cfc文件作为api的返回响应。有些呼叫会起作用,而有些则不会(通常需要2秒钟以上的时间才能返回呼叫并填充数据)。

可悲的是,该api是私有的,我无法提供调用(专有信息)。但是我愿意接受所有建议,并将提供调用数据的代码(其中不包含任何专有数据)。

string response;

DWORD dwSize = 0;
DWORD dwDownloaded = 0;
LPSTR pszOutBuffer;
BOOL  bResults = FALSE;
HINTERNET  hSession = NULL,
    hConnect = NULL,
    hRequest = NULL;

// Use WinHttpOpen to obtain a session handle.
hSession = WinHttpOpen(L"WinHTTP Example/1.0",
    WINHTTP_ACCESS_TYPE_DEFAULT_PROXY,
    WINHTTP_NO_PROXY_NAME,
    WINHTTP_NO_PROXY_BYPASS, 0);

// Specify an HTTP server.
if (hSession) {
    hConnect = WinHttpConnect(hSession, s2ws(host).c_str(),
        INTERNET_DEFAULT_HTTPS_PORT, 0);
    if (!WinHttpSetTimeouts(hSession, 600000, 600000, 600000, 600000))
        printf("Error %u in WinHttpSetTimeouts.\n", GetLastError());
}

// Create an HTTP request handle.
if (hConnect)
    hRequest = WinHttpOpenRequest(hConnect, L"GET", s2ws(call).c_str(),
        NULL, WINHTTP_NO_REFERER,
        WINHTTP_DEFAULT_ACCEPT_TYPES,
        WINHTTP_FLAG_REFRESH | WINHTTP_FLAG_SECURE);

// Send a request.
if (hRequest) {
    bResults = WinHttpSendRequest(hRequest,
        WINHTTP_NO_ADDITIONAL_HEADERS, 0,
        WINHTTP_NO_REQUEST_DATA, 0,
        0, 0);
}

// End the request.
if (bResults) {
    bResults = WinHttpReceiveResponse(hRequest, 0);
}

// Keep checking for data until there is nothing left.
if (bResults) {
    do {
        // Check for available data.
        dwSize = 0;
        if (!WinHttpQueryDataAvailable(hRequest, &dwSize)) {
            printf("Error %u in WinHttpQueryDataAvailable.\n",
                GetLastError());

            break;
        }

        if (!dwSize)
            break;

        // Allocate space for the buffer.
        pszOutBuffer = new char[dwSize + 1];
        if (!pszOutBuffer) {
            printf("Out of memory\n");
            break;
        }
        // Read the data.
        ZeroMemory(pszOutBuffer, dwSize + 1);

        if (!WinHttpReadData(hRequest, (LPVOID)pszOutBuffer,
            dwSize, &dwDownloaded))
            printf("Error %u in WinHttpReadData.\n", GetLastError());
        else {
            response = response + string(pszOutBuffer);
        }

        // Free the memory allocated to the buffer.
        delete[] pszOutBuffer;

        // This condition should never be reached since WinHttpQueryDataAvailable
        // reported that there are bits to read.
        if (!dwDownloaded)
            break;
    } while (dwSize > 0);
}

if (response == "") {
    wcout << "Value is blank: " << bResults << endl << GetLastError() << endl << response.c_str() << endl;
}

// Report any errors.
if (!bResults)
    printf("Error %d has occurred.\n", GetLastError());

// Close any open handles.
if (hRequest) WinHttpCloseHandle(hRequest);
if (hConnect) WinHttpCloseHandle(hConnect);
if (hSession) WinHttpCloseHandle(hSession);

return response;

编辑:同样使用curl lib也会产生不返回数据的相同结果(它将返回其他api上的数据)

编辑2:我还尝试了本示例WinHttpSendRequest fails with ERROR_WINHTTP_SECURE_FAILURE中的解决方案 它也无法捕获数据,因此我不确定为什么无法捕获数据。

编辑3:Fiddler报告 HTTP / 1.1 200 OK 缓存控制:私有 内容长度:0 内容类型:文本/纯文本;字符集= UTF-8 服务器:Microsoft-IIS / 8.5 Set-Cookie:CFID = randomCookie; Path = / Set-Cookie:CFTOKEN = 0; Path = / 返回格式:普通 X-AspNet版本:4.0.30319 X-Powered-By:ASP.NET Date:Thu,06 Jun 2019 18:15:01 GMT

0 个答案:

没有答案