如何使用WinHTTP将C ++ HTTPS客户端连接到Python HTTPS Server

问题描述

我正在尝试在HTTPS python服务器与HTTPS c ++客户端之间建立HTTPS连接。我使用浏览器测试了服务器,并且工作正常,但是我在使用c ++客户端的winhttp库时遇到了麻烦,无法建立正确的连接。
我尝试在线阅读有关该问题和Winhttp库的更多信息,但我发现问题出在客户端的服务器证书配置上,但找不到合适的解决方案,当我尝试阅读时迷失了方向关于winhtttp库中的不同功能。如何与服务器和客户端正确建立HTTPS连接?
(这两个程序都在Windows上运行。在运行服务器以及运行客户端之后,我收到消息Error 12175 has occurred.

我的服务器代码是:

from http.server import HTTPServer,BaseHTTPRequestHandler
import ssl
import pathlib


class myHandler(BaseHTTPRequestHandler):
    def do_GET(self):
        self.send_response(200)
        self.send_header('content-type','text/html')
        self.end_headers()
        self.wfile.write(self.path.encode())


def run(server_class=HTTPServer):

    server_address = ('',8000)
    httpd = server_class(server_address,myHandler)

    currPath = str(pathlib.Path(__file__).parent.absolute())
    keyPath = currPath + '\\server.pem'
    httpd.socket = ssl.wrap_socket(
        httpd.socket,certfile=keyPath,server_side=True)

    httpd.serve_forever()


run()

我的客户代码

#include <iostream>
#include <ws2tcpip.h>
#include <winhttp.h>
#pragma comment(lib,"winhttp.lib")
using namespace std;

void main()
{
    DWORD dwSize = 0;
    DWORD dwDownloaded = 0;
    LPSTR pszOutBuffer;
    BOOL bResults = FALSE;
    HINTERNET hSession = NULL,hConnect = NULL,hRequest = NULL;

    // Use WinHttpOpen to obtain a session handle.
    hSession = WinHttpOpen(L"WinHTTP Example/1.0",WINHTTP_ACCESS_TYPE_DEFAULT_PROXY,WINHTTP_NO_PROXY_NAME,WINHTTP_NO_PROXY_BYPASS,0);

    // Specify an HTTP server.
    if (hSession)
    {
        hConnect = WinHttpConnect(hSession,L"localhost",8000,0);
    }

    // Create an HTTP request handle.
    if (hConnect)
    {
        hRequest = WinHttpOpenRequest(hConnect,L"GET",NULL,WINHTTP_NO_REFERER,WINHTTP_DEFAULT_ACCEPT_TYPES,WINHTTP_FLAG_SECURE);
    }

    // Send a request.
    if (hRequest)
    {
        bResults = WinHttpSendRequest(hRequest,WINHTTP_NO_ADDITIONAL_HEADERS,WINHTTP_NO_REQUEST_DATA,0);
    }

    // End the request.
    if (bResults)
    {
        bResults = WinHttpReceiveResponse(hRequest,NULL);
    }

    // Keep checking for data until there is nothing left.
    if (bResults)
    {
        do
        {
            // Check for available data.
            dwSize = 0;
            if (!WinHttpQueryDataAvailable(hRequest,&dwSize))
                printf("Error %u in WinHttpQueryDataAvailable.\n",GetLastError());

            // Allocate space for the buffer.
            pszOutBuffer = new char[dwSize + 1];
            if (!pszOutBuffer)
            {
                printf("Out of memory\n");
                dwSize = 0;
            }
            else
            {
                // Read the data.
                ZeroMemory(pszOutBuffer,dwSize + 1);

                if (!WinHttpReadData(hRequest,(LPVOID)pszOutBuffer,dwSize,&dwDownloaded))
                    printf("Error %u in WinHttpReadData.\n",GetLastError());
                else
                    printf("%s",pszOutBuffer);

                // Free the memory allocated to the buffer.
                delete[] pszOutBuffer;
            }
        } while (dwSize > 0);
    }

    // Report any errors.
    if (!bResults)
        printf("Error %d has occurred.\n",GetLastError());

    // Close any open handles.
    if (hRequest)
        WinHttpCloseHandle(hRequest);
    if (hConnect)
        WinHttpCloseHandle(hConnect);
    if (hSession)
        WinHttpCloseHandle(hSession);
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)