winhttp 访问https,winhttp https获取错误12044

I use winhttp api to sendrequest to server,but I income the problem, I got 12044 error, I find the error is ERROR_WINHTTP_CLIENT_AUTH_CERT_NEEDED. Any one know how to solve it?

hConnect = WinHttpConnect(hOpen, L"raw.github.com", INTERNET_DEFAULT_HTTPS_PORT, 0);

if (!hConnect) {

wprintf(L"WinHttpConnect failed (0x%.8X)\n", GetLastError());

break;

}

LPCWSTR types[2];

types[0] = L"text/html";

types[1] = 0;

// use flag WINHTTP_FLAG_SECURE to initiate SSL

hRequest = WinHttpOpenRequest(hConnect, L"GET", L"zpfzzz/test/master/README.md",

NULL, WINHTTP_NO_REFERER, &types[0], WINHTTP_FLAG_SECURE);

解决方案There is a nice explanation and suggestion for a solution in MSDN here.

Basically, upon receipt of this error, you open your MY store using cryptoapi and specify an identification certificate you need to have there.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用 WinHTTP 在 Qt 中进行 HTTPS 请求的示例代码: ```cpp #include <QCoreApplication> #include <QUrl> #include <QDebug> #include <QByteArray> #include <Windows.h> #include <winhttp.h> #pragma comment(lib,"winhttp.lib") int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); // 创建 WinHTTP 会话 HINTERNET hSession = WinHttpOpen(L"WinHTTP Example/1.0", WINHTTP_ACCESS_TYPE_DEFAULT_PROXY, WINHTTP_NO_PROXY_NAME, WINHTTP_NO_PROXY_BYPASS, WINHTTP_FLAG_SECURE); if (!hSession) { qDebug() << "WinHttpOpen failed: " << GetLastError(); return -1; } // 创建 HTTP 连接 QUrl url("https://www.example.com"); HINTERNET hConnect = WinHttpConnect(hSession, reinterpret_cast<const wchar_t*>(url.host().utf16()), url.port() == -1 ? INTERNET_DEFAULT_HTTPS_PORT : url.port(), 0); if (!hConnect) { WinHttpCloseHandle(hSession); qDebug() << "WinHttpConnect failed: " << GetLastError(); return -1; } // 创建 HTTP 请求 HINTERNET hRequest = WinHttpOpenRequest(hConnect, L"GET", reinterpret_cast<const wchar_t*>(url.path().utf16()), L"HTTP/1.1", WINHTTP_NO_REFERER, WINHTTP_DEFAULT_ACCEPT_TYPES, WINHTTP_FLAG_SECURE); if (!hRequest) { WinHttpCloseHandle(hConnect); WinHttpCloseHandle(hSession); qDebug() << "WinHttpOpenRequest failed: " << GetLastError(); return -1; } // 发送 HTTP 请求 if (!WinHttpSendRequest(hRequest, WINHTTP_NO_ADDITIONAL_HEADERS, 0, WINHTTP_NO_REQUEST_DATA, 0, 0, 0)) { WinHttpCloseHandle(hRequest); WinHttpCloseHandle(hConnect); WinHttpCloseHandle(hSession); qDebug() << "WinHttpSendRequest failed: " << GetLastError(); return -1; } // 等待响应 if (!WinHttpReceiveResponse(hRequest, 0)) { WinHttpCloseHandle(hRequest); WinHttpCloseHandle(hConnect); WinHttpCloseHandle(hSession); qDebug() << "WinHttpReceiveResponse failed: " << GetLastError(); return -1; } // 读取响应数据 QByteArray responseData; DWORD dwSize = 0; do { char szData[1024] = { 0 }; if (!WinHttpReadData(hRequest, szData, sizeof(szData) - 1, &dwSize)) { WinHttpCloseHandle(hRequest); WinHttpCloseHandle(hConnect); WinHttpCloseHandle(hSession); qDebug() << "WinHttpReadData failed: " << GetLastError(); return -1; } responseData.append(szData, static_cast<int>(dwSize)); } while (dwSize > 0); // 输出响应数据 qDebug() << responseData; // 关闭 WinHTTP 相关句柄 WinHttpCloseHandle(hRequest); WinHttpCloseHandle(hConnect); WinHttpCloseHandle(hSession); return a.exec(); } ``` 在这个示例中,我们增加了一个 `WINHTTP_FLAG_SECURE` 标志来打开安全连接。请注意,在使用 HTTPS 请求时,我们需要确保我们使用的证书是受信任的,否则 WinHTTP 将无法建立安全连接。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值