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)
上一篇 2022年7月11日 下午4:46
下一篇 2022年7月11日 下午5:00


相关推荐

  • 怎么使用pyd 文件

    怎么使用pyd 文件拿到其他人的程序试着运行 显示 nomodulename 去看程序 importMCDAQa 其中 MCDAQ 是有红色波浪线的 说明没有这个模块 google 之后找不到 后来发现是一个后缀为 pyd 的文件 不能运行可能是缺少一些必需的 dll 文件 使用 dependencywa 查找缺失的 dll 文件参考链接 https stackoverflo com questions dependency walker missin

    2026年3月17日
    2
  • Win10 桌面美化

    Win10 桌面美化Win10桌面美化最近发现了几款Win10界面美化的软件,看了看别人家的Win10操作界面,瞬间觉得自己的low了,关键是赏心悦目啊!废话不多说,先看看我原来桌面和美化后的桌面对比图原始桌面美化桌面1.安装RocketDockRocketDock可以提供类似macos的操作系统图标特效,打开安装包进行安装,完毕后启动得到效果如下:可以发现切换效果与mac类似,他默认的主题是C…

    2022年4月25日
    38
  • 天河免费刷流量工具

    天河免费刷流量工具在用,觉得不错,拿出来分享一下,网址: http://u.115.com/file/f2c6dc01b4   提取码:f2c6dc01b4 http://download.csdn.net/source/2676044 需要有CSDN账号 http://ishare.iask.sina.com.cn/f/9876474.html…

    2026年4月18日
    2
  • rust-vmm 学习

    rust-vmm 学习V0.1.0featurebaseknowledge:ArchitectureoftheKernel-basedVirtualMachine(KVM)用rust-vmm打造未来的虚拟化架构KVM内核文档阅读笔记<MasteringKVMVirtualization>:第二章KVM内部原理UsingtheKVMAPI(org)…

    2026年2月14日
    5
  • Oracle之删除数据之后如何恢复的方法总结

    Oracle之删除数据之后如何恢复的方法总结导读:1、delete误删除的解决方法;2、drop误删除的解决方法;3、闪回整个数据库;4、总结以下以oracle数据库为例,介绍关于表中数据删除的解决办法。(不考虑全库备份和利用归档日志

    2022年7月1日
    24
  • Unity3D Invoke 方法的调用

    Unity3D Invoke 方法的调用Invoke()方法是Unity3D的一种委托机制如:Invoke(“SendMsg”,5); 它的意思是:5秒之后调用SendMsg()方法;使用Invoke()方法需要注意3点:1:它应该在脚本的生命周期里的(Start、Update、OnGUI、FixedUpdate、LateUpdate)中被调用;2:Invoke();不能接受含有参数的方

    2022年5月27日
    94

发表回复

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

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