使用设置的用户代理c ++进行数据下载

时间:2018-07-20 19:04:59

标签: c++ http

我有一个将信息上传到字节数组的功能,但是它以-if (!Httpsendrequest(httprequest, szHeaders, strlen(szhheaders), szRequest, strlen(szRequest)))

结尾
string GetUrlData(const string& url, LPCSTR host, string& output)
{
    string request_data = "";
    output = "";
    HINTERNET hIntSession = InternetOpenA("token", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
   if (!hIntSession)
   {
       return request_data;
   }
    HINTERNET hHttpSession = InternetConnectA(hIntSession, host, 80, 0, 0, INTERNET_SERVICE_HTTP, 0, NULL);
   if (!hHttpSession)
   {
       return request_data;
   }
    HINTERNET hHttpRequest = HttpOpenRequestA(hHttpSession, "GET", url.c_str()
        , 0, 0, 0, INTERNET_FLAG_RELOAD, 0);
   if (!hHttpSession)
   {
       return request_data;
   }
   char* szHeaders = ("Content-Type: text/html\r\nUser-Agent: License");
   char szRequest[1024] = { 0 };
   if (!HttpSendRequestA(hHttpRequest, szHeaders, strlen(szHeaders), szRequest, strlen(szRequest)))
{
       qDebug()<<"BLYA";
       return request_data;
   }
    CHAR szBuffer[1024] = { 0 };
    DWORD dwRead = 0;
   while (InternetReadFile(hHttpRequest, szBuffer, sizeof(szBuffer) - 1, &dwRead) && dwRead)
   {
        request_data.append(szBuffer, dwRead);
   }
    InternetCloseHandle(hHttpRequest);
    InternetCloseHandle(hHttpSession);
    InternetCloseHandle(hIntSession);
    output = request_data;
}

1 个答案:

答案 0 :(得分:0)

该故障可能与szHeaders标头上的\r\n缺少User-Agent的事实有关。您发送的每个标头都必须以\r\n结尾。顺便说一句,由于没有消息正文发送到服务器,因此没有理由在Content-Type请求中发送GET头。

此外,如果发生任何错误,您的代码也会发生大量内存泄漏。您需要关闭成功打开的每个手柄。

此外,同时具有返回相同数据的返回值和输出参数毫无意义。选择一个或另一个。我建议使用bool返回值和string输出参数。

尝试更多类似的方法:

bool GetUrlData(const string& resource, const string& host)
{
    output.clear();
    bool result = false;

    HINTERNET hIntSession = InternetOpenA("token", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
    if (!hIntSession) {
        qDebug() << "InternetOpen failed, error: " << GetLastError();
        return false;
    }

    HINTERNET hHttpSession = InternetConnectA(hIntSession, host.c_str(), 80, 0, 0, INTERNET_SERVICE_HTTP, 0, NULL);
    if (!hHttpSession) {
        qDebug() << "InternetConnect failed, error: " << GetLastError();
        InternetCloseHandle(hIntSession);
        return false;
    }

    HINTERNET hHttpRequest = HttpOpenRequestA(hHttpSession, "GET", resource.c_str(), 0, 0, 0, INTERNET_FLAG_RELOAD, 0);
    if (!hHttpRequest) {
        qDebug() << "HttpOpenRequest failed, error: " << GetLastError();
        InternetCloseHandle(hHttpSession);
        InternetCloseHandle(hIntSession);
        return false;
    }

    const char* szHeaders = "User-Agent: License\r\n";
    if (!HttpSendRequestA(hHttpRequest, szHeaders, -1, NULL, 0)) {
        qDebug() << "HttpSendRequest failed, error: " << GetLastError();
        InternetCloseHandle(hHttpRequest);
        InternetCloseHandle(hHttpSession);
        InternetCloseHandle(hIntSession);
        return false;
    }

    char szBuffer[1024];
    DWORD dwRead = 0;

    do {
        if (!InternetReadFile(hHttpRequest, szBuffer, sizeof(szBuffer), &dwRead)) {
            qDebug() << "InternetReadFile failed, error: " << GetLastError();
            InternetCloseHandle(hHttpRequest);
            InternetCloseHandle(hHttpSession);
            InternetCloseHandle(hIntSession);
            return false;
        }

        if (dwRead == 0)
            break;

        output.append(szBuffer, dwRead);
    }
    while (true);

    return true;
}