如何在C ++上使用WinHTTP进行HTTPS调用(POST请求)?

时间:2014-09-02 10:09:57

标签: c++ https winhttp

我在C ++上编写程序,使用WinHTTP对Java Servlet进行调用(POST请求)。当我通过HTTP请求POST时一切正常,当我通过HTTPS请求时出现问题。它向服务器发送空请求,但切断请求正文(但它有内容)

int sendPostRequest(char *pszPostData, LPCTSTR servletUrl, char* resultBuffer, ofstream &outputFile) {

    outputFile << "====================================== SENDING REEQUEST ======================================" << endl;

    HINTERNET hSession = WinHttpOpen(
        userAgent,
        WINHTTP_ACCESS_TYPE_NO_PROXY,
        WINHTTP_NO_PROXY_NAME,
        WINHTTP_NO_PROXY_BYPASS,
        0);
    if (!hSession)
    {
        _tprintf(_TEXT("Failed to open WinHTTP session: %ld\n"), GetLastError());
        outputFile << "Failed to open WinHTTP session: %ld\n" << GetLastError() << endl;
        return NULL;
    }
    else {
        _tprintf(_TEXT("Oppening WinHTTP session successful: %ld\n"), GetLastError());
        outputFile << "Oppening WinHTTP session successful: %ld\n" << GetLastError() << endl;
    }

    HINTERNET hConnect = WinHttpConnect(
        hSession,
        serverIP,
        serverPort,
        0);
    if (!hConnect)
    {
        _tprintf(_TEXT("Failed to connect to server: %ld\n"), GetLastError());
        outputFile << "Failed to connect to server: %ld\n" << GetLastError() << endl;
        WinHttpCloseHandle(hSession);
        return NULL;
    }
    else {
        _tprintf(_TEXT("Connection to server successful: %ld\n"), GetLastError());
        outputFile << "Connection to server successful: %ld\n" << GetLastError() << endl;
    }

    _tprintf(_TEXT("Post data : %ld\n"), pszPostData);
    outputFile << "Post data : %ld\n" << pszPostData << endl;
    DWORD dwDataLen = strlen(pszPostData);

    HINTERNET hRequest = WinHttpOpenRequest(
        hConnect,
        _TEXT("POST"),
        servletUrl,
        NULL,
        WINHTTP_NO_REFERER,
        WINHTTP_DEFAULT_ACCEPT_TYPES,
        WINHTTP_FLAG_REFRESH);
    if (!hRequest)
    {
        _tprintf(_TEXT("Failed to open request: %ld\n"), GetLastError());
        outputFile << "Failed to open request: %ld\n" << GetLastError() << endl;
        WinHttpCloseHandle(hConnect);
        WinHttpCloseHandle(hSession);
        return -1;
    }
    else {
        _tprintf(_TEXT("Opening request successful: %ld\n"), GetLastError());
        outputFile << "Opening request successful: %ld\n" << GetLastError() << endl;
    }

    DWORD dwReqOpts = 0;
    DWORD dwSize = sizeof(DWORD);
    WinHttpSetOption(
        hRequest,
        WINHTTP_OPTION_SECURITY_FLAGS,
        &dwReqOpts,
        sizeof(DWORD));

    BOOL done = false;
    BOOL rc = WinHttpSendRequest(
        hRequest,
        contentTypeHeader,
        -1,
        (LPVOID)pszPostData,
        dwDataLen,
        dwDataLen,
        NULL);

    if (rc) {
        rc = WinHttpReceiveResponse(hRequest, NULL);
        _tprintf(_TEXT("Sending request successful: %ld\n"), GetLastError());
        outputFile << "Sending request successful: %ld\n" << GetLastError() << endl;
    }
    else
    {
        _tprintf(_TEXT("Send request failed: %ld\n"), GetLastError());
        outputFile << "Send request failed: %ld\n" << GetLastError() << endl;
        WinHttpCloseHandle(hRequest);
        WinHttpCloseHandle(hConnect);
        WinHttpCloseHandle(hSession);
        return -1;
    }

    // Get the status from the server
    DWORD dwCode = 0;
    if (rc)
    {
        rc = WinHttpQueryHeaders(
            hRequest,
            WINHTTP_QUERY_STATUS_CODE | WINHTTP_QUERY_FLAG_NUMBER,
            NULL,
            &dwCode,
            &dwSize,
            NULL);
    }

    if (dwCode != HTTP_STATUS_OK)
    {
        _tprintf(_TEXT("HTTP Request failed: %ld\n"), dwCode);
        outputFile << "HTTP Request failed: %ld\n" << dwCode << endl;
        WinHttpCloseHandle(hRequest);
        WinHttpCloseHandle(hConnect);
        WinHttpCloseHandle(hSession);
    }
    else
    {
        _tprintf(_TEXT("HTTP Request is ok: %ld\n"), dwCode);
        outputFile << "HTTP Request is ok : %ld\n" << dwCode << endl;

        // Keep reading from the remote server until there's nothing left to read
        DWORD dwBytesToBeRead = 0, dwBytesRead = 0;
        //char szBuffer[8192] = { 0 };
        //strcpy(resultBuffer, "");
        do
        {
            if (!WinHttpQueryDataAvailable(hRequest, &dwBytesToBeRead))
            {
                _tprintf(_TEXT("No data available from server? %ld"), GetLastError());
                outputFile << "No data available from server? %ld" << GetLastError() << endl;

                WinHttpCloseHandle(hRequest);
                WinHttpCloseHandle(hConnect);
                WinHttpCloseHandle(hSession);
                return -1;
            }

            if (!WinHttpReadData(
                hRequest,

                //szBuffer,
                resultBuffer,

                //sizeof(szBuffer),
                RESULT_BUFFER_SIZE,

                &dwBytesRead))
            {
                _tprintf(_TEXT("Failed to read data from server: %ld"), GetLastError());
                outputFile << "Failed to read data from server: %ld" << GetLastError() << endl;

                WinHttpCloseHandle(hRequest);
                WinHttpCloseHandle(hConnect);
                WinHttpCloseHandle(hSession);
                return -1;
            }
            if (dwBytesRead > 0)
            {
                //szBuffer[dwBytesRead] = 0;
                resultBuffer[dwBytesRead] = 0; // NULL-terminated returned buffer
            }
        } while (dwBytesRead > 0);
        WinHttpCloseHandle(hRequest);
        WinHttpCloseHandle(hConnect);
        WinHttpCloseHandle(hSession);
        return 0;
    }
    return -1;
}

其中pszPostData - 请求正文的内容,servletUrl - url到Servlet(端点),resultBuffer - 调用结果将被写入此缓冲区outputFile - 文件用于日志。

那么如何在不切断请求体的情况下进行HTTPS调用?

2 个答案:

答案 0 :(得分:4)

关于 WinHttpSendRequest 方法,在第2个paramaet插入:L"content-type:application/x-www-form-urlencoded"和第3个参与者-1,根据w3。

它应该适用于此修正。

另外,检查.php文件的编码,给我一次麻烦。

答案 1 :(得分:0)

我今天遇到了这个问题,因为我面临着一个非常相似的问题。在 WinHttpConnect() 中,我将 pswzServerName 作为“example.com”发送。问题是,在 Apache 中,我强制将 domain.com 重定向到 www.domain.com,并将 http 重定向到 https。此重定向导致没有发送 POST 数据、错误的内容长度标头和错误的内容类型标头,因为我在 example.com 中指定了 WinHttpConnect()

有两种解决方案,最后一种是最佳选择:

Removing my htaccess non-www to www and http to https redirect

OR

just changing `pswzServerName` to 'www.example.com' (including the www.) in WinHttpConnect()

花了很长时间试图弄清楚为什么 winhttp 不起作用,因为这很容易被忽视,所以希望它可以帮助可能在同一条船上的其他人。