#include "Wininet.h"
#pragma comment(lib, "Wininet.lib")
{
CString str;
HINTERNET hSession = ::InternetOpen(_T("YourSectionName") , INTERNET_OPEN_TYPE_PRECONFIG , NULL , NULL , 0);
if(hSession == NULL)
return FALSE;
HINTERNET hHttpFile = ::InternetOpenUrl(hSession , pszUrl , NULL , 0 , 0 , 0);
if(hHttpFile == NULL)
{
::InternetCloseHandle(hSession);
return FALSE;
}
TCHAR szSizeBuffer [MAX_PATH];
DWORD dwLengthSizeBuffer = sizeof(szSizeBuffer);
BOOL bRetun = FALSE;
if(::HttpQueryInfo(hHttpFile, HTTP_QUERY_CONTENT_LENGTH, szSizeBuffer, &dwLengthSizeBuffer, NULL))
{
DWORD dwFileSize = _ttol(szSizeBuffer);
dwBytesRead = 0;
if(dwDataSize < dwFileSize)
{
dwDataSize = dwFileSize;
}
else if(::InternetReadFile(hHttpFile , lpData , dwFileSize , &dwBytesRead))
{
bRetun=TRUE;
}
}
::InternetCloseHandle(hHttpFile);
::InternetCloseHandle(hSession);
return bRetun;
}
BOOL SaveHttpFile(LPCTSTR pszUrl, LPCTSTR lpszFile)
{
DWORD dataSize = 0;
DWORD dwByte = 0;
GetDataFromUrl(pszUrl, NULL, dataSize, dwByte);
if(dataSize == 0)
return FALSE;
CFile file;
if(!file.Open(lpszFile, CFile::modeCreate|CFile::modeWrite))
return FALSE;
LPBYTE lpData = new BYTE[dataSize];
memset(lpData, 0, dataSize);
BOOL bRet = GetDataFromUrl(pszUrl, lpData, dataSize, dwByte);
if(bRet)
file.Write(lpData, dwByte);
delete []lpData;
return bRet;
}