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


相关推荐

  • 关于Loadrunner11激活成功教程的各种问题。。。泪奔。。。[通俗易懂]

    关于Loadrunner11激活成功教程的各种问题。。。泪奔。。。[通俗易懂]loadrunner11的激活成功教程步骤:(1)打开LoadRunner8.0文件夹,用LR8.0中的mlr5lprg.dll、lm70.dll覆盖LR11安装目录下“bin”文件夹中的对应文件4.手动修改注册表,删除下面内容,也就是Licesen2目录(不删除的话,在添加licence时,会提示“Licensesecurityviolation……”):[HKEY_LO

    2022年7月22日
    6
  • java笔记02

    java笔记02

    2021年7月8日
    68
  • 注册广播接收器registerReceiver

    注册广播接收器registerReceiver从registerReceiver(BroadcastReceiverreceiver,IntentFilterfilter)出发所经历的类和方法:registerReceiver(receiver,filter)–>ContextWrapper.java$registerReceiver(receiver,filter);@OverridepublicIntentregis…

    2025年7月25日
    0
  • mybatis的collection属性_安全带的使用方法和步骤

    mybatis的collection属性_安全带的使用方法和步骤问题1==>n问题选项//问题实体类publicclassQuestion{ private Stringid;//ID private Stringcontent;//问题 private Stringtype;//问题类型1:单选,2:多选,3:问答 private Integersort;//排序 privateList<Questi…

    2022年8月22日
    4
  • vue页面刷新_vue强制重置组件

    vue页面刷新_vue强制重置组件vue页面刷新首先我们都知道vue属于单页面应用,默认境况下是不会触发刷新页面操作的,所以这个时候就需要我们通过事件来触发reload()来达到刷新操作接下来我就为大家介绍三种刷新页面的方法1.wiindow.location.reload([bForceGet])该方法强迫浏览器刷新当前页面bForceGet可选参数,默认为false,从客户端缓存里取当前true,则以get方式,从服务器端获取最新的页面,相当于页面f5刷新wiindow.location.replace(URL)

    2022年10月16日
    0
  • 学习java的好书及视频推荐

    学习java的好书及视频推荐转载来自:点击打开链接要想在java领域成为大牛,除了不断进行项目实战以外,还要不断的进行进修和学习,以下将本人学习java多年使用的好书和一些好的视频推荐给大家,这些书和视频都是本人在网络找了很久,后来又经过实践证明的好书和视频。希望对大家学习java有帮助首先,是书的推荐:1学习java,java基础,1.0 入门:HeadFirstJava(

    2022年6月21日
    25

发表回复

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

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