如何使用WinINet通过代理连接到HTTPS

时间:2015-03-13 12:16:10

标签: c https proxy wininet

我正在尝试使用WinINet通过代理连接到HTTPS服务。我的代码如下所示:

hInternet = InternetOpenW(L"Test", INTERNET_OPEN_TYPE_PROXY,L"PROXY_IP:PROXY_PORT", L"<local>", 0);

if (hInternet == NULL)
{
    printf("InternetOpenW failed! Error code: %d\n", GetLastError());
}
else
{
    HINTERNET hConnect = InternetConnectW(hInternet, L"HTTPS_SERVICE_IP", 443, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
    if (hConnect == NULL)
    {
        printf("InternetConnectW failed! Error code: %d\n", GetLastError());
    }
    else
    {
        HINTERNET hRequest = HttpOpenRequestW(hConnect, NULL, L"", NULL, NULL, NULL, 0, 0);
        if (hRequest == NULL)
        {
            printf("HttpOpenRequestW failed! Error code: %d\n", GetLastError());
        }
        else
        {
            BOOL bRequestSent = HttpSendRequestW(hRequest, NULL, 0, NULL, 0);
            if (!bRequestSent)
            {
                printf("HttpSendRequestW failed! Error code: %d\n", GetLastError());
            }
            else
            {
                printf("Connection ok!\n");
            }

            InternetCloseHandle(hRequest);
        }

        InternetCloseHandle(hConnect);
    }

    InternetCloseHandle(hInternet);
}

它向代理发送GET请求,执行ntml身份验证,但连接失败。代理期望客户端发送CONNECT请求。如何使用该代码发送CONNECT方法?

1 个答案:

答案 0 :(得分:0)

您需要在INTERNET_FLAG_SECURE来电中添加HttpOpenRequestW标记,例如:

HINTERNET hRequest = HttpOpenRequestW(hConnect, NULL, L"", NULL, NULL, NULL, INTERNET_FLAG_SECURE, 0);

显然,您身边的应用程序代码需要识别您是在处理HTTP还是HTTP,并仅在需要时添加此标志。