WinHttp 类封装「建议收藏」

WinHttp 类封装「建议收藏」头文件#pragmaonce#include<Windows.h>#include<stdio.h>#include<string>usingstd::string;usingstd::wstring;boollibHttp_Get(wstringwszServerName,WORDnServerPort,wstringw…

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺

头文件

#pragma once
#include <Windows.h>
#include <stdio.h>
#include <string>
using std::string;
using std::wstring;


bool libHttp_Get(wstring wszServerName,WORD nServerPort, wstring wszObjectName, string &result);

bool libHttp_Post(wstring wszServerName,WORD nServerPort, wstring wszObjectName,string &postData, string &result);

bool libHttp_DownloadFile(const wchar_t *wszURL, const wchar_t *wszFileSavePath);

源文件

#include "StdAfx.h"
#include "libHttp.h"

#include <iostream>
#include <windows.h>
#include <winhttp.h> 
#pragma comment(lib,"winhttp.lib")

bool libHttp_Get(wstring wszServerName,WORD nServerPort, wstring wszObjectName, string &result)
{ 
   
	HINTERNET  hSession = NULL;
	HINTERNET  hConnect = NULL;
	HINTERNET  hRequest = NULL;
	BOOL  bResults = FALSE;
		
	hSession = WinHttpOpen(L"Mozilla/5.0 (Windows NT 6.1; WOW64) Chrome/45.0.2454.101",
						   WINHTTP_ACCESS_TYPE_NO_PROXY,
						   NULL,
						   NULL,
						   0);
	hConnect = WinHttpConnect(hSession, wszServerName.c_str(), nServerPort, 0);	// WINHTTP_FLAG_ASYNC 指示WinHTTP API将异步执行
	hRequest = WinHttpOpenRequest(hConnect, L"GET", wszObjectName.c_str(), L"HTTP/1.1", WINHTTP_NO_REFERER,WINHTTP_DEFAULT_ACCEPT_TYPES, 0);
	bResults = WinHttpSendRequest(hRequest, WINHTTP_NO_ADDITIONAL_HEADERS, 0, WINHTTP_NO_REQUEST_DATA, 0, 0, 0 );
	bResults = WinHttpReceiveResponse(hRequest, NULL);

	if(!bResults)
	{ 
   
		if (hRequest) WinHttpCloseHandle(hRequest);
		if (hConnect) WinHttpCloseHandle(hConnect);
		if (hSession) WinHttpCloseHandle(hSession);
		return false;
	}

	DWORD dwNumberOfBytesToRead = 0;
	DWORD dwNumberOfBytesRead = 0;;
	do 
	{ 
   

		dwNumberOfBytesToRead = 0;
		WinHttpQueryDataAvailable(hRequest, &dwNumberOfBytesToRead);
		if(dwNumberOfBytesToRead <= 0) break;


		char *pbufRecv = new char[dwNumberOfBytesToRead + 1];
		ZeroMemory(pbufRecv, dwNumberOfBytesToRead + 1);
		WinHttpReadData( hRequest, (LPVOID)pbufRecv, dwNumberOfBytesToRead, &dwNumberOfBytesRead);

		result += string(pbufRecv);
				
		delete [] pbufRecv;
		pbufRecv = NULL;

	} while (dwNumberOfBytesToRead > 0);

	if (hRequest) WinHttpCloseHandle(hRequest);
	if (hConnect) WinHttpCloseHandle(hConnect);
	if (hSession) WinHttpCloseHandle(hSession);

	return true;
}

