有时候,总想验证一些奇怪的想法,干脆就写下来,以备后查。
想法:获取HTTP(S)协议GET请求返回的信息
1. 获取HTTP协议GET请求返回的信息
#include <Windows.h>
#include <Wininet.h>
#pragma comment(lib, "Wininet.lib")
int main(int argc, char* argv[])
{
BOOL bRet = FALSE;
HINTERNET hSession = NULL, hRequest = NULL;
HANDLE hFile = INVALID_HANDLE_VALUE;
do
{
hSession = InternetOpen(_T("Testing"), INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
if (hSession == NULL) {
printf_s("InternetOpen failed. E%u\n", GetLastError());
break;
}
LPCTSTR lpURL = _T("http://192.168.1.2/download/updatefile.dat");
hRequest = InternetOpenUrl(hSession, lpURL, NULL, 0, INTERNET_FLAG_NO_CACHE_WRITE, NULL);
if (hRequest== NULL) {
printf_s("InternetOpenUrl failed. E%u\n", GetLastError());
break;
}
DWORD dwTimeout = 3000; // 3s
bRet = InternetSetOption(hRequest, INTERNET_OPTION_CONNECT_TIMEOUT, &dwTimeout, sizeof(dwTimeout));
if (!bRet) {
printf("InternetSetOption CONNECT_TIMEOUT Error: %d\n", GetLastError());
break;
}
bRet = InternetSetOption(hRequest, INTERNET_OPTION_SEND_TIMEOUT, &dwTimeout, sizeof(dwTimeout));
if (!bRet) {
printf("InternetSetOption SEND_TIMEOUT Error: %d\n", GetLastError());
break;
}
bRet = InternetSetOption(hRequest, INTERNET_OPTION_RECEIVE_TIMEOUT, &dwTimeout, sizeof(dwTimeout));
if (!bRet) {
printf("InternetSetOption RECEIVE_TIMEOUT Error: %d\n", GetLastError());
break;
}
hFile = CreateFile(_T("d:\\updatefile.dat"), GENERIC_WRITE, 0, 0, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
if (hFile == INVALID_HANDLE_VALUE) {
printf_s("CreateFile failed. E%u\n", GetLastError());
break;
}
while (TRUE) {
char buffer[100];
memset(buffer, 0, 100);
DWORD byteread = 0;
DWORD written = 0;
bRet = InternetReadFile(hUrl, buffer, sizeof(buffer), &byteread);
if (!bRet) {
printf("InternetReadFile Error: %d\n", GetLastError());
break;
}
if (byteread == 0) {
printf("byteread = 0\n");
break;
}
printf("byteread = %u\n", byteread);
bRet = WriteFile(hFile, buffer, byteread, &written, NULL);
if (!bRet) {
printf("WriteFile Error: %d\n", GetLastError());
break;
}
if (written == 0) {
printf("written = 0\n");
break;
}
printf("written = %u\n", written);
}
} while (0);
CloseHandle(hFile);
InternetCloseHandle(hRequest);
InternetCloseHandle(hSession);
return 0;
}
2. 获取HTTPS协议GET请求返回的信息
#include <Windows.h>
#include <Wininet.h>
#pragma comment(lib, "Wininet.lib")
int main(int argc, char* argv[])
{
BOOL bRet = FALSE;
HINTERNET hSession = NULL, hConnect = NULL, hRequest = NULL;
HANDLE hFile = INVALID_HANDLE_VALUE;
do
{
hSession = InternetOpen(_T("Testing"), INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
if (hSession == NULL) {
printf_s("InternetOpen failed. E%u\n", GetLastError());
break;
}
hConnect = InternetConnect(hSession, _T("192.168.18.162"), 443, _T(""), _T(""), INTERNET_SERVICE_HTTP, 0, 0);
if (hConnect == NULL) {
printf_s("InternetConnect failed. E%u\n", GetLastError());
break;
}
hRequest = HttpOpenRequest(hConnect, _T("GET"), _T("/public/agent/windows/version.ini"), _T("HTTP/1.1"), NULL, 0,
INTERNET_FLAG_DONT_CACHE | INTERNET_FLAG_SECURE, 0);
if (hRequest == NULL) {
printf_s("HttpOpenRequest failed. E%u\n", GetLastError());
break;
}
DWORD dwFlags = 0;
DWORD dwBuffLen = sizeof(dwFlags);
bRet = InternetQueryOption(hRequest, INTERNET_OPTION_SECURITY_FLAGS, (LPVOID)&dwFlags, &dwBuffLen);
if (!bRet) {
printf("InternetQueryOption Error: %d\n", GetLastError());
break;
}
dwFlags |= SECURITY_SET_MASK;
bRet = InternetSetOption(hRequest, INTERNET_OPTION_SECURITY_FLAGS, &dwFlags, sizeof(dwFlags));
if (!bRet) {
printf("InternetSetOption Error: %d\n", GetLastError());
break;
}
DWORD dwTimeout = 3000; // 3s
bRet = InternetSetOption(hRequest, INTERNET_OPTION_CONNECT_TIMEOUT, &dwTimeout, sizeof(dwTimeout));
if (!bRet) {
printf("InternetSetOption CONNECT_TIMEOUT Error: %d\n", GetLastError());
break;
}
bRet = InternetSetOption(hRequest, INTERNET_OPTION_SEND_TIMEOUT, &dwTimeout, sizeof(dwTimeout));
if (!bRet) {
printf("InternetSetOption SEND_TIMEOUT Error: %d\n", GetLastError());
break;
}
bRet = InternetSetOption(hRequest, INTERNET_OPTION_RECEIVE_TIMEOUT, &dwTimeout, sizeof(dwTimeout));
if (!bRet) {
printf("InternetSetOption RECEIVE_TIMEOUT Error: %d\n", GetLastError());
break;
}
bRet = HttpAddRequestHeaders(hRequest, _T("Content-Type: application/json;charset=utf-8"), -1, HTTP_ADDREQ_FLAG_ADD);
if (!bRet) {
printf_s("HttpAddRequestHeaders Content-Type failed. E%u\n", GetLastError());
break;
}
bRet = HttpAddRequestHeaders(hRequest, _T("Accept: application/json;charset=utf-8"), -1, HTTP_ADDREQ_FLAG_ADD);
if (!bRet) {
printf_s("HttpAddRequestHeaders Accept failed. E%u\n", GetLastError());
break;
}
bRet = HttpSendRequest(hRequest, NULL, 0, 0, 0);
if (!bRet) {
printf_s("HttpSendRequest failed. E%u\n", GetLastError());
break;
}
hFile = CreateFile(_T("d:\\version.ini"), GENERIC_WRITE, 0, 0, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
if (hFile == INVALID_HANDLE_VALUE) {
printf_s("CreateFile failed. E%u\n", GetLastError());
break;
}
while (TRUE) {
char buffer[100];
memset(buffer, 0, 100);
DWORD byteread = 0;
DWORD written = 0;
bRet = InternetReadFile(hUrl, buffer, sizeof(buffer), &byteread);
if (!bRet) {
printf("InternetReadFile Error: %d\n", GetLastError());
break;
}
if (byteread == 0) {
printf("byteread = 0\n");
break;
}
printf("byteread = %u\n", byteread);
bRet = WriteFile(hFile, buffer, byteread, &written, NULL);
if (!bRet) {
printf("WriteFile Error: %d\n", GetLastError());
break;
}
if (written == 0) {
printf("written = 0\n");
break;
}
printf("written = %u\n", written);
}
} while (0);
CloseHandle(hFile);
InternetCloseHandle(hRequest);
InternetCloseHandle(hConnect);
InternetCloseHandle(hSession);
return 0;
}
这篇博客记录了验证HTTP和HTTPS协议GET请求返回信息的过程,包括如何获取HTTP及HTTPS协议的GET请求数据。

3195

被折叠的 条评论
为什么被折叠?



