vc實現https檔案下載
不多說廢話,直接上程式碼。
.h檔案
#pragma once
class CDownloadHttps
{
public:
CDownloadHttps();
~CDownloadHttps();
static BOOL DownloadFile(std::wstring serverName, std::wstring objectName, std::wstring path);
};
.cpp檔案
#include "stdafx.h"
#include "DownloadHttps.h"
#include "windows.h"
#include "winhttp.h"
#pragma comment(lib,"Winhttp.lib")
CDownloadHttps::CDownloadHttps()
{
}
CDownloadHttps::~CDownloadHttps()
{
}
BOOL CDownloadHttps::DownloadFile(std::wstring serverName, std::wstring objectName, std::wstring path)
{
HINTERNET hOpen = 0;
HINTERNET hConnect = 0;
HINTERNET hRequest = 0;
BOOL bResult = FALSE;
do {
hOpen = WinHttpOpen(_T("DownloadFile"), 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, serverName.c_str(), INTERNET_DEFAULT_HTTPS_PORT, 0);
if (!hConnect) {
wprintf(L"WinHttpConnect failed (0x%.8X)\n", GetLastError());
break;
}
// use flag WINHTTP_FLAG_SECURE to initiate SSL
hRequest = WinHttpOpenRequest(hConnect, L"GET", objectName.c_str(),
NULL, WINHTTP_NO_REFERER, WINHTTP_DEFAULT_ACCEPT_TYPES, 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
TCHAR szContentLength[32] = { 0 };
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);
BYTE Buffer[4096];
HANDLE hFile = CreateFile(path.c_str(), GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL);
if (hFile == INVALID_HANDLE_VALUE) {
wprintf(L"CreateFile(%s) failed (0x%.8X)\n", path.c_str(), GetLastError());
break;
}
DWORD dwSize, dwWrite;
while (WinHttpQueryDataAvailable(hRequest, &dwSize) && dwSize) {
if (dwSize > 4096) dwSize = 4096;
WinHttpReadData(hRequest, Buffer, dwSize, &dwSize);
WriteFile(hFile, Buffer, dwSize, &dwWrite, NULL);
}
CloseHandle(hFile);
bResult = TRUE;
} while (FALSE);
if (hRequest) WinHttpCloseHandle(hRequest);
if (hConnect) WinHttpCloseHandle(hConnect);
if (hOpen) WinHttpCloseHandle(hOpen);
return bResult;
}
參考了http://www.cppblog.com/zhupf/archive/2013/07/02/201459.aspx的內容,稍微改了下。轉載請註明出處。
相關文章
- Flutter實現檔案下載Flutter
- AJAX實現檔案下載----
- vc++實現檔案關聯C++
- Flutter 實現檔案下載功能Flutter
- Java實現檔案下載功能Java
- Weex 實現檔案的下載
- servlet實現檔案下載demoServlet
- Django 實現下載檔案功能Django
- 利用IhttpHandler實現檔案下載HTTP
- 使用Servlet實現檔案下載Servlet
- js下載檔案的實現方式JS
- js實現txt/excel檔案下載JSExcel
- Java程式碼實現下載檔案Java
- vue/js實現檔案流下載,檔案下載進度監聽VueJS
- Feign實現檔案上傳下載
- PowerShell實現檔案下載(類wget)wget
- 如何實現java檔案佇列下載Java佇列
- jsp下載檔案的實現方法JS
- SpringMVC下http和https路徑下的檔案下載SpringMVCHTTP
- iis實現點選檔案下載而不是開啟檔案
- javaWeb不生成臨時檔案實現壓縮檔案下載JavaWeb
- java實現檔案的下載的方法概述Java
- 前端實現檔案下載和拖拽上傳前端
- java實現sftp檔案的上傳下載JavaFTP
- Android 中 DownLoadManager 實現檔案下載Android
- 用C++實現下載檔案的功能C++
- SpringMVC實現檔案上傳&下載(2)SpringMVC
- Java實現上傳檔案到Oracle及從Oracle下載檔案JavaOracle
- VC從檔案中載入圖片
- Java多執行緒檔案分片下載實現Java執行緒
- Javascript 實現一個post方式的檔案下載JavaScript
- vue實現Excel檔案的上傳與下載VueExcel
- JavaWeb之實現檔案上傳與下載工具JavaWeb
- JavaWeb之實現檔案上傳與下載元件JavaWeb元件
- JavaWeb之實現檔案上傳與下載示例JavaWeb
- OSS實現檔案下載進度條顯示
- 2020JavaWeb實現檔案下載JavaWeb
- Java實現多檔案邊壓縮邊下載Java