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”);

}

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

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

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

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

(0)


相关推荐

  • idea for mac 激活码2021【中文破解版】

    (idea for mac 激活码2021)JetBrains旗下有多款编译器工具(如:IntelliJ、WebStorm、PyCharm等)在各编程领域几乎都占据了垄断地位。建立在开源IntelliJ平台之上,过去15年以来,JetBrains一直在不断发展和完善这个平台。这个平台可以针对您的开发工作流进行微调并且能够提供…

  • php .asmx,php实现通过soap调用.Net的WebService asmx文件

    php .asmx,php实现通过soap调用.Net的WebService asmx文件本文实例讲述了php实现通过soap调用.Net的WebServiceasmx文件。分享给大家供大家参考,具体如下:最近,帮一个同行测试用.net写的WebService接口,C#调用通过,现在需要测试一下php版本对它的调用,经过各种探索,相关的PHP调用webservice的过程如下:1.打开php相关扩展:找到配置文件php.ini文件,打开以下扩展extension=php_so…

  • pki体系包括_pki证书丢失

    pki体系包括_pki证书丢失一PKI简介PKI(PublicKeyInfrastructure)公共密钥基础建设,又称为公开密钥基础架构、公钥基础建设、公钥基础设施或公钥基础机构,是一组由硬件、软件、参与者、管理政策与流程组成的基础架构,能够为所有网络应用提供加密和数字签名等密码服务及所必须的密钥和证书管理体系,简单来说PKI就是利用公钥理论和技术建立的提供的安全服务设施,是信息安全技术的核心。其…

  • 安卓性能调优:内存使用分析和方法调用优化

    安卓性能调优:内存使用分析和方法调用优化

  • ASP.NET MVC4开发指南_vue mvc

    ASP.NET MVC4开发指南_vue mvcWhatistheWebGrid?TheWebGridisahelperthatwasincludedonthenewBeta1versionofASP.NETMVC3(Changescanhappenssinceitisabetaversion)thatenableustoshowdataeasily.Withasimp…

  • 利用Redis实现高并发计数器

    利用Redis实现高并发计数器业务需求中经常有需要用到计数器的场景:譬如一个手机号一天限制发送5条短信、一个接口一分钟限制多少请求、一个接口一天限制调用多少次等等。使用Redis的Incr自增命令可以轻松实现以上需求。以一个接口一天限制调用次数为例: /** *是否拒绝服务 *@return */ privatebooleandenialOfService(StringuserId){ longc…

发表回复

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

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