贴上代码
try
{
{
hSenssion = ::InternetOpen(0, INTERNET_OPEN_TYPE_DIRECT, 0, 0, 0);//初始化 WinInet.dll
if (!hSenssion)
{
wsprintf(urlBuff, _T("Error! InternetOpen failed! [%s] [%d] "), __FUNCTION__, __LINE__);
CFFLog::Out(urlBuff);
CFFLog::Out(_T("ERROR Num:%d"), GetLastError());
return FALSE;
}
DWORD dwTimeOut = 5000;
::InternetSetOption(hSenssion, INTERNET_OPTION_CONNECT_TIMEOUT, &dwTimeOut, sizeof(DWORD));
::InternetSetOption(hSenssion, INTERNET_OPTION_SEND_TIMEOUT, &dwTimeOut, sizeof(DWORD));
::InternetSetOption(hSenssion, INTERNET_OPTION_RECEIVE_TIMEOUT, &dwTimeOut, sizeof(DWORD));
//
}
//下载文件
DWORD dwFlag = INTERNET_FLAG_TRANSFER_BINARY | INTERNET_FLAG_DONT_CACHE | INTERNET_FLAG_RELOAD;
CDuiString strHttpHeader;
strHttpHeader.Format(_T("RANGE: bytes=%ld-\r\n"), lDownloadedSize);
hHttpFile = InternetOpenUrl(hSenssion, objTask.strFileURL, strHttpHeader, strHttpHeader.GetLength(), dwFlag, 0);
if (!hHttpFile)
{
if (hSenssion)
{
::InternetCloseHandle(hSenssion);
}
/*wchar_t szError[256];
DWORD dwErrorSize = 256;
DWORD dwLastErrorMsg;
::InternetGetLastResponseInfo(&dwLastErrorMsg, szError, &dwErrorSize);*/
wsprintf(urlBuff, _T("Error! InternetOpenUrl failed! [%s] [%d] Error Number is %d"), __FUNCTION__, __LINE__, GetLastError());
CFFLog::Out(urlBuff);
return FALSE;
}
_tfopen_s(&pFile, objTask.strDownloadPath, _T("ab+"));
if (!pFile)
{
wsprintf(urlBuff, _T("Open File ErrorNum:%d"), GetLastError());
CFFLog::Out(urlBuff);
if (hHttpFile)
{
::InternetCloseHandle(hHttpFile);
}
if (hSenssion)
{
::InternetCloseHandle(hSenssion);
}
return FALSE;
}
const int nRECVPACK_SIZE = 512;
char buf[nRECVPACK_SIZE + 1] = { 0 };
DWORD dwRead = 0;
do
{
if (this->IsClose())
{
if (pFile)
{
fclose(pFile);
pFile = NULL;
}
if (hHttpFile)
{
InternetCloseHandle(hHttpFile);
}
if (hSenssion)
{
InternetCloseHandle(hSenssion);
}
wsprintf(urlBuff, _T("Error! Thread close down load file [%s] failed! "), objTask.strDownloadPath);
CFFLog::Out(urlBuff);
return Status_UpdateCancel;
}
::InternetReadFile(hHttpFile, buf, nRECVPACK_SIZE, &dwRead);
if (dwRead == 0)
{
break;
}
fwrite(buf, dwRead, 1, pFile);
lDownloadedSize += dwRead;
gpFFDlownloader->UpdateDownLoadSpeed(dwRead);
//
//更新正在下载的文件包大小到当前已经下载的文件大小值
gpFFDlownloader->updateDownLoadSize(dwRead, 0);
//CFFLog::Out(_T("lDownloadedSize = %d"), lDownloadedSize);
} while (1);
}
catch (...)
{
if (pFile)
{
fclose(pFile);
pFile = NULL;
}
if (hHttpFile)
{
InternetCloseHandle(hHttpFile);
}
if (hSenssion)
{
InternetCloseHandle(hSenssion);
}
}
if (pFile)
{
fclose(pFile);
pFile = NULL;
}
if (hHttpFile)
{
InternetCloseHandle(hHttpFile);
}
if (hSenssion)
{
InternetCloseHandle(hSenssion);
}
if (lDownloadedSize == lTotalSize)
{
wsprintf(urlBuff, _T("OK, down load file success %s "),objTask.strUpdatePath.GetData());
CFFLog::Out(urlBuff);
return TRUE;
}
else
{
wsprintf(urlBuff, _T("Error! down load file size [%d] is not equal to the url file size[%d]"), lDownloadedSize, lTotalSize);
CFFLog::Out(urlBuff);
wsprintf(urlBuff, _T(" error file: [%s] ! "), objTask.strDownloadPath);
CFFLog::Out(urlBuff);
return FALSE;
}
1 个解决方案
#1
请检查每个函数调用的返回值。
#1
请检查每个函数调用的返回值。