winhttp 访问https_WinHttp支持HTTPS下载「建议收藏」

winhttp 访问https_WinHttp支持HTTPS下载「建议收藏」WinHttp支持HTTPS下载#include”windows.h”#include”winhttp.h”#include”wchar.h”#pragmacomment(lib,”Winhttp.lib”)//SSL(SecureSocketsLayer)example//compileforconsolevoidmain(){HINTERNEThOpen=0;H…

大家好,又见面了,我是你们的朋友全栈君。

WinHttp支持HTTPS下载

#include “windows.h”

#include “winhttp.h”

#include “wchar.h”

#pragma comment(lib,”Winhttp.lib”)

// SSL (Secure Sockets Layer) example

// compile for console

void main()

{

HINTERNET hOpen = 0;

HINTERNET hConnect = 0;

HINTERNET hRequest = 0;

IStream *stream = NULL;

HRESULT hr;

while (1)

{

hOpen = WinHttpOpen(L”Aurora Console App”, WINHTTP_ACCESS_TYPE_DEFAULT_PROXY,

WINHTTP_NO_PROXY_NAME, WINHTTP_NO_PROXY_BYPASS, 0);

if (!hOpen) {

wprintf(L”WinHttpOpen failed (0x%.8X)\n”, GetLastError());

break;

}

hConnect = WinHttpConnect(hOpen, L”raw.github.com”, INTERNET_DEFAULT_HTTPS_PORT, 0);

if (!hConnect) {

wprintf(L”WinHttpConnect failed (0x%.8X)\n”, GetLastError());

break;

}

LPCWSTR types[2];

types[0] = L”text/html”;

types[1] = 0;

// use flag WINHTTP_FLAG_SECURE to initiate SSL

hRequest = WinHttpOpenRequest(hConnect, L”GET”, L”zpfzzz/test/master/README.md”,

NULL, WINHTTP_NO_REFERER, &types[0], WINHTTP_FLAG_SECURE);

if (!hRequest)

{

wprintf(L”WinHttpOpenRequest failed (0x%.8X)\n”, GetLastError());

break;

}

if (!WinHttpSendRequest(hRequest, WINHTTP_NO_ADDITIONAL_HEADERS, 0, WINHTTP_NO_REQUEST_DATA, 0, 0, 0))

{

wprintf(L”WinHttpSendRequest failed (0x%.8X)\n”, GetLastError());

break;

}

if (!WinHttpReceiveResponse(hRequest, 0))

{

wprintf(L”WinHttpReceiveResponse failed (0x%.8X)\n”, GetLastError());

break;

}

// query remote file size, set haveContentLength on success and dwContentLength to the length

wchar_t szContentLength[32];

DWORD cch = 64;

DWORD dwHeaderIndex = WINHTTP_NO_HEADER_INDEX;

BOOL haveContentLength = WinHttpQueryHeaders(hRequest, WINHTTP_QUERY_CONTENT_LENGTH, NULL,

&szContentLength, &cch, &dwHeaderIndex);

DWORD dwContentLength;

if (haveContentLength) dwContentLength = _wtoi(szContentLength);

// read the response into memory stream

hr = CreateStreamOnHGlobal(0, true, &stream);

if (hr) {

wprintf(L”CreateStreamOnHGlobal failed (0x%.8X)\n”, hr);

break;

}

// allocate buffer for streaming received data

char *p = new char[4096];

if (!p)

{

wprintf(L”failed to allocate buffer\n”);

break;

}

// to receive all data, we need to enter a loop

DWORD dwReceivedTotal = 0;

while (WinHttpQueryDataAvailable(hRequest, &cch) && cch)

{

if (cch > 4096) cch = 4096;

dwReceivedTotal += cch;

// display number of received bytes

if (haveContentLength)

{

wprintf(L”received %d of %d (%d%%)%c”, dwReceivedTotal, dwContentLength,

dwReceivedTotal*100/dwContentLength, 13);

}

else {

wprintf(L”received %d (unknown length)%c”, dwReceivedTotal, 10);

}

WinHttpReadData(hRequest, p, cch, &cch);

// write into stream

hr = stream->Write(p, cch, NULL);

if (hr)

{

wprintf(L”failed to write data to stream (0x%.8X)\n”, hr);

}

}

delete [] p;

wprintf(L”\n\nreceived all data.\n”);

// terminate the sream with a NULL

p = NULL;

stream->Write(&p, 1, NULL);

// get pointer to stream bytes

wprintf(L”getting HGLOBAL from stream…\n”);

HGLOBAL hgl;

hr = GetHGlobalFromStream(stream, &hgl);

if (hr) {

wprintf(L”GetHGlobalFromStream failed (0x%.8X)\n”, hr);

break;

}

wprintf(L”locking memory…\n”);

p = (char*)GlobalLock(hgl);

if (!p)

{

wprintf(L”GlobalLock failed (0x%.8X)\n”, GetLastError());

break;

}

wprintf(L”displaying received data…\n”);

// terminate the string at 1024 bytes (MessageBox lag)

//if (dwReceivedTotal > 1024) dwReceivedTotal = 1024;

//*p[dwReceivedTotal] = 0;

MessageBoxA(0, p, “”, 0);

GlobalUnlock(hgl);

break;

}

// delete stream and close handles

if (stream) stream->Release();

if (hRequest) WinHttpCloseHandle(hRequest);

if (hConnect) WinHttpCloseHandle(hConnect);

if (hOpen) WinHttpCloseHandle(hOpen);

system(“pause”);

}

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

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

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

