Search This Blog

Showing posts with label Internet. Show all posts
Showing posts with label Internet. Show all posts

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;
}

Friday, May 22, 2009

Download a web page into a file

     HRESULT hr;
     CString csWebsiteName,csFileName;
     csWebsiteName="http://google.com";
     csFileName="c:\\google.htm";
     hr = URLDownloadToFile( NULL ,csWebsiteName ,csFileName , 0 ,NULL );

Thursday, April 16, 2009

Download Webpage to content to CString

BOOL DownloadURLContent(const char *url, CString &csContent, CString &errorMessage)
{
     const int FILEBUFLEN = 1024;
     char *httpBuff = new char[FILEBUFLEN+1];
     memset( httpBuff ,0 , FILEBUFLEN+1 );
     TCHAR szErr[255];
     errorMessage = "";
     CString csTemp;
    
     TRY {
          CInternetSession session;
          session.SetOption(INTERNET_OPTION_CONNECT_TIMEOUT, 1000);
          session.SetOption(INTERNET_OPTION_CONNECT_RETRIES, 3);
          CFile *remoteFile = session.OpenURL(url, 1 ,INTERNET_FLAG_TRANSFER_BINARY | INTERNET_FLAG_RELOAD);
         
          int numBytes;
          while (numBytes = remoteFile->Read(httpBuff, FILEBUFLEN))
          {
               //localFile.Write(httpBuff, numBytes);

               csTemp=httpBuff;
               memset( httpBuff ,0 , FILEBUFLEN+1 );

               csContent=csContent+csTemp;
              
          }
     }
     CATCH_ALL(error) {
          error->GetErrorMessage(szErr,254,NULL);
          errorMessage.Format("%s",szErr);
          return FALSE;
     }
     END_CATCH_ALL;
    
     return TRUE;
}

Friday, December 19, 2008

MFC code to Download a webPage

#include "afxinet.h" //add this header file

BOOL DownloadURL(const char *url, const char *filename, CString &errorMessage)
{
const int FILEBUFLEN = 1024;
char httpBuff[FILEBUFLEN];
TCHAR szErr[255];
errorMessage = "";

TRY {
CInternetSession session;
session.SetOption(INTERNET_OPTION_CONNECT_TIMEOUT, 1000);
session.SetOption(INTERNET_OPTION_CONNECT_RETRIES, 3);
CFile *remoteFile = session.OpenURL(url, 1 ,INTERNET_FLAG_TRANSFER_BINARY | INTERNET_FLAG_RELOAD);
CFile localFile(filename, CFile::modeCreate | CFile::modeWrite | CFile::typeBinary);
int numBytes;
while (numBytes = remoteFile->Read(httpBuff, FILEBUFLEN))
{
localFile.Write(httpBuff, numBytes);
}
}
CATCH_ALL(error) {
error->GetErrorMessage(szErr,254,NULL);
errorMessage.Format("%s",szErr);
return FALSE;
}
END_CATCH_ALL;
return TRUE;
}

void CDownloadWebPageDlg::OnOK() //Copy the following Lines where you need to download.
{
CString csErr;
if(!DownloadURL("http://www.google.com","c:\\google.htm",csErr))
{
AfxMessageBox(csErr);
}
}