BOOL HttpDown(void)
{
HINTERNET hinternet = InternetOpen(_T("CartoType"),INTERNET_OPEN_TYPE_DIRECT,NULL,NULL,0);
if (NULL == hinternet)
{
MessageBox(L"打开网络不成功",L"错误",MB_OKCANCEL);
InternetCloseHandle(hinternet);
return FALSE;
}
HINTERNET hfile = InternetOpenUrl(hinternet,(LPCTSTR)szURL,NULL,0,0,1);
if (NULL == hfile)
{
MessageBox(L"打开网络地址不成功",L"错误",MB_OKCANCEL);
InternetCloseHandle(hfile);
InternetCloseHandle(hinternet);
return FALSE;
}
char Buffer[32] = {0};
DWORD BufLen = sizeof(Buffer);
DWORD WIndex = 0;
DWORD dwFileSize = 0;
HttpQueryInfo(hfile,HTTP_QUERY_CONTENT_LENGTH , Buffer, &BufLen, &WIndex);
dwFileSize=(DWORD)_wtoi((wchar_t*)Buffer);
CFile myFile;
CFileException fileException;
CString currentPath,fileName;
GetCurrentDirectory(currentPath);
if ( !myFile.Open (currentPath+fileName, CFile::modeCreate | CFile::modeReadWrite,
&fileException ) )
{
MessageBox(L"创建文件不成功",L"错误",MB_OKCANCEL);
return FALSE;
}
if (dwFileSize==0)
dwFileSize=10000000;//设置个最大值
DWORD tmp2=dwFileSize/100;
DWORD currentFileSize=0;
CString str;
str.Format(_T("已下载%d%%"),0);
DWORD dwSize;
VOID * szTemp[25];
do
{
if (!InternetReadFile (hfile, szTemp, 50, &dwSize) )
{
myFile.Close();
return FALSE;
}
if (!dwSize)
break; // Condition of dwSize=0 indicate EOF. Stop.
else
{
myFile.Write(szTemp,dwSize);
currentFileSize+=dwSize;
}
if ((currentFileSize/50)%(tmp2/50)==0)
{
lb.DeleteString(lb.GetCount()-1);
str.Format(_T("已下载%d%%"),currentFileSize/tmp2);
}
} // do
while (TRUE);
myFile.Close();
InternetCloseHandle(hfile);
InternetCloseHandle(hinternet);
return TRUE;
}