|
分享源码
界面截图: |
- |
是否带模块: |
- |
备注说明: |
- |
本帖最后由 z13228604287 于 2024-5-9 21:12 编辑
#include <windows.h>
#include <winhttp.h>
#include <stdio.h>
#include <iostream>
#include <tchar.h>
#include <io.h>
#include <fcntl.h>
#include <string>
#pragma comment(lib, "winhttp.lib")
// 将 UTF-8 字符串转换为 GB2312 字符串
std::string UTF8ToGB2312(const std::string& utf8Str) {
int wideLength = MultiByteToWideChar(CP_UTF8, 0, utf8Str.c_str(), -1, NULL, 0);
if (wideLength == 0) {
return std::string();
}
std::wstring wideStr(wideLength, L'\0');
MultiByteToWideChar(CP_UTF8, 0, utf8Str.c_str(), -1, &wideStr[0], wideLength);
int gb2312Length = WideCharToMultiByte(CP_ACP, 0, wideStr.c_str(), -1, NULL, 0, NULL, NULL);
if (gb2312Length == 0) {
return std::string();
}
std::string gb2312Str(gb2312Length, '\0');
WideCharToMultiByte(CP_ACP, 0, wideStr.c_str(), -1, &gb2312Str[0], gb2312Length, NULL, NULL);
return gb2312Str;
}
// 将 GB2312 字符串转换为 UTF-8 字符串
std::string GB2312ToUTF8(const std::string& gb2312Str) {
int wideLength = MultiByteToWideChar(CP_ACP, 0, gb2312Str.c_str(), -1, NULL, 0);
if (wideLength == 0) {
return std::string();
}
std::wstring wideStr(wideLength, L'\0');
MultiByteToWideChar(CP_ACP, 0, gb2312Str.c_str(), -1, &wideStr[0], wideLength);
int utf8Length = WideCharToMultiByte(CP_UTF8, 0, wideStr.c_str(), -1, NULL, 0, NULL, NULL);
if (utf8Length == 0) {
return std::string();
}
std::string utf8Str(utf8Length, '\0');
WideCharToMultiByte(CP_UTF8, 0, wideStr.c_str(), -1, &utf8Str[0], utf8Length, NULL, NULL);
return utf8Str;
}
int main()
{
if (WinHttpCheckPlatform()) {
// 设置用户名和密码
LPCWSTR username = L"****\\****";
LPCWSTR password = L"****";
URL_COMPONENTSW URL_Structure = { 0 };
URL_Structure.dwStructSize = sizeof(URL_Structure);
URL_Structure.dwSchemeLength = -1;
URL_Structure.dwHostNameLength = -1;
URL_Structure.dwUrlPathLength = -1;
URL_Structure.dwExtraInfoLength = -1;
if (!WinHttpCrackUrl(L"http://********", -1, 0, &URL_Structure))
{
std::wcerr << L"错误:WinHttpOpen 失败" << std::endl;
system("pause");
return 1;
}
//会话句柄 不使用代理
HINTERNET hSession = WinHttpOpen(L"User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.87 Safari/537.36", WINHTTP_ACCESS_TYPE_NO_PROXY, WINHTTP_NO_PROXY_NAME, WINHTTP_NO_PROXY_BYPASS, 0);
if (hSession == NULL) {
return false;
}
std::wstring wstrHostName = URL_Structure.lpszHostName;
wstrHostName = wstrHostName.substr(0, URL_Structure.dwHostNameLength);
//连接句柄
HINTERNET hConnect = WinHttpConnect(hSession, wstrHostName.c_str(), URL_Structure.nPort, 0);
if (hConnect == NULL) {
WinHttpCloseHandle(hSession); return false; //创建WinHttp连接失败!
}
//请求句柄
HINTERNET hRequest = WinHttpOpenRequest(hConnect, L"GET", URL_Structure.lpszUrlPath, L"", WINHTTP_NO_REFERER, WINHTTP_DEFAULT_ACCEPT_TYPES, 0);
if (hRequest == NULL) {
WinHttpCloseHandle(hSession); WinHttpCloseHandle(hConnect); return false; //创建WinHttp请求失败!
}
// 设置凭据指定
if (!WinHttpSetCredentials(hRequest, WINHTTP_AUTH_TARGET_SERVER,
WINHTTP_AUTH_SCHEME_NTLM,
username, password, NULL)) {
std::cerr << "Failed to set credentials. Error: " << GetLastError() << std::endl;
WinHttpCloseHandle(hRequest);
WinHttpCloseHandle(hConnect);
WinHttpCloseHandle(hSession);
return 1;
}
//// 设置 Negotiate 认证系统
//DWORD dwEnableNegotiate = WINHTTP_AUTOLOGON_SECURITY_LEVEL_HIGH; // 使用 Negotiate 认证
//if (!WinHttpSetOption(hRequest, WINHTTP_OPTION_AUTOLOGON_POLICY, &dwEnableNegotiate, sizeof(dwEnableNegotiate))) {
// std::cerr << "Failed to set Negotiate authentication. Error: " << GetLastError() << std::endl;
// WinHttpCloseHandle(hRequest);
// WinHttpCloseHandle(hConnect);
// WinHttpCloseHandle(hSession);
// return 1;
//}
PCWSTR headers = L"Host: dptwhrap1\r\n"
L"Connection: keep-alive\r\n"
L"Cache-Control: max-age=0\r\n"
L"Upgrade-Insecure-Requests: 1\r\n"
L"User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.6045.160 Safari/537.36\r\n"
L"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7\r\n"
L"Accept-Encoding: gzip, deflate\r\n"
L"Accept-Language: zh-CN,zh;q=0.9\r\n";
// 添加请求头
if (!WinHttpAddRequestHeaders(hRequest, headers, -1, WINHTTP_ADDREQ_FLAG_ADD)) {
DWORD dwError = GetLastError();
std::wcerr << L"Error: WinHttpAddRequestHeaders failed: " << dwError << std::endl;
system("pause");
WinHttpCloseHandle(hRequest);
WinHttpCloseHandle(hConnect);
WinHttpCloseHandle(hSession);
return 1;
}
// 发送请求
if (!WinHttpSendRequest(hRequest, WINHTTP_NO_ADDITIONAL_HEADERS, 0, 0, 0, 0, 0)) {
DWORD dwError = GetLastError();
std::wcerr << L"Error: WinHttpSendRequest failed :" << dwError << std::endl;
system("pause");
WinHttpCloseHandle(hRequest);
WinHttpCloseHandle(hConnect);
WinHttpCloseHandle(hSession);
return 1;
}
// 接收响应
if (!WinHttpReceiveResponse(hRequest, NULL)) {
std::wcerr << L"错误:WinHttpReceiveResponse 失败" << std::endl;
system("pause");
WinHttpCloseHandle(hRequest);
WinHttpCloseHandle(hConnect);
WinHttpCloseHandle(hSession);
return 1;
}
// 读取响应内容
DWORD dwSize = 0;
DWORD dwDownloaded = 0;
LPBYTE pszOutBuffer;
BOOL bResults = FALSE;
std::string responseContent;
// 读取响应内容
do {
// cha询可用数据大小
if (!WinHttpQueryDataAvailable(hRequest, &dwSize)) {
std::wcerr << L"错误:WinHttpQueryDataAvailable 失败" << std::endl;
system("pause");
break;
}
// 如果没有可用数据,则跳出循环
if (dwSize == 0) {
break;
}
// 分配空间来存储响应内容
pszOutBuffer = new BYTE[dwSize];
if (!pszOutBuffer) {
std::wcerr << L"错误:内存不足" << std::endl;
system("pause");
break;
}
// 读取响应内容
if (!WinHttpReadData(hRequest, (LPVOID)pszOutBuffer, dwSize, &dwDownloaded)) {
std::wcerr << L"sss:WinHttpReadData 失败" << std::endl;
system("pause");
delete[] pszOutBuffer;
break;
}
// 将读取的内容添加到字符串中
responseContent.append(reinterpret_cast<char*>(pszOutBuffer), dwDownloaded);
// 释放内存
delete[] pszOutBuffer;
} while (dwSize > 0);
std::cout << L"data:" << std::endl<< UTF8ToGB2312(responseContent)<< std::endl;
// 关闭句柄
WinHttpCloseHandle(hRequest);
WinHttpCloseHandle(hConnect);
WinHttpCloseHandle(hSession);
system("pause");
}
return 0;
}
|
评分
-
查看全部评分
|