cpp
复制代码
#include <Windows.h>
#include <Wininet.h>
#include <stdio.h>
#pragma comment(lib, "wininet.lib")
#define TRANSFER_SIZE 1024
// 定义一个函数来执行HTTP请求
bool PerformHttpRequest() {
// 设置用户代理字符串
LPCSTR lpszAgent = "";
// 初始化WinINet会话
HINTERNET hInternet = InternetOpenA(lpszAgent, INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
if (!hInternet) {
return false; // 如果InternetOpen失败,返回false
}
// 服务器详情
LPCSTR lpszServerName = "192.168.99.111"; // 设置服务器IP地址
INTERNET_PORT nServerPort = INTERNET_DEFAULT_HTTPS_PORT; // HTTPS端口443
LPCSTR lpszUserName = NULL; // 无登录用户名
LPCSTR lpszPassword = NULL; // 无登录密码
DWORD dwConnectFlags = 0;
DWORD dwConnectContext = 0;
// 连接到服务器
HINTERNET hConnect = InternetConnectA(hInternet,
lpszServerName, nServerPort,
lpszUserName, lpszPassword,
INTERNET_SERVICE_HTTP,
dwConnectFlags, dwConnectContext);
if (!hConnect) {
InternetCloseHandle(hInternet);
return false; // 如果连接失败,返回false
}
// 请求详情
LPCSTR lpszVerb = "GET";
LPCSTR lpszObjectName = "/Login";
LPCSTR lpszVersion = NULL; // 默认.
LPCSTR lpszReferrer = NULL; // 没有引用页
LPCSTR* lplpszAcceptTypes = NULL; // Accpet所有类型.
DWORD dwOpenRequestFlags = INTERNET_FLAG_IGNORE_REDIRECT_TO_HTTP |
INTERNET_FLAG_KEEP_CONNECTION |
INTERNET_FLAG_NO_AUTH |
INTERNET_FLAG_NO_COOKIES |
INTERNET_FLAG_NO_UI |
// 设置启用HTTPS
INTERNET_FLAG_SECURE |
INTERNET_FLAG_IGNORE_CERT_CN_INVALID |
INTERNET_FLAG_RELOAD;
DWORD dwOpenRequestContext = 0;
// 初始化请求
HINTERNET hRequest = HttpOpenRequestA(hConnect, lpszVerb, lpszObjectName, lpszVersion,
lpszReferrer, lplpszAcceptTypes,
dwOpenRequestFlags, dwOpenRequestContext);
if (!hRequest) {
InternetCloseHandle(hConnect);
InternetCloseHandle(hInternet);
return false; // 如果初始化请求失败,返回false
}
// 发送请求
BOOL bResult = HttpSendRequestA(hRequest, NULL, 0, NULL, 0);
if (!bResult && GetLastError() == ERROR_INTERNET_INVALID_CA) {
// 处理HTTPS证书无效的情况
DWORD dwFlags;
DWORD dwBuffLen = sizeof(dwFlags);
InternetQueryOptionA(hRequest, INTERNET_OPTION_SECURITY_FLAGS, (LPVOID)&dwFlags, &dwBuffLen);
dwFlags |= SECURITY_FLAG_IGNORE_UNKNOWN_CA;
InternetSetOptionA(hRequest, INTERNET_OPTION_SECURITY_FLAGS, &dwFlags, sizeof(dwFlags));
bResult = HttpSendRequestA(hRequest, NULL, 0, NULL, 0);
}
if (!bResult) {
InternetCloseHandle(hRequest);
InternetCloseHandle(hConnect);
InternetCloseHandle(hInternet);
return false; // 如果发送请求失败,返回false
}
// 获取HTTP响应头信息
char szBuff[TRANSFER_SIZE];
DWORD dwReadSize = TRANSFER_SIZE;
bResult = HttpQueryInfoA(hRequest, HTTP_QUERY_RAW_HEADERS_CRLF, szBuff, &dwReadSize, NULL);
if (!bResult) {
InternetCloseHandle(hRequest);
InternetCloseHandle(hConnect);
InternetCloseHandle(hInternet);
return false; // 如果获取响应头信息失败,返回false
}
szBuff[dwReadSize] = '\0';
printf("%s\n", szBuff);
// HTTP响应的正文
DWORD dwBytesAvailable;
bResult = InternetQueryDataAvailable(hRequest, &dwBytesAvailable, 0, 0);
if (!bResult) {
InternetCloseHandle(hRequest);
InternetCloseHandle(hConnect);
InternetCloseHandle(hInternet);
return false; // 如果查询可用数据失败,返回false
}
if (dwBytesAvailable > TRANSFER_SIZE) {
printf("数据太长 %d /b", GetLastError(), dwBytesAvailable);
InternetCloseHandle(hRequest);
InternetCloseHandle(hConnect);
InternetCloseHandle(hInternet);
return false; // 如果可用数据太长,返回false
}
DWORD dwBytesRead;
bResult = InternetReadFile(hRequest, szBuff, dwBytesAvailable, &dwBytesRead);
if (!bResult) {
InternetCloseHandle(hRequest);
InternetCloseHandle(hConnect);
InternetCloseHandle(hInternet);
return false; // 如果读取数据失败,返回false
}
szBuff[dwBytesRead] = '\0';
printf("%s\n", szBuff);
InternetCloseHandle(hRequest);
InternetCloseHandle(hConnect);
InternetCloseHandle(hInternet);
return true; // 成功执行HTTP请求,返回true
}
int main() {
if (PerformHttpRequest()) {
return 0;
}
else {
return 1;
}
}