Search This Blog

Thursday, April 1, 2010

Download a webpage using WinInet



#include <wininet.h>


BOOL DownloadWebContentUsingWinInet(CString csURL,CString &csDownloadedContent)
{

if ( csURL.IsEmpty() )
return FALSE;
HINTERNET hINet, hFile;
hINet = InternetOpen("InetURL/1.0", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0 );
if ( !hINet )
{
AfxMessageBox("InternetOpen Failed");
return FALSE;
}
hFile = InternetOpenUrl( hINet, csURL, NULL, 0, 0, 0 ) ;
if ( hFile )
{
CHAR buffer[1024];
DWORD dwRead;
while ( InternetReadFile( hFile, buffer, 1023, &dwRead ) )
{
if ( dwRead == 0 )
break;
buffer[dwRead] = 0;
csDownloadedContent += buffer;
}
InternetCloseHandle( hFile );
}
InternetCloseHandle( hINet );

return TRUE;
}

No comments: