WinInet:如何使用C ++发送多个http get请求

时间:2016-09-13 17:45:14

标签: c++ http winapi wininet winhttp

我试图编写一个从远程服务器下载一些东西的程序,

#include <iostream>
#include <string>
#include <Windows.h>
#include <WinInet.h>
#pragma comment(lib,"wininet.lib")

using namespace std;

string Get(){
    DWORD size = 0;
    DWORD wrt;
    string msg = "";
    HINTERNET io=InternetOpen("Downloader",INTERNET_OPEN_TYPE_DIRECT,NULL,NULL,0);
    HINTERNET ic=InternetConnect(io,"192.168.1.15",8080,NULL,NULL,INTERNET_SERVICE_HTTP,0,0);
    HINTERNET hreq=HttpOpenRequest(ic,NULL,"/cgi-bin/cmd.py","HTTP/1.0",NULL,NULL,0,0);
    HttpSendRequest(hreq,NULL,0,NULL,0);
    InternetQueryDataAvailable(hreq,&size,0,0);
    char* buffer = new char[size+1];
    memset(buffer,0,size+1);
    InternetReadFile(hreq,buffer,size,&wrt);
    msg += buffer;
    free(buffer);
    InternetCloseHandle(io);
    InternetCloseHandle(ic);
    InternetCloseHandle(hreq);
    return msg;
}


int main(){
    while(TRUE){
        string msg=Get();
        if(msg.length()>1){
            cout<<msg<<endl;
        }
        Sleep(2000);
    }
return 0;
}

在另一侧(在服务器上)我运行python CGI脚本,以发送文本。 问题是程序只发送一次GET请求,即使有一个循环并且msg.length()等于0,另一方面我可以看到我刚收到一个GET请求。 有人可以解决我的问题,或任何想法......

1 个答案:

答案 0 :(得分:2)

您需要为每个WinInet API调用添加错误处理。

您还需要循环InternetReadFile(),因为它可能需要多次读取才能收到完整的响应。并且在将每个缓冲区附加到std::string时,您需要考虑实际读取的字节数。

尝试更像这样的事情:

#include <iostream>
#include <string>
#include <sstring>
#include <stdexcept>

#pragma comment(lib, "wininet.lib")

struct sHINTERNET
{
    HINTERNET hInet;

    sHINTERNET(HINTERNET AInet = NULL) : hInet(AInet) {}
    ~sHINTERNET() { InternetCloseHandle(hInet); }

    operator HINTERNET() { return hInet; }
    bool operator!() const { return !hInet; }
}

void WinInetError(const char *FuncName)
{
    DWORD dwErr = GetLastError();

    std::ostringstream oss;
    oss << FuncName << " failed!";

    if (dwErr != ERROR_INTERNET_EXTENDED_ERROR)
        oss << " Error: " << dwErr;
    else
    {
        DWORD dwLen = 0;
        InternetGetLastResponseInfo(&dwErr, NULL, &dwLen);

        if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
        {
            std::basic_string<TCHAR> msg;

            ++dwLen;
            msg.resize(dwLen);

            if (InternetGetLastResponseInfo(&dwErr, &msg[0], &dwLen))
            {
                msg.resize(dwLen);
                oss << " Error: " << msg;
            }
        }
    }

    throw std::runtime_error(oss.str());
}

std::string Download()
{
    sHINTERNET io = InternetOpen("Downloader", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
    if (!io)
        WinInetError("InternetOpen");

    sHINTERNET ic = InternetConnect(io, "192.168.1.15", 8080, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
    if (!ic)
        WinInetError("InternetConnect");

    sHINTERNET hreq = HttpOpenRequest(ic, NULL, "/cgi-bin/cmd.py", "HTTP/1.0", NULL, NULL, 0, 0);
    if (!hreq)
        WinInetError("HttpOpenRequest");

    if (!HttpSendRequest(hreq, NULL, 0, NULL, 0))
        WinInetError("HttpSendRequest");

    std::string data;
    char buffer[1024];
    DWORD wrt;

    do
    {
        if (!InternetReadFile(hreq, buffer, sizeof(buffer), &wrt))
            WinInetError("InternetReadFile");

        if (wrt == 0)
            break; 

        data.append(buffer, wrt);
    }
    while (true);

    return data;
}

int main()
{
    while (true)
    {
        try
        {
            std::string data = Download();
            std::cout << data << std::endl;
        }
        catch (const std::exception &e)
        {
            std::cerr << "Error! " << e.what() << std::endl;
        }

        Sleep(2000);
    }

    return 0;
}