相关推荐

  • 什么是平均数,中位数,众数,方差_平均数标准差

    什么是平均数,中位数,众数,方差_平均数标准差Python代码实现第一步:添加数据到列表defnum(a):iffloat(a)==int(a):returnint(a)else:returnfloat(a)#添加数据li=[]print("请逐条添加数据!(若退出请输入0000)")whileTrue:print("请输入:&q

    2022年9月17日
  • C++基本概念_c语言 c++区别

    C++基本概念_c语言 c++区别1.    面向对象的程序设计思想是什么?答:把数据结构和对数据结构进行操作的方法封装形成一个个的对象。 2.    什么是类?答:把一些具有共性的对象归类后形成一个集合,也就是所谓的类。 3.    对象都具有的二方面特征是什么?分别是什么含义?答:对象都具有的特征是:静态特征和动态特征。静态特征是指能描述对象的一些属性;动态特征是指对象表现出来的行为;

    2022年10月3日
  • siamfc++代码_asic芯片设计

    siamfc++代码_asic芯片设计SiamFC论文解读及代码实现摘要:传统上,任意目标跟踪的问题是通过专门在线学习目标外观的模型来解决的,使用视频本身作为唯一的训练数据。尽管这些方法取得了成功,但他们的纯在线方法固有地限制了他们可以学习的模型的丰富性。最近,有人试图利用深层卷积网络的表达能力。然而,当要跟踪的目标事先未知时,需要在线执行随机梯度下降以适应网络的权重,这严重影响了系统的速度。在本文中,我们在ILSVRC15数据集上为视频中的目标检测配备了一个基本的跟踪算法和一个新的端到端训练的孪生神经网络。我们的跟踪器以超过实时的帧速率运

    2022年10月1日
  • 恐怖的vs2005 sp1安装过程.

    恐怖的vs2005 sp1安装过程.今天刚把这个400多M的sp1从公司拷回来,算计着这样可以省掉不少下载时间.可是,可是,竟然没有想到,安装过程是如此的恐怖.首先,我的C盘空间明显的感到害羞,只有1.5G的剩余空间了,赶

    2022年7月3日
  • linux 开发板 ldd,Linux ldd命令

    linux 开发板 ldd,Linux ldd命令Linux下的lld命令的主要作用:用来查看程式运行所需的共享库,常用来解决程式因缺少某个库文件而不能运行的一些问题。ldd是list,dynamic,dependencies的缩写,列出动态库依赖关系。可以用ldd–help或者manldd来看其用法。可以用which命令找到ldd的位置:$whichldd/usr/bin/ldd使用ldd,往往在制作自己的发行版时经常需要判断某条命…

    2022年6月11日
  • 阿拉伯媒体爆百度被黑最新进展 疑似一伊朗少年所为

    阿拉伯媒体爆百度被黑最新进展 疑似一伊朗少年所为

    2021年8月4日

发表回复

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

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