VC++ HTTP/HTTPS文件下载

21 篇文章 0 订阅
12 篇文章 2 订阅
#include <afxinet.h>
BOOL HttpDownload(const CString& strFileURLInServer, const CString & strFileLocalFullPath)//存放到本地的路径
{
	ASSERT(strFileURLInServer != "");
	ASSERT(strFileLocalFullPath != "");
	CInternetSession session;
	CHttpConnection* pHttpConnection = NULL;
	CHttpFile* pHttpFile = NULL;
	CString strServer, strObject;
	INTERNET_PORT wPort;
	BOOL bReturn = FALSE;
 
	DWORD dwType;
	const int nTimeOut = 2000;
	session.SetOption(INTERNET_OPTION_CONNECT_TIMEOUT, nTimeOut);	//重试之间的等待延时 
	session.SetOption(INTERNET_OPTION_CONNECT_RETRIES, 1);			//重试次数 
	char* pszBuffer = NULL;
 
	try
	{
		AfxParseURL(strFileURLInServer, dwType, strServer, strObject, wPort);
		if (strFileURLInServer.Left(5) == _T("https"))
		{
			pHttpConnection = session.GetHttpConnection(strServer, INTERNET_FLAG_SECURE, wPort, NULL, NULL);
			pHttpFile = pHttpConnection->OpenRequest(CHttpConnection::HTTP_VERB_GET,
				strObject,
				NULL, 1,
				NULL, NULL, INTERNET_FLAG_SECURE |
				INTERNET_FLAG_EXISTING_CONNECT |
				INTERNET_FLAG_RELOAD |
				INTERNET_FLAG_NO_CACHE_WRITE |
				INTERNET_FLAG_IGNORE_CERT_DATE_INVALID |
				INTERNET_FLAG_IGNORE_CERT_CN_INVALID/*| INTERNET_FLAG_TRANSFER_ASCII*/);//打开请求 ,这几个标识都要加上
		}
		else if (strFileURLInServer.Left(4) == _T("http"))
		{
			pHttpConnection = session.GetHttpConnection(strServer, wPort);
			pHttpFile = pHttpConnection->OpenRequest(CHttpConnection::HTTP_VERB_GET, strObject);
		}
		else
		{
			return bReturn;
		}
 
		if (pHttpFile->SendRequest() == FALSE)
			return bReturn;
		DWORD dwStateCode;
		pHttpFile->QueryInfoStatusCode(dwStateCode);
		if (dwStateCode == HTTP_STATUS_OK)
		{
			HANDLE hFile = CreateFile(strFileLocalFullPath, GENERIC_WRITE,
				FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL,
				NULL); //创建本地文件 
			if (hFile == INVALID_HANDLE_VALUE)
			{
				pHttpFile->Close();
				pHttpConnection->Close();
				session.Close();
				return bReturn;
			}
 
			char szInfoBuffer[1000]; //返回消息 
			DWORD dwFileSize = 0; //文件长度 
			DWORD dwInfoBufferSize = sizeof(szInfoBuffer);
			BOOL bResult = FALSE;
			bResult = pHttpFile->QueryInfo(HTTP_QUERY_CONTENT_LENGTH,
				(void*)szInfoBuffer, &dwInfoBufferSize, NULL);
 
			dwFileSize = atoi(szInfoBuffer);
			const int BUFFER_LENGTH = 1024 * 10;
			pszBuffer = new char[BUFFER_LENGTH]; //读取文件的缓冲 
			DWORD dwWrite, dwTotalWrite;
			dwWrite = dwTotalWrite = 0;
			UINT nRead = pHttpFile->Read(pszBuffer, BUFFER_LENGTH); //读取服务器上数据 
 
			while (nRead > 0)
			{
				WriteFile(hFile, pszBuffer, nRead, &dwWrite, NULL); //写到本地文件 
				dwTotalWrite += dwWrite;
				nRead = pHttpFile->Read(pszBuffer, BUFFER_LENGTH);
				MSG msg;
				for (int i = 0; i < 10; i++)
				{
					if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
					{
						TranslateMessage(&msg);
						DispatchMessage(&msg);
					}
				}
			}
 
			delete[]pszBuffer;
			pszBuffer = NULL;
			CloseHandle(hFile);
			bReturn = TRUE;
		}
	}
	catch (CInternetException* e)
	{
		TCHAR tszErrString[256];
		e->GetErrorMessage(tszErrString, sizeof(tszErrString));
		e->Delete();
	}
	catch (...)
	{
	}
 
	if (pszBuffer != NULL)
	{
		delete[]pszBuffer;
	}
	if (pHttpFile != NULL)
	{
		pHttpFile->Close();
		delete pHttpFile;
	}
	if (pHttpConnection != NULL)
	{
		pHttpConnection->Close();
		delete pHttpConnection;
	}
	session.Close();
	return bReturn;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值