bool libHttp_Post(wstring wszServerName,WORD nServerPort, wstring wszObjectName,string &postData, string &result)
{ 
   
	HINTERNET  hSession = NULL;
	HINTERNET  hConnect = NULL;
	HINTERNET  hRequest = NULL;
	BOOL  bResults = FALSE;

	hSession = WinHttpOpen(L"Mozilla/5.0 (Windows NT 6.1; WOW64) Chrome/45.0.2454.101",
		WINHTTP_ACCESS_TYPE_NO_PROXY,
		NULL,
		NULL,
		0);
	hConnect = WinHttpConnect(hSession, wszServerName.c_str(), nServerPort, 0);	// WINHTTP_FLAG_ASYNC 指示WinHTTP API将异步执行
	hRequest = WinHttpOpenRequest(hConnect, L"POST", wszObjectName.c_str(), L"HTTP/1.1", WINHTTP_NO_REFERER,WINHTTP_DEFAULT_ACCEPT_TYPES, 0);
	//bResults = WinHttpSendRequest(hRequest, WINHTTP_NO_ADDITIONAL_HEADERS, 0, WINHTTP_NO_REQUEST_DATA, 0, 0, 0 );
	bResults = WinHttpSendRequest(hRequest, 0, 0, (void*)(postData.c_str()), postData.length(), postData.length(), 0);
	bResults = WinHttpReceiveResponse(hRequest, NULL);

	if(!bResults)
	{ 
   
		if (hRequest) WinHttpCloseHandle(hRequest);
		if (hConnect) WinHttpCloseHandle(hConnect);
		if (hSession) WinHttpCloseHandle(hSession);
		return false;
	}

	DWORD dwNumberOfBytesToRead = 0;
	DWORD dwNumberOfBytesRead = 0;;
	do 
	{ 
   

		dwNumberOfBytesToRead = 0;
		WinHttpQueryDataAvailable(hRequest, &dwNumberOfBytesToRead);
		if(dwNumberOfBytesToRead <= 0) break;


		char *pbufRecv = new char[dwNumberOfBytesToRead + 1];
		ZeroMemory(pbufRecv, dwNumberOfBytesToRead + 1);
		WinHttpReadData( hRequest, (LPVOID)pbufRecv, dwNumberOfBytesToRead, &dwNumberOfBytesRead);

		result += string(pbufRecv);

		delete [] pbufRecv;
		pbufRecv = NULL;

	} while (dwNumberOfBytesToRead > 0);

	if (hRequest) WinHttpCloseHandle(hRequest);
	if (hConnect) WinHttpCloseHandle(hConnect);
	if (hSession) WinHttpCloseHandle(hSession);

	return true;
}

typedef struct _URL_INFO
{ 
   
	WCHAR szScheme[512];
	WCHAR szHostName[512];
	WCHAR szUserName[512];
	WCHAR szPassword[512];
	WCHAR szUrlPath[512];
	WCHAR szExtraInfo[512];
}URL_INFO, *PURL_INFO;

bool libHttp_DownloadFile(const wchar_t *wszURL, const wchar_t *wszFileSavePath)
{ 
   
    URL_INFO url_info = { 
    0 };
    URL_COMPONENTSW lpUrlComponents = { 
    0 };
    lpUrlComponents.dwStructSize = sizeof(lpUrlComponents);
    lpUrlComponents.lpszExtraInfo = url_info.szExtraInfo;
    lpUrlComponents.lpszHostName = url_info.szHostName;
    lpUrlComponents.lpszPassword = url_info.szPassword;
    lpUrlComponents.lpszScheme = url_info.szScheme;
    lpUrlComponents.lpszUrlPath = url_info.szUrlPath;
    lpUrlComponents.lpszUserName = url_info.szUserName;

    lpUrlComponents.dwExtraInfoLength = 
	lpUrlComponents.dwHostNameLength = 
	lpUrlComponents.dwPasswordLength = 
	lpUrlComponents.dwSchemeLength = 
	lpUrlComponents.dwUrlPathLength = 
	lpUrlComponents.dwUserNameLength = 512;

    WinHttpCrackUrl(wszURL, 0, ICU_ESCAPE, &lpUrlComponents);

    HINTERNET hSession = WinHttpOpen(NULL, WINHTTP_ACCESS_TYPE_NO_PROXY, NULL, NULL, 0);
    DWORD dwReadBytes, dwSizeDW = sizeof(dwSizeDW), dwContentSize, dwIndex = 0;

    HINTERNET hConnect = WinHttpConnect(hSession, lpUrlComponents.lpszHostName, lpUrlComponents.nPort, 0);

    HINTERNET hRequest = WinHttpOpenRequest(hConnect, L"HEAD", lpUrlComponents.lpszUrlPath, L"HTTP/1.1", WINHTTP_NO_REFERER, WINHTTP_DEFAULT_ACCEPT_TYPES, WINHTTP_FLAG_REFRESH);
    WinHttpSendRequest(hRequest, WINHTTP_NO_ADDITIONAL_HEADERS, 0, WINHTTP_NO_REQUEST_DATA, 0, 0, 0);
    WinHttpReceiveResponse(hRequest, 0);
    WinHttpQueryHeaders(hRequest, WINHTTP_QUERY_CONTENT_LENGTH | WINHTTP_QUERY_FLAG_NUMBER, NULL, &dwContentSize, &dwSizeDW, &dwIndex);
    WinHttpCloseHandle(hRequest);

    // 创建一个请求,获取数据
    hRequest = WinHttpOpenRequest(hConnect, L"GET", lpUrlComponents.lpszUrlPath, L"HTTP/1.1", WINHTTP_NO_REFERER, WINHTTP_DEFAULT_ACCEPT_TYPES, WINHTTP_FLAG_REFRESH);
    WinHttpSendRequest(hRequest, WINHTTP_NO_ADDITIONAL_HEADERS, 0, WINHTTP_NO_REQUEST_DATA, 0, 0, 0);
    WinHttpReceiveResponse(hRequest, 0);

    // 分段回调显示进度
    DWORD BUF_LEN = 1024, ReadedLen = 0;
    BYTE *pBuffer = NULL;
    pBuffer = new BYTE[BUF_LEN];

	bool bRet = false;

    HANDLE hFile = CreateFileW(wszFileSavePath, GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
    while (dwContentSize > ReadedLen)
    { 
   
        ZeroMemory(pBuffer, BUF_LEN);
        WinHttpReadData(hRequest, pBuffer, BUF_LEN, &dwReadBytes);
        ReadedLen += dwReadBytes;
        WriteFile(hFile, pBuffer, dwReadBytes, &dwReadBytes, NULL);
		bRet = true;
    }

    CloseHandle(hFile);
    delete pBuffer;


    WinHttpCloseHandle(hRequest);
    WinHttpCloseHandle(hConnect);
    WinHttpCloseHandle(hSession);

	return bRet;
}
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/157631.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • SQL服务器操作系统和SQL版本的选择

    SQL服务器操作系统和SQL版本的选择

    2021年8月2日
    49
  • [渝粤教育] 西安工业大学 汉语文字学 参考 资料[通俗易懂]

    教育-汉语文字学-章节资料考试资料-西安工业大学【】请简要说明黄帝时期与半坡遗址之间的关系。第一章汉字的起源章测试题1、【多选题】请选从下列选项中找出“八卦”的用途()A、卜筮B、族徽C、计数D、装饰参考资料【】2、【多选题】请找出《周礼》中有关史官系统的名称()A、大史B、小史C、内史D、外史参考资料【】3、【多选题】请从下面选项中选出黄帝时期的特点。()A、战争B、纺织C、宫室D、穴居参考资料【】4、【判断题】文

    2022年4月7日
    112
  • Pytest+Allure安装

    Pytest+Allure安装文章目录1.Pytest环境安装2.Window环境下Allure安装3.Linux环境下Allure安装1.Pytest环境安装#在线安装pip3installpytestpip3installpytest-rerunfailurespip3installpytest-htmlpip3installpytest-repeatpip3installpytest-assumepip3installallure-pytest#离线安装-先在线下载pip3

    2022年7月26日
    3
  • 阿里云国外服务器购买_阿里云购买服务器流程

    阿里云国外服务器购买_阿里云购买服务器流程目录前言购买实际全过程1、选择服务器基础配置2、网络和安全组3、系统配置(选填)4、分组设置(选填)5、确认订单6、付钱完,前往控制台控制台基本操作1、获取服务器公网IP2、登录服务器3、正常使用,放行端口前言阿里云官网:https://www.aliyun.com云服务器ECS购买链接:https://ecs-buy.aliyun.com/wizard#/prepay/ap-northeast-1共享型可用,最低日本服30.2/月(不含带宽)1Mbps要55.2元流量付费0.6元/

    2022年9月26日
    0
  • pycahrm激活码【在线破解激活】

    pycahrm激活码【在线破解激活】,https://javaforall.net/100143.html。详细ieda激活码不妨到全栈程序员必看教程网一起来了解一下吧!

    2022年3月17日
    42
  • pycharm 删除项目_pycharm怎么删除python文件

    pycharm 删除项目_pycharm怎么删除python文件按照网上查找到的方法基本上都是:删除本地项目,重新打开Pycharm。这样做在打算彻底删除时是有效的,但是如果只是打算从pycharm中删除,而不是删除本地项目就出现问题。而且重新建立一个同名文件夹,pycharm中又会出现这个项目。正确的方式应该是:1.在用户\XXX\.Pycharm40\config\options目录下,查找到recentProjectDirectori

    2022年8月25日
    3

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号