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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 谷粒商城笔记-基础篇-2(2/4)

    谷粒商城笔记-基础篇-2(2/4)1.整体介绍1)安装vagrant2)安装Centos7$vagrantinitcentos/7A`Vagrantfile`hasbeenplacedinthisdirectory.Youarenowreadyto`vagrantup`yourfirstvirtualenvironment!PleasereadthecommentsintheVagrantfileaswellasdocumentationon`vagrantup

    2022年6月5日
    33
  • 【剑指offer】删除字符也出现在一个字符串

    【剑指offer】删除字符也出现在一个字符串

    2022年1月3日
    43
  • 代理模式proxy_反向代理是什么

    代理模式proxy_反向代理是什么代理模式 Proxy动机模式定义实例结构要点总结笔记动机在面向对象系统中,由于某种原因(比如对象创建的开销很大,或者某些操作需要安全控制,或者需要进程额外的访问等),直接访问会给使用者,或者系统结构带来很多麻烦.如何在不是去透明操作对象的同时来管理/控制这些对象特有的复杂性?增加一层间接曾是软件开发中常见的解决方式模式定义为其他对象提供一种代理以控制(隔离,使用接口)对这个对象的访问实例朴素客户端要去使用process 但是process周围需要做很多事情class ISubject{p

    2022年8月9日
    5
  • 小众网java下载_jar应用下载

    小众网java下载_jar应用下载全版本都有包含:windows、Linux、源码等,根据名称进行挑选、下载!网址:https://jbossas.jboss.org/downloads/如何解决了您的问题,欢迎关注我!还希望来JAVAWEB开发交流群:958923746,有问题欢迎共享,共同提升!…

    2022年10月3日
    4
  • vue3.0中关闭eslint(全部不检测)[通俗易懂]

    vue3.0中关闭eslint(全部不检测)[通俗易懂]前言:在项目中eslint检查是一个很棒的工具,但是在非正式场合,或者某些情况下,他会给我们带来很多不方便,这里分享下如何在新建项目后把他的eslint检查先关掉(正式项目不推荐关闭,因为良好的习惯是从每一行代码的规范开始)方法:打开我们的.eslintrc.js文件,把’@vue/standard’注释,然后把服务重启,重启,重启,重要的事情说三遍.eslintrc.jsmodule.exports={root:true,…

    2022年6月7日
    98
  • win10键盘win键失效了[通俗易懂]

    win10键盘win键失效了[通俗易懂]与系统无关,应该是键盘关闭了win键。按Fn+windows键启用/关闭windows键,  

    2022年5月4日
    55

发表回复

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

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