Search This Blog

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

No comments: