Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send HTTPS request using WinInet?

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?

like image 287
Alexander Avatar asked Sep 20 '13 06:09

Alexander


1 Answers

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

This is explained in the MSDN documentation for WinINet API flags:

INTERNET_FLAG_SECURE

0x00800000

Uses secure transaction semantics. This translates to using Secure Sockets Layer/Private Communications Technology (SSL/PCT) and is only meaningful in HTTP requests. This flag is used by HttpOpenRequest and InternetOpenUrl, but this is redundant if https:// appears in the URL. The InternetConnect function uses this flag for HTTP connections; all the request handles created under this connection will inherit this flag.

like image 81
Igor Skochinsky Avatar answered Nov 16 '22 09:11

Igor Skochinsky