#include <assert.h>
#include "HTTPAPI.h"
#include "HTTPConnect.h"
#include "ustd\string.h"
static int HTTPDownloadFile(HTTPConnect& connector, HANDLE hFile, bool (*ProgressCallback)(void*, int), void* param)
{
// HTTP Status Code
DWORD statusCode = 0;
if (!connector.QueryStatusCode(statusCode) || statusCode != HTTP_STATUS_OK)
{
// assert(false);
return statusCode;
}
// 文件长度
DWORD contentLength = 0;
if (!connector.QueryContentLength(contentLength))
{
assert(false);
return _ERRCODE_HTTP;
}
// 读文件
unsigned long long nTotalBytes = 0;
DWORD nReadBytes = 0;
unsigned char buffer[4096] = { 0 };
while (connector.Read(buffer, sizeof(buffer), nReadBytes) && nReadBytes > 0)
{
DWORD nWriteBytes = 0;
if (!::WriteFile(hFile, buffer, nReadBytes, &nWriteBytes, NULL))
{
return _ERRCODE_FILE;
}
assert(nReadBytes == nWriteBytes);
nTotalBytes += nWriteBytes;
// 回调 -- 进度报告
if (ProgressCallback != NULL)
{
if (contentLength == 0)
{
(*ProgressCallback)(param, 100);
}
else
{
int percent = (int)((nTotalBytes * 100) / (unsigned long long)(contentLength));
if (!(*ProgressCallback)(param, percent))
{
return _ERRCODE_EXIT;
}
}
}
}
return _ERRCODE_SUCCESS;
}
int HTTPDownloadFile(const TCHAR* url, const TCHAR* filename, bool (*ProgressCallback)(void*, int), void* param, bool bAlwaysIgnoreCache/* = false*/)
{
int errCode = _ERRCODE_NET;
// step 1. open file
HANDLE hFile = CreateFile(
filename,
GENERIC_WRITE,
FILE_SHARE_READ,
NULL,
CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
NULL);
if (hFile == INVALID_HANDLE_VALUE)
{
return _ERRCODE_FILE;
}
// step 2. http connect
HTTPConnect connector;
HTTPConnect::URLParser urlParser(url);
if (connector.Connect(urlParser.GetHostname().c_str(), urlParser.GetPort()))
{
std::basic_string<TCHAR> strHeader(_T("Pragma: no-cache\r\n"));
if (bAlwaysIgnoreCache)
{
strHeader += _T("If-Modified-Since:0\r\n");
}
// step 3. construct http request
if (connector.OpenRequest(_T("GET"), urlParser.GetPath().c_str()) &&
connector.AddRequestHeaders(strHeader.c_str()) &&
connector.SendRequest(NULL, 0))
{
// step 4. read / write file
errCode = HTTPDownloadFile(connector, hFile, ProgressCallback, param);
}
connector.Close();
}
// step 5. close;
CloseHandle(hFile);
// step 6. rollback
if (errCode != _ERRCODE_SUCCESS)
{
::DeleteFile(filename);
}
return errCode;
}
static int HTTPDownloadFileEx(HTTPConnect& connector, HANDLE hFile, bool (*ProgressCallback)(void*, int), void* param)
{
// HTTP Status Code
DWORD statusCode = 0;
if (!connector.QueryStatusCode(statusCode) || statusCode != HTTP_STATUS_OK)
{
// assert(false);
return statusCode;
}
// 文件长度
DWORD contentLength = 0;
if (!connector.QueryContentLength(contentLength))
{
assert(false);
return _ERRCODE_HTTP;
}
// 读文件
unsigned long long nTotalBytes = 0;
DWORD nReadBytes = 0;
unsigned char buffer[4096] = { 0 };
while (connector.Read(buffer, sizeof(buffer), nReadBytes) && nReadBytes > 0)
{
DWORD nWriteBytes = 0;
if (!::WriteFile(hFile, buffer, nReadBytes, &nWriteBytes, NULL))
{
return _ERRCODE_FILE;
}
assert(nReadBytes == nWriteBytes);
nTotalBytes += nWriteBytes;
// 回调 -- 进度报告
if (ProgressCallback != NULL)
{
if (contentLength == 0)
{
(*ProgressCallback)(param, 100);
}
else
{
int percent = (int)((nTotalBytes * 100) / (unsigned long long)(contentLength));
if (!(*ProgressCallback)(param, percent))
{
return _ERRCODE_EXIT;
}
}
}
}
return _ERRCODE_SUCCESS;
}
int HTTPDownloadFileEx(const TCHAR* url, const TCHAR* filename, bool (*ProgressCallback)(void*, int), void* param, bool bAlwaysIgnoreCache,void *pCallbackParam)
{
int errCode = _ERRCODE_NET;
// step 1. open file
HANDLE hFile = CreateFile(
filename,
GENERIC_WRITE,
FILE_SHARE_READ,
NULL,
CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
NULL);
if (hFile == INVALID_HANDLE_VALUE)
{
return _ERRCODE_FILE;
}
// step 2. http connect
HTTPConnect connector;
HTTPConnect::URLParser urlParser(url);
if (connector.Connect(urlParser.GetHostname().c_str(), urlParser.GetPort()))
{
std::basic_string<TCHAR> strHeader(_T("Pragma: no-cache\r\n"));
if (bAlwaysIgnoreCache)
{
strHeader += _T("If-Modified-Since:0\r\n");
}
// step 3. construct http request
if (connector.OpenRequest(_T("GET"), urlParser.GetPath().c_str()) &&
connector.AddRequestHeaders(strHeader.c_str()) &&
connector.SendRequest(NULL, 0))
{
// step 4. read / write file
errCode = HTTPDownloadFileEx(connector, hFile, ProgressCallback, param);
}
connector.Close();
}
// step 5. close;
CloseHandle(hFile);
// step 6. rollback
if (errCode != _ERRCODE_SUCCESS)
{
::DeleteFile(filename);
}
return errCode;
}
static int HTTPGet(HTTPConnect& connector, std::string& response)
{
// HTTP Status Code
DWORD statusCode = 0;
if (!connector.QueryStatusCode(statusCode) || statusCode != HTTP_STATUS_OK)
{
// assert(false);
return statusCode;
}
// 文件长度
DWORD contentLength = 0;
if (!connector.QueryContentLength(contentLength))
{
assert(false);
return _ERRCODE_HTTP;
}
// 读文件
unsigned long long nTotalBytes = 0;
DWORD nReadBytes = 0;
unsigned char buffer[4096] = { 0 };
while (connector.Read(buffer, sizeof(buffer), nReadBytes) && nReadBytes > 0)
{
response.append((char*)buffer, nReadBytes);
}
return _ERRCODE_SUCCESS;
}
static int HTTPGetEx(HTTPConnect& connector, std::string& response)
{
// HTTP Status Code
DWORD statusCode = 0;
if (!connector.QueryStatusCode(statusCode) || statusCode != HTTP_STATUS_OK)
{
// assert(false);
return statusCode;
}
// 读文件
unsigned long long nTotalBytes = 0;
DWORD nReadBytes = 0;
unsigned char buffer[4096] = { 0 };
while (connector.Read(buffer, sizeof(buffer), nReadBytes) && nReadBytes > 0)
{
response.append((char*)buffer, nReadBytes);
}
return _ERRCODE_SUCCESS;
}
int HTTPGet(const TCHAR* url, std::string& response)
{
int errCode = _ERRCODE_NET;
// step 1. http connect
HTTPConnect connector;
HTTPConnect::URLParser urlParser(url);
if (connector.Connect(urlParser.GetHostname().c_str(), urlParser.GetPort()))
{
// step 2. construct http request
if (connector.OpenRequest(_T("GET"), urlParser.GetPath().c_str()) &&
connector.AddRequestHeaders(_T("Pragma: no-cache\r\n")) &&
connector.SendRequest(NULL, 0))
{
// step 3. read / write file
errCode = HTTPGet(connector, response);
}
connector.Close();
}
return errCode;
}
int HTTPGetEx(const TCHAR* url, std::string& response)
{
int errCode = _ERRCODE_NET;
// step 1. http connect
HTTPConnect connector;
HTTPConnect::URLParser urlParser(url);
if (connector.Connect(urlParser.GetHostname().c_str(), urlParser.GetPort()))
{
// step 2. construct http request
if (connector.OpenRequest(_T("GET"), urlParser.GetPath().c_str()) &&
connector.AddRequestHeaders(_T("Pragma: no-cache\r\n")) &&
connector.SendRequest(NULL, 0))
{
// step 3. read / write file
errCode = HTTPGetEx(connector, response);
}
connector.Close();
}
return errCode;
}
int HTTPGet(const TCHAR* url)
{
std::string response;
return HTTPGet(url, response);
}
static int HTTPTestSpeed(HTTPConnect& connector, size_t& nTotalBytes)
{
// HTTP Status Code
DWORD statusCode = 0;
if (!connector.QueryStatusCode(statusCode) || statusCode != HTTP_STATUS_OK)
{
// assert(false);
return statusCode;
}
// 文件长度
DWORD contentLength = 0;
if (!connector.QueryContentLength(contentLength))
{
// assert(false);
return _ERRCODE_HTTP;
}
http.rar_HTTP_O9A4_OM7Q_heardsz8_http协议封装辅助类
版权申诉
40 浏览量
2022-07-15
02:03:27
上传
评论
收藏 5KB RAR 举报
寒泊
- 粉丝: 76
- 资源: 1万+
最新资源
- PicGo-Setup-2.3.1.exe
- 【分类数据集】战斗飞机图像分类数据集7300张30类.zip
- 基于C语言+AT89C52单片机搭建的PID直流电机控制程序,用于Proteus电路仿真+源码+开发文档(高分优秀项目)
- HBuilder X.zip
- 基于C++开发的unreal 数字电路仿真+源码(毕业设计&课程设计&项目开发)
- Modeling_three_phase_inverter.slx
- 营销人员;绩效管理;绩效考核
- 基于Vue+TypeScript开发的数字电路仿真软件+源码(毕业设计&课程设计&项目开发)
- Survival Animations 1.0
- 基于 Python Flask 的微博系统
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