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

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

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)


相关推荐

  • Android开发前景(海洋药物开发前景)

    一、Android的产生过程和发展1.概念:Android是一种基于Linux的自由及开放源代码的操作系统,现在的主要适用范围一般是为移动端设备,如一类安卓手机和平板电脑。最初的安卓系统由Google公司和开放手机联盟领导及开发,2007年11月,Google与84家硬件制造商、软件开发商及电信营运商组建开放手机联盟共同研发改良Android系统。第一部Android智能手机发布于2008年10月,随后安卓系统也由手机平台逐渐像像平板电脑以及其他领域扩展。2011年第一季度,Android在全球的市

  • pycharm企业版激活码【2022最新】2022.01.22

    (pycharm企业版激活码)好多小伙伴总是说激活码老是失效,太麻烦,关注/收藏全栈君太难教程,2021永久激活的方法等着你。https://javaforall.cn/100143.htmlIntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,上面是详细链接哦~1TCF2R91JZ-eyJsaWNlbnNlSWQiOi…

  • linux netlink套接字学习资料

    linux netlink套接字学习资料

  • 慢 SQL 问题经验总结

    1、导致慢SQL的原因在遇到慢SQL情况时,不能简单的把原因归结为SQL编写问题(虽然这是最常见的因素),实际上导致慢SQL有很多因素,甚至包括硬件和mysql本身的bug。根据出现的概率从大到小,罗列如下: SQL编写问题 锁 业务实例相互干绕对IO/CPU资源争用 服务器硬件 MYSQLBUG  2、由…

  • activexobject对象不能创建_无法创建office组件对象

    activexobject对象不能创建_无法创建office组件对象JavaScript中ActiveXObject对象是启用并返回Automation对象的引用。使用方法:newObj=newActiveXObject(servername.typename[,location])ActiveXObject对象语法有这些部分:其中newObj是必选项。要赋值为ActiveXObject的变量名。1.servername是必选项。提供该对象的…

    2022年10月15日
  • 十进制小数转换为二进制[通俗易懂]

    十进制小数转换为二进制[通俗易懂]十进制小数转换为二进制十进制小数转换方法十进制小数→→→→→二进制小数方法:“乘2取整”对十进制小数乘2得到的整数部分和小数部分,整数部分既是相应的二进制数码,再用2乘小数部分(之前乘后得到新的小数部分),又得到整数和小数部分.如此不断重复,直到小数部分为0或达到精度要求为止.第一次所得到为最高位,最后一次得到为最低位如:0.25的二进制0.25*2=0.5取整是00.5*…

发表回复

您的电子邮箱地址不会被公开。

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