验证:获取HTTP(S)协议GET请求返回的信息

这篇博客记录了验证HTTP和HTTPS协议GET请求返回信息的过程,包括如何获取HTTP及HTTPS协议的GET请求数据。

有时候,总想验证一些奇怪的想法,干脆就写下来,以备后查。

想法:获取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;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值