// down.cpp : Defines the entry point for the application.
//
#include "stdafx.h"
#include <windows.h>
#include <wininet.h>
#include <string>
#include <iostream>
#include <vector>
using namespace std;
#pragma comment(lib, "wininet.lib")
//下载
#define DOWNHELPER_AGENTNAME "down"
#define LEN_OF_BUFFER_FOR_QUERYINFO 128
#define DOWNLOAD_BUF_SIZE (10*1024) //10KB
#define MAX_DOWNLOAD_REQUEST_TIME 10
#define MAX_DOWNLOAD_BYTESIZE (1000*1024*1024) //1000MB
BOOL _TryHttpSendRequest(LPVOID hRequest, int nMaxTryTimes); //多次发送请求函数
//HTTP下载函数,通过先请求HEAD的方式然后GET,可以通过HEAD对下载的文件类型和大小做限制
BOOL DownloadUrl(std::string strUrl, std::string strFileName)
{
BOOL bRet = FALSE;
if (strUrl == "" || strFileName == "")
return FALSE;
//定义变量
HINTERNET hInet = NULL; //打开internet连接handle
HINTERNET hConnect = NULL; //HTTP连接
HINTERNET hRequestHead = NULL; //HTTP Request
HINTERNET hRequestGet = NULL; //HTTP Request
HANDLE hFileWrite = NULL; //写文件的句柄
char* pBuf = NULL; //缓冲区
DWORD dwRequestTryTimes = MAX_DOWNLOAD_REQUEST_TIME; //尝试请求的次数
DWORD dwDownBytes = 0; //每次下载的大小
DWORD dwDownFileTotalBytes = 0; //下载的文件总大小
DWORD dwWriteBytes = 0; //写入文件的大小
char bufQueryInfo[LEN_OF_BUFFER_FOR_QUERYINFO] = {0}; //用来查询信息的buffer
DWORD dwBufQueryInfoSize = sizeof(bufQueryInfo);
DWORD dwStatusCode = 0;
DWORD dwContentLen = 0;
DWORD dwSizeDW = sizeof(DWORD);
//分割URL
CHAR pszHostName[INTERNET_MAX_HOST_NAME_LENGTH] = {0};
CHAR pszUserName[INTERNET_MAX_USER_NAME_LENGTH] = {0};
CHAR pszPassword[INTERNET_MAX_PASSWORD_LENGTH] = {0};
CHAR pszURLPath[INTERNET_MAX_URL_LENGTH] = {0};
CHAR szURL[INTERNET_MAX_URL_LENGTH] = {0};
URL_COMPONENTSA urlComponents = {0};
urlComponents.dwStructSize = sizeof(URL_COMPONENTSA);
urlComponents.lpszHostName = pszHostName;
urlComponents.dwHostNameLength = INTERNET_MAX_HOST_NAME_LENGTH;
urlComponents.lpszUserName = pszUserName;
urlComponents.dwUserNameLength = INTERNET_MAX_USER_NAME_LENGTH;
urlComponents.lpszPassword = pszPassword;
urlComponents.dwPasswordLength = INTERNET_MAX_PASSWORD_LENGTH;
urlComponents.lpszUrlPath = pszURLPath;
urlComponents.dwUrlPathLength = INTERNET_MAX_URL_LENGTH;
bRet = InternetCrackUrlA(strUrl.c_str(), 0, NULL, &urlComponents);
bRet = (bRet && urlComponents.nScheme == INTERNET_SERVICE_HTTP);
if (!bRet)
{
goto _END_OF_DOWNLOADURL;
}
//打开一个internet连接
hInet = InternetOpenA(DOWNHELPER_AGENTNAME, INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, NULL);
if (!hInet)
{
bRet = FALSE;
goto _END_OF_DOWNLOADURL;
}
//打开HTTP连接
hConnect = InternetConnectA(hInet, pszHostName, urlComponents.nPort, pszUserName, pszPassword, INTERNET_SERVICE_HTTP, 0, NULL);
if (!hConnect)
{
bRet = FALSE;
goto _END_OF_DOWNLOADURL;
}
//创建HTTP request句柄
if (urlComponents.dwUrlPathLength != 0)
strcpy(szURL, urlComponents.lpszUrlPath);
else
strcpy(szURL, "/");
//请求HEAD,通过HEAD获得文件大小及类型进行校验
hRequestHead = HttpOpenRequestA(hConnect, "HEAD", szURL, "HTTP/1.1", "", NULL, INTERNET_FLAG_RELOAD, 0);
bRet = _TryHttpSendRequest(hRequestHead, dwRequestTryTimes);
if (!bRet)
{
goto _END_OF_DOWNLOADURL; //请求HEAD失败
}
//查询content-length大小
dwContentLen = 0;
dwSizeDW = sizeof(DWORD);
bRet = HttpQueryInfo(hRequestHead, HTTP_QUERY_FLAG_NUMBER | HTTP_QUERY_CONTENT_LENGTH, &dwContentLen, &dwSizeDW, NULL);
if (bRet)
{
//检查是否文件过大
if (dwContentLen > MAX_DOWNLOAD_BYTESIZE)
{
bRet = FALSE;
goto _END_OF_DOWNLOADURL;
}
}
//校验完成后再请求GET,下载文件
hRequestGet = HttpOpenRequestA(hConnect, "GET", szURL, "HTTP/1.1", "", NULL, INTERNET_FLAG_RELOAD, 0);
bRet = _TryHttpSendRequest(hRequestGet, dwRequestTryTimes);
if (!bRet)
{
goto _END_OF_DOWNLOADURL; //请求HEAD失败
}
//创建文件
hFileWrite = CreateFileA(strFileName.c_str(), GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (INVALID_HANDLE_VALUE == hFileWrite)
{
bRet = FALSE;
goto _END_OF_DOWNLOADURL;
}
//分配缓冲
pBuf = new char[DOWNLOAD_BUF_SIZE]; //分配内存
if (!pBuf)
{
bRet = FALSE;
goto _END_OF_DOWNLOADURL;
}
//多次尝试下载文件
dwDownFileTotalBytes = 0;
while (1)
{
dwDownBytes = 0;
memset(pBuf, 0, DOWNLOAD_BUF_SIZE*sizeof(char));
bRet = InternetReadFile(hRequestGet, pBuf, DOWNLOAD_BUF_SIZE, &dwDownBytes);
if (bRet)
{
if (dwDownBytes > 0)
{
dwDownFileTotalBytes += dwDownBytes;
bRet = WriteFile(hFileWrite, pBuf, dwDownBytes, &dwWriteBytes, NULL); //写入文件
if (!bRet)
{
goto _END_OF_DOWNLOADURL;
}
}
else if (0 == dwDownBytes)
{
bRet = TRUE;
break; //下载成功完成
}
}
}
//清理
_END_OF_DOWNLOADURL:
if (INVALID_HANDLE_VALUE != hFileWrite)
CloseHandle(hFileWrite);
if (pBuf)
delete [] pBuf;
if (hRequestGet)
InternetCloseHandle(hRequestGet);
if (hRequestHead)
InternetCloseHandle(hRequestHead);
if (hConnect)
InternetCloseHandle(hConnect);
if (hInet)
InternetCloseHandle(hInet);
return bRet;
}
//多次发送请求函数
BOOL _TryHttpSendRequest(LPVOID hRequest, int nMaxTryTimes)
{
BOOL bRet = FALSE;
DWORD dwStatusCode = 0;
DWORD dwSizeDW = sizeof(DWORD);
while (hRequest && (nMaxTryTimes-- > 0)) //多次尝试发送请求
{
//发送请求
bRet = HttpSendRequestA(hRequest, NULL, 0, NULL, 0);
if (!bRet)
{
continue;
}
else
{
//判断HTTP返回的状态码
dwStatusCode = 0;
dwSizeDW = sizeof(DWORD);
bRet = HttpQueryInfo(hRequest, HTTP_QUERY_FLAG_NUMBER | HTTP_QUERY_STATUS_CODE, &dwStatusCode, &dwSizeDW, NULL);
if (bRet)
{
//检查状态码
if (HTTP_STATUS_OK == dwStatusCode) //200 OK
{
break;
}
else
{
bRet = FALSE;
continue;
}
}
}
}
return bRet;
}
//////////////////////////////////////////////////////////////////////////
BOOL HttpGet(string url, string &result)
{
vector<char> v;
const CHAR * szUrl = url.c_str();
CHAR szAgent[] = "";
HINTERNET hInternet1 =
InternetOpen(NULL,INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,NULL);
if (NULL == hInternet1)
{
InternetCloseHandle(hInternet1);
return FALSE;
}
HINTERNET hInternet2 =
InternetOpenUrl(hInternet1,szUrl,NULL,NULL,INTERNET_FLAG_NO_CACHE_WRITE,NULL);
if (NULL == hInternet2)
{
InternetCloseHandle(hInternet2);
InternetCloseHandle(hInternet1);
return FALSE;
}
DWORD dwMaxDataLength = 500;
PBYTE pBuf = (PBYTE)malloc(dwMaxDataLength*sizeof(TCHAR));
if (NULL == pBuf)
{
InternetCloseHandle(hInternet2);
InternetCloseHandle(hInternet1);
return FALSE;
}
DWORD dwReadDataLength = NULL;
BOOL bRet = TRUE;
do
{
ZeroMemory(pBuf,dwMaxDataLength*sizeof(TCHAR));
bRet = InternetReadFile(hInternet2,pBuf,dwMaxDataLength,&dwReadDataLength);
for (DWORD dw = 0;dw < dwReadDataLength;dw++)
{
v.push_back(pBuf[dw]);
}
} while (NULL != dwReadDataLength);
vector<char>::iterator i;
for(i=v.begin(); i!=v.end(); i++)
result += *i;
return TRUE;
}
//////////////////////////////////////////////////////////////////////////
/*
int main(int argc, char* argv[])
{
cout << "正在下载...";
BOOL bR = DownloadUrl("http://42.duote.com.cn/office2007.zip", "test.zip");
if (bR)
cout << "完成" << endl;
else
cout << "失败" << endl;
return 0;
}
*/
string trim(const string& str)
{
string::size_type pos = str.find_first_not_of(' ');
if (pos == string::npos)
{
return str;
}
string::size_type pos2 = str.find_last_not_of(' ');
if (pos2 != string::npos)
{
return str.substr(pos, pos2 - pos + 1);
}
return str.substr(pos);
}
int split(const string& str, vector<string>& ret_, string sep = ",")
{
if (str.empty())
{
return 0;
}
string tmp;
string::size_type pos_begin = str.find_first_not_of(sep);
string::size_type comma_pos = 0;
while (pos_begin != string::npos)
{
comma_pos = str.find(sep, pos_begin);
if (comma_pos != string::npos)
{
tmp = str.substr(pos_begin, comma_pos - pos_begin);
pos_begin = comma_pos + sep.length();
}
else
{
tmp = str.substr(pos_begin);
pos_begin = comma_pos;
}
if (!tmp.empty())
{
ret_.push_back(tmp);
tmp = "";
}
}
return 0;
}
string replace(const string& str, const string& src, const string& dest)
{
string ret;
string::size_type pos_begin = 0;
string::size_type pos = str.find(src);
while (pos != string::npos)
{
cout <<"replacexxx:" << pos_begin <<" " << pos <<"\n";
ret.append(str.data() + pos_begin, pos - pos_begin);
ret += dest;
pos_begin = pos + 1;
pos = str.find(src, pos_begin);
}
if (pos_begin < str.length())
{
ret.append(str.begin() + pos_begin, str.end());
}
return ret;
}
void Debug(string str)
{
MessageBox(NULL, str.c_str(), "DEBUG", MB_OK);
}
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
string html;
if (!HttpGet("http://42.96.190.81:81/list.txt", html))
{
MessageBox(NULL, "列表获取失败!", "ERROR", MB_OK);
return 0;
}
vector<string> urls;
split(html, urls, "\r\n");
for(int i=0; i<urls.size(); i++)
{
size_t pos = urls[i].rfind('/', urls[i].size());
string fileName = urls[i].substr(pos+1, urls[i].size()-pos-1);
if (DownloadUrl(urls[i], fileName))
{
Debug(fileName + "下载成功");
}
}
Debug("完成");
return 0;
}
本文为“技术点滴”的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。