用WinHttp 写个访问https://www.amazon.com.au/ap/signin的函数 并且返回响应内容和Headers
时间: 2023-07-19 21:19:06 浏览: 144
好的,以下是用WinHttp访问https://www.amazon.com.au/ap/signin并返回响应内容和Headers的函数:
```c++
#include <Windows.h>
#include <Winhttp.h>
#include <iostream>
#include <vector>
#pragma comment(lib, "winhttp.lib")
std::vector<std::string> split(const std::string& str, const std::string& delim) {
std::vector<std::string> res;
if (str.empty()) {
return res;
}
std::string::size_type start = 0;
std::string::size_type index = str.find(delim, start);
while (index != std::string::npos) {
res.push_back(str.substr(start, index - start));
start = index + delim.length();
index = str.find(delim, start);
}
res.push_back(str.substr(start));
return res;
}
std::string UrlEncode(const std::string& str) {
std::string res;
for (size_t i = 0; i < str.length(); ++i) {
char c = str[i];
if ((c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z') ||
(c >= 'a' && c <= 'z') || c == '-' || c == '_' || c == '.' || c == '~') {
res.push_back(c);
} else if (c == ' ') {
res.push_back('+');
} else {
res.push_back('%');
res.push_back("0123456789ABCDEF"[((unsigned char)c) >> 4]);
res.push_back("0123456789ABCDEF"[((unsigned char)c) & 15]);
}
}
return res;
}
std::string AmazonSignIn() {
// 创建会话句柄
HINTERNET hSession = WinHttpOpen(L"WinHTTP Example/1.0", WINHTTP_ACCESS_TYPE_DEFAULT_PROXY, WINHTTP_NO_PROXY_NAME, WINHTTP_NO_PROXY_BYPASS, 0);
if (!hSession) {
std::cout << "WinHttpOpen failed: " << GetLastError() << std::endl;
return "";
}
// 创建连接句柄
HINTERNET hConnect = WinHttpConnect(hSession, L"www.amazon.com.au", INTERNET_DEFAULT_HTTPS_PORT, 0);
if (!hConnect) {
std::cout << "WinHttpConnect failed: " << GetLastError() << std::endl;
WinHttpCloseHandle(hSession);
return "";
}
// 创建请求句柄
HINTERNET hRequest = WinHttpOpenRequest(hConnect, L"GET", L"/ap/signin", NULL, WINHTTP_NO_REFERER, WINHTTP_DEFAULT_ACCEPT_TYPES, WINHTTP_FLAG_SECURE);
if (!hRequest) {
std::cout << "WinHttpOpenRequest failed: " << GetLastError() << std::endl;
WinHttpCloseHandle(hConnect);
WinHttpCloseHandle(hSession);
return "";
}
// 发送请求
if (!WinHttpSendRequest(hRequest, WINHTTP_NO_ADDITIONAL_HEADERS, 0, WINHTTP_NO_REQUEST_DATA, 0, 0, 0)) {
std::cout << "WinHttpSendRequest failed: " << GetLastError() << std::endl;
WinHttpCloseHandle(hRequest);
WinHttpCloseHandle(hConnect);
WinHttpCloseHandle(hSession);
return "";
}
// 等待响应
if (!WinHttpReceiveResponse(hRequest, NULL)) {
std::cout << "WinHttpReceiveResponse failed: " << GetLastError() << std::endl;
WinHttpCloseHandle(hRequest);
WinHttpCloseHandle(hConnect);
WinHttpCloseHandle(hSession);
return "";
}
// 获取响应头部信息
DWORD dwSize = 0;
WinHttpQueryHeaders(hRequest, WINHTTP_QUERY_RAW_HEADERS_CRLF, WINHTTP_HEADER_NAME_BY_INDEX, NULL, &dwSize, WINHTTP_NO_HEADER_INDEX);
if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
std::vector<char> buffer(dwSize);
if (!WinHttpQueryHeaders(hRequest, WINHTTP_QUERY_RAW_HEADERS_CRLF, WINHTTP_HEADER_NAME_BY_INDEX, &buffer[0], &dwSize, WINHTTP_NO_HEADER_INDEX)) {
std::cout << "WinHttpQueryHeaders failed: " << GetLastError() << std::endl;
WinHttpCloseHandle(hRequest);
WinHttpCloseHandle(hConnect);
WinHttpCloseHandle(hSession);
return "";
}
std::string headers(&buffer[0], dwSize);
std::vector<std::string> lines = split(headers, "\r\n");
for (const auto& line : lines) {
std::cout << line << std::endl;
}
}
// 获取响应内容
std::string response;
char buffer[1024];
DWORD bytesRead = 0;
while (WinHttpReadData(hRequest, buffer, sizeof(buffer), &bytesRead) && bytesRead != 0) {
response.append(buffer, bytesRead);
}
// 关闭句柄
WinHttpCloseHandle(hRequest);
WinHttpCloseHandle(hConnect);
WinHttpCloseHandle(hSession);
return response;
}
int main() {
std::string response = AmazonSignIn();
std::cout << "Response: " << response << std::endl;
return 0;
}
```
注意事项:
1. 要设置WINHTTP_FLAG_SECURE来指示使用HTTPS协议。
2. 要获取响应头部信息,可以先调用一次WinHttpQueryHeaders来获取需要的缓冲区大小,然后再分配缓冲区并调用一次WinHttpQueryHeaders来获取头部信息。
3. 要获取响应内容,可以使用WinHttpReadData来逐个读取数据块,并将它们附加到响应字符串中。
4. 这个函数只是一个简单的示例,实际使用时可能需要添加更多的错误处理和异常情况的处理。
阅读全文