如何使用WinInet发送HTTPS请求? [英] How to send HTTPS request using WinInet?

查看:472
本文介绍了如何使用WinInet发送HTTPS请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用WinInet发送HTTPS GET请求.据我所知,我应该像发送HTTP请求那样去做,除了我必须使用 INTERNET_DEFAULT_HTTPS_PORT INTERNET_FLAG_SECURE 标志.

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.

这就是我尝试过的:

#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>

我在做什么错了?

推荐答案

INTERNET_FLAG_SECURE应该用于HttpOpenRequest而不是InternetConnect的标志中.仔细阅读文档.

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

这篇关于如何使用WinInet发送HTTPS请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