qt 使用 winhttp POST https demo
时间: 2023-09-17 22:09:04 浏览: 188
winhttp_it_winhttp_
以下是使用 Qt 的 WinHTTP 发送 POST 请求的示例代码:
```cpp
#include <Windows.h>
#include <Winhttp.h>
#include <QDebug>
#pragma comment(lib, "Winhttp.lib")
void sendRequest()
{
// 打开一个会话句柄
HINTERNET hSession = WinHttpOpen(L"WinHTTP Example/1.0", WINHTTP_ACCESS_TYPE_DEFAULT_PROXY, WINHTTP_NO_PROXY_NAME, WINHTTP_NO_PROXY_BYPASS, 0);
if (!hSession) {
qWarning() << "WinHttpOpen failed: " << GetLastError();
return;
}
// 连接服务器
HINTERNET hConnect = WinHttpConnect(hSession, L"www.example.com", INTERNET_DEFAULT_HTTPS_PORT, 0);
if (!hConnect) {
qWarning() << "WinHttpConnect failed: " << GetLastError();
WinHttpCloseHandle(hSession);
return;
}
// 创建一个请求句柄
HINTERNET hRequest = WinHttpOpenRequest(hConnect, L"POST", L"/api", NULL, WINHTTP_NO_REFERER, WINHTTP_DEFAULT_ACCEPT_TYPES, WINHTTP_FLAG_SECURE);
if (!hRequest) {
qWarning() << "WinHttpOpenRequest failed: " << GetLastError();
WinHttpCloseHandle(hConnect);
WinHttpCloseHandle(hSession);
return;
}
// 设置请求头
LPCWSTR headers = L"Content-Type: application/json\r\n";
BOOL bResults = WinHttpAddRequestHeaders(hRequest, headers, (ULONG)wcslen(headers), WINHTTP_ADDREQ_FLAG_ADD);
if (!bResults) {
qWarning() << "WinHttpAddRequestHeaders failed: " << GetLastError();
WinHttpCloseHandle(hRequest);
WinHttpCloseHandle(hConnect);
WinHttpCloseHandle(hSession);
return;
}
// 设置请求体
LPCWSTR data = L"{\"name\":\"example\",\"age\":18}";
bResults = WinHttpSendRequest(hRequest, WINHTTP_NO_ADDITIONAL_HEADERS, 0, (LPVOID)data, (DWORD)wcslen(data) * sizeof(WCHAR), (DWORD)wcslen(data) * sizeof(WCHAR), 0);
if (!bResults) {
qWarning() << "WinHttpSendRequest failed: " << GetLastError();
WinHttpCloseHandle(hRequest);
WinHttpCloseHandle(hConnect);
WinHttpCloseHandle(hSession);
return;
}
// 发送请求
bResults = WinHttpReceiveResponse(hRequest, NULL);
if (!bResults) {
qWarning() << "WinHttpReceiveResponse failed: " << GetLastError();
WinHttpCloseHandle(hRequest);
WinHttpCloseHandle(hConnect);
WinHttpCloseHandle(hSession);
return;
}
// 读取响应体
DWORD dwSize = 0;
DWORD dwDownloaded = 0;
LPSTR pszOutBuffer;
std::string strResponse;
do {
// 获取响应体的大小
dwSize = 0;
if (!WinHttpQueryDataAvailable(hRequest, &dwSize)) {
qWarning() << "WinHttpQueryDataAvailable failed: " << GetLastError();
WinHttpCloseHandle(hRequest);
WinHttpCloseHandle(hConnect);
WinHttpCloseHandle(hSession);
return;
}
// 分配缓存
pszOutBuffer = new char[dwSize + 1];
if (!pszOutBuffer) {
qWarning() << "Out of memory";
WinHttpCloseHandle(hRequest);
WinHttpCloseHandle(hConnect);
WinHttpCloseHandle(hSession);
return;
}
// 读取响应体
ZeroMemory(pszOutBuffer, dwSize + 1);
if (!WinHttpReadData(hRequest, (LPVOID)pszOutBuffer, dwSize, &dwDownloaded)) {
qWarning() << "WinHttpReadData failed: " << GetLastError();
delete[] pszOutBuffer;
WinHttpCloseHandle(hRequest);
WinHttpCloseHandle(hConnect);
WinHttpCloseHandle(hSession);
return;
}
// 添加到响应字符串中
strResponse.append(pszOutBuffer, dwDownloaded);
delete[] pszOutBuffer;
} while (dwSize > 0);
// 输出响应字符串
qDebug() << QString::fromStdString(strResponse);
// 关闭句柄
WinHttpCloseHandle(hRequest);
WinHttpCloseHandle(hConnect);
WinHttpCloseHandle(hSession);
}
```
在上面的代码中,我们使用 WinHTTP 库发送了一个 HTTPS POST 请求,请求体为 JSON 格式的数据。你需要根据实际情况修改以下参数:
- `L"www.example.com"`:服务器域名或 IP 地址。
- `L"/api"`:请求的 API 路径。
- `L"Content-Type: application/json\r\n"`:请求头中的内容类型,根据实际情况修改。
- `L"{\"name\":\"example\",\"age\":18}"`:请求体的内容,根据实际情况修改。
阅读全文