无法使用wininet获取POST请求响应

问题描述 投票:1回答:1

我需要向API发出POST请求以获取一些XML数据(http://freecite.library.brown.edu/welcome/api_instructions)。这适用于curl

curl -H "Accept: application/xml" --data "citation=Udvarhelyi, I.S., Gatsonis, C.A., Epstein, A.M., Pashos, C.L., Newhouse, J.P. and McNeil, B.J. Acute Myocardial Infarction in the Medicare population: process of care and clinical outcomes. Journal of the American Medical Association, 1992; 18:2530-2536. " http://freecite.library.brown.edu:80/citations/create

所以我试图使用Win32 SDK做类似的事情。这是我的代码:

void LoadData()
{
    wil::unique_hinternet hInternet(InternetOpen(L"Dummy", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0));
    wil::unique_hinternet hConnect(InternetConnect(hInternet.get(), L"http://freecite.library.brown.edu", 80, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0));
    wil::unique_hinternet hRequest(HttpOpenRequest(hConnect.get(), L"POST", L"/citations/create", NULL, NULL, NULL, NULL, NULL));
    wstring data = L"citation=Udvarhelyi, I.S., Gatsonis, C.A., Epstein, A.M., Pashos, C.L., Newhouse, J.P. and McNeil, B.J. Acute Myocardial Infarction in the Medicare population: process of care and clinical outcomes. Journal of the American Medical Association, 1992; 18:2530-2536.";
    PCWSTR szHeaders = L"Accept: application/xml";

    HttpSendRequest(hRequest.get(), szHeaders, 0, (LPVOID)data.c_str(), static_cast<int>(data.length()));

    DWORD availableBytes = 0;
    InternetQueryDataAvailable(hRequest.get(), &availableBytes, 0, 0);
    PBYTE outputBuffer = (PBYTE)HeapAlloc(GetProcessHeap(), 0, availableBytes);
    PBYTE nextBytes = outputBuffer;
    DWORD bytesUsed = 0; // number of used bytes.
    while (availableBytes)
    {
        DWORD downloadedBytes;
        InternetReadFile(hRequest.get(), nextBytes, availableBytes, &downloadedBytes);
        bytesUsed = bytesUsed + downloadedBytes;

        InternetQueryDataAvailable(hRequest.get(), &availableBytes, 0, 0);
        if (availableBytes > 0)
        {
            // lazy buffer growth here. Only alloc for what we need. could be optimized if we end up with huge payloads (>10MB).
            // otherwise, this is good enough.
            outputBuffer = (PBYTE)HeapReAlloc(GetProcessHeap(), 0, outputBuffer, bytesUsed + availableBytes);
            nextBytes = outputBuffer + bytesUsed; // careful, pointer might have moved! Update cursor.
        }
    }

    // Convert outputed XML to wide char
    int size_needed = MultiByteToWideChar(CP_UTF8, 0, (PCCH)outputBuffer, bytesUsed, NULL, 0);
    std::wstring wstrTo(size_needed, 0);
    MultiByteToWideChar(CP_UTF8, 0, (PCCH)outputBuffer, bytesUsed, &wstrTo[0], size_needed);

    wstring res = wstrTo;
}

问题是,在进入for循环之前,甚至在调用InternetQueryDataAvailable之后,availableBytes出现为0.结果,我最终得到一个空字符串作为响应,而我期待XML响应。

任何人都可以指出我做错了什么,以及如何解决它?

c++ winapi wininet
1个回答
1
投票

InternetConnect需要服务器名称或IP地址,因此请勿在地址中包含"http://"。改成:

InternetConnect(handle, L"freecite.library.brown.edu"...);

data使用UTF-8。 WinAPI函数的其他参数正确使用UTF-16,它们会自动进行必要的转换。

更改标题:

std::wstring szHeaders = L"Content-Type: application/x-www-form-urlencoded\r\n";

accept应该通过HttpOpenRequest发送

const wchar_t *accept[] = { L"text/xml", NULL };
HINTERNET hrequest = HttpOpenRequest(hconnect, L"POST", L"/citations/create",
    NULL, NULL, accept, 0, 0);

注意,如果你没有指定accept(在其位置使用NULL),那么结果可以是普通的html。

以下示例应返回XML数据

std::wstring get_utf16(const std::string &str);
std::string get_utf8(const std::wstring &wstr);

int main()
{
    HINTERNET hsession = NULL;
    HINTERNET hconnect = NULL;
    HINTERNET hrequest = NULL;
    std::wstring result;
    std::wstring szHeaders = L"Content-Type: application/x-www-form-urlencoded\r\n";
    std::wstring data_w = L"citation=Udvarhelyi, I.S., Gatsonis, C.A., Epstein, A.M., Pashos, C.L., Newhouse, J.P. and McNeil, B.J. Acute Myocardial Infarction in the Medicare population: process of care and clinical outcomes. Journal of the American Medical Association, 1992; 18:2530-2536.";
    std::string data = get_utf8(data_w);

    hsession = InternetOpen(L"appname", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
    if(!hsession)
        goto cleanup;

    hconnect = InternetConnect(hsession, L"freecite.library.brown.edu",
        INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
    if(!hconnect)
        goto cleanup;

    const wchar_t *accept[] = { L"text/xml", NULL };
    hrequest = HttpOpenRequest(hconnect, L"POST", L"/citations/create",
        NULL, NULL, accept, 0, 0);
    if(!hrequest)
        goto cleanup;
    BOOL sent = HttpSendRequest(hrequest, szHeaders.c_str(), szHeaders.size(),
        &data[0], data.size());

    if(sent)
    {
        DWORD blocksize = 4096;
        DWORD received = 0;
        std::string temp;
        std::string block(blocksize, 0);
        while(InternetReadFile(hrequest, &block[0], blocksize, &received) && received)
        {
            block.resize(received);
            temp += block;
        }
        result = get_utf16(temp);
        std::wcout << result << std::endl;
    }

cleanup:
    if(hrequest)  InternetCloseHandle(hrequest);
    if(hconnect)  InternetCloseHandle(hconnect);
    if(hsession)  InternetCloseHandle(hsession);
    return 0;
}

std::wstring get_utf16(const std::string &str)
{
    if(str.empty()) return std::wstring();
    int sz = MultiByteToWideChar(CP_UTF8, 0, &str[0], -1, 0, 0);
    std::wstring res(sz, 0);
    MultiByteToWideChar(CP_UTF8, 0, &str[0], -1, &res[0], sz);
    return res;
}

std::string get_utf8(const std::wstring &wstr)
{
    int sz = WideCharToMultiByte(CP_UTF8, 0, &wstr[0], -1, 0, 0, 0, 0);
    std::string res(sz, 0);
    WideCharToMultiByte(CP_UTF8, 0, &wstr[0], -1, &res[0], sz, 0, 0);
    return res;
}
© www.soinside.com 2019 - 2024. All rights reserved.