How to send HTTPS request using WinInet?

▼魔方 西西 提交于 2020-04-08 09:26:06

问题


I want to send HTTPS GET request using WinInet. As far as i know, i should do it just like sending HTTP request except i have to use INTERNET_DEFAULT_HTTPS_PORT and INTERNET_FLAG_SECURE flag.

So here is what i tried:

#include "stdafx.h"
#include <string>
#include <windows.h>
#include <WinInet.h>

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

using namespace std;


// convert string
wstring CharPToWstring(const char* _charP)
{
    return wstring(_charP, _charP + strlen(_charP));
}

// send https request
wstring SendHTTPSRequest_GET(const wstring& _server,
                             const wstring& _page,
                             const wstring& _params = L"")
{
    char szData[1024];

    // initialize WinInet
    HINTERNET hInternet = ::InternetOpen(TEXT("WinInet Test"), INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
    if (hInternet != NULL) 
    {
        // open HTTP session
        HINTERNET hConnect = ::InternetConnect(hInternet, _server.c_str(), INTERNET_DEFAULT_HTTPS_PORT, NULL,NULL, INTERNET_SERVICE_HTTP, INTERNET_FLAG_SECURE, 1);
        if (hConnect != NULL) 
        {
            wstring request = _page + 
                    (_params.empty() ? L"" : (L"?" + _params));

            // open request
            HINTERNET hRequest = ::HttpOpenRequest(hConnect, L"GET", (LPCWSTR)request.c_str() ,NULL, NULL, 0, INTERNET_FLAG_KEEP_CONNECTION, 1);
            if (hRequest != NULL) 
            {   
                // send request
                BOOL isSend = ::HttpSendRequest(hRequest, NULL, 0, NULL, 0);

                if (isSend)
                {
                    for(;;)
                    {
                        // reading data
                        DWORD dwByteRead;
                        BOOL isRead = ::InternetReadFile(hRequest, szData, sizeof(szData) - 1, &dwByteRead);

                        // break cycle if error or end
                        if (isRead == FALSE || dwByteRead == 0)
                            break;

                        // saving result
                        szData[dwByteRead] = 0;
                    }
                }

                // close request
                ::InternetCloseHandle(hRequest);
            }
            // close session
            ::InternetCloseHandle(hConnect);
        }
        // close WinInet
       ::InternetCloseHandle(hInternet);
    }

    wstring answer = CharPToWstring(szData);

    return answer;
}


int _tmain(int argc, _TCHAR* argv[])
{
    wstring answer = SendHTTPSRequest_GET(L"www.site.com", L"page.php", L"param1=value1&param2=value2&param3=value3&param4=value4");

    return 0;
}

And my function returned an answer:

<html>

<head><title>400 The plain HTTP request was sent to HTTPS port</title></head>

<body bgcolor="white">

<center><h1>400 Bad Request</h1></center>

<center>The plain HTTP request was sent to HTTPS port</center>

<hr><center>nginx/1.0.10</center>

</body>

</html>

What am i doing wrong?


回答1:


INTERNET_FLAG_SECURE should be used in the flags for HttpOpenRequest, not InternetConnect. Read the docs carefully.



来源:https://stackoverflow.com/questions/18910463/how-to-send-https-request-using-wininet

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!