QT 下使用winhttp库 使用https SSL post数据 例子
时间: 2024-03-03 11:46:51 浏览: 19
以下是使用winhttp库在QT下使用https SSL post数据的示例代码:
```cpp
#include <Windows.h>
#include <Winhttp.h>
#pragma comment(lib, "Winhttp.lib")
void postData()
{
HINTERNET hSession = NULL;
HINTERNET hConnect = NULL;
HINTERNET hRequest = NULL;
DWORD dwSize = 0;
DWORD dwDownloaded = 0;
LPSTR pszOutBuffer;
BOOL bResults = FALSE;
// Initialize WinHTTP session
hSession = WinHttpOpen(L"WinHTTP Example/1.0", WINHTTP_ACCESS_TYPE_DEFAULT_PROXY, WINHTTP_NO_PROXY_NAME, WINHTTP_NO_PROXY_BYPASS, 0);
if (!hSession)
{
qDebug() << "WinHttpOpen failed!" << GetLastError();
goto cleanup;
}
// Specify an HTTPS server
hConnect = WinHttpConnect(hSession, L"www.example.com", INTERNET_DEFAULT_HTTPS_PORT, 0);
if (!hConnect)
{
qDebug() << "WinHttpConnect failed!" << GetLastError();
goto cleanup;
}
// Create an HTTPS request
hRequest = WinHttpOpenRequest(hConnect, L"POST", L"/post", NULL, WINHTTP_NO_REFERER, WINHTTP_DEFAULT_ACCEPT_TYPES, WINHTTP_FLAG_SECURE);
if (!hRequest)
{
qDebug() << "WinHttpOpenRequest failed!" << GetLastError();
goto cleanup;
}
// Set request headers
LPCWSTR pszHeaders = L"Content-Type: application/x-www-form-urlencoded\r\n";
bResults = WinHttpAddRequestHeaders(hRequest, pszHeaders, (DWORD)-1L, WINHTTP_ADDREQ_FLAG_ADD);
if (!bResults)
{
qDebug() << "WinHttpAddRequestHeaders failed!" << GetLastError();
goto cleanup;
}
// Send the POST request
LPCWSTR pszData = L"key1=value1&key2=value2";
bResults = WinHttpSendRequest(hRequest, WINHTTP_NO_ADDITIONAL_HEADERS, 0, (LPVOID)pszData, wcslen(pszData), wcslen(pszData), 0);
if (!bResults)
{
qDebug() << "WinHttpSendRequest failed!" << GetLastError();
goto cleanup;
}
// Receive response from the server
bResults = WinHttpReceiveResponse(hRequest, NULL);
if (!bResults)
{
qDebug() << "WinHttpReceiveResponse failed!" << GetLastError();
goto cleanup;
}
// Read the server's response
do
{
// Check for available data
dwSize = 0;
if (!WinHttpQueryDataAvailable(hRequest, &dwSize))
{
qDebug() << "WinHttpQueryDataAvailable failed!" << GetLastError();
goto cleanup;
}
// Allocate space for the buffer
pszOutBuffer = new char[dwSize + 1];
if (!pszOutBuffer)
{
qDebug() << "Out of memory!";
goto cleanup;
}
// Read the data
ZeroMemory(pszOutBuffer, dwSize + 1);
if (!WinHttpReadData(hRequest, (LPVOID)pszOutBuffer, dwSize, &dwDownloaded))
{
qDebug() << "WinHttpReadData failed!" << GetLastError();
goto cleanup;
}
// Print the response to the console
qDebug() << QString::fromLocal8Bit(pszOutBuffer);
// Free the memory allocated to the buffer
delete[] pszOutBuffer;
} while (dwSize > 0);
cleanup:
// Close any open handles
if (hRequest) WinHttpCloseHandle(hRequest);
if (hConnect) WinHttpCloseHandle(hConnect);
if (hSession) WinHttpCloseHandle(hSession);
}
```
以上代码中,我们首先使用WinHttpOpen函数创建一个WinHTTP会话,然后使用WinHttpConnect函数连接到指定的HTTPS服务器。接着,我们使用WinHttpOpenRequest函数创建一个HTTPS请求,并使用WinHttpAddRequestHeaders函数设置请求头。然后,我们使用WinHttpSendRequest函数发送POST请求,并使用WinHttpReceiveResponse函数接收服务器的响应。最后,我们使用WinHttpQueryDataAvailable函数查询是否有可用的数据,使用WinHttpReadData函数读取服务器的响应,并使用qDebug打印到控制台上。注意,我们使用了QString::fromLocal8Bit函数将服务器的响应从char *转换为QString。最后,我们使用WinHttpCloseHandle函数关闭所有打开的句柄。