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實現檔案下載----
- 使用Servlet實現檔案下載Servlet
- Flutter 實現檔案下載功能Flutter
- Java實現檔案下載功能Java
- js下載檔案的實現方式JS
- Feign實現檔案上傳下載
- 2020JavaWeb實現檔案下載JavaWeb
- js實現txt/excel檔案下載JSExcel
- vue/js實現檔案流下載,檔案下載進度監聽VueJS
- SpringMVC實現檔案上傳&下載(2)SpringMVC
- javaWeb不生成臨時檔案實現壓縮檔案下載JavaWeb
- iis實現點選檔案下載而不是開啟檔案
- .netcore+vue 實現壓縮檔案下載NetCoreVue
- java實現檔案的下載的方法概述Java
- 前端實現檔案下載和拖拽上傳前端
- Java多執行緒檔案分片下載實現Java執行緒
- Javascript 實現一個post方式的檔案下載JavaScript
- OSS實現檔案下載進度條顯示
- Python使用socket_TCP實現小檔案下載PythonTCP
- vue實現Excel檔案的上傳與下載VueExcel
- JavaWeb之實現檔案上傳與下載工具JavaWeb
- JavaWeb之實現檔案上傳與下載元件JavaWeb元件
- JavaWeb之實現檔案上傳與下載示例JavaWeb
- React中使用fetch實現檔案上傳下載React
- Java實現多檔案邊壓縮邊下載Java
- js實現使用檔案流下載csv檔案JS
- 點選按鈕,實現檔案下載,通過按鈕傳送url,spring後臺實現伺服器端檔案下載。Spring伺服器
- springCloud 微服務通過minio實現檔案上傳和檔案下載介面SpringGCCloud微服務
- 教你如何實現c#檔案上傳下載功能C#
- JavaWeb之實現檔案上傳與下載外掛JavaWeb
- JavaWeb之實現檔案上傳與下載原始碼JavaWeb原始碼
- JavaWeb之實現檔案上傳與下載例項JavaWeb
- 02vue+axios+form實現檔案下載(附Java實現程式碼)VueiOSORMJava
- 檔案下載
- 使用Vue+go實現前後端檔案的上傳下載,csv檔案上傳下載可直接照搬VueGo後端
- vc實現ping
- flutter使用dio實現 檔案下載並實現進度監聽總結Flutter