home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 40 / IOPROG_40.ISO / SOFT / mfc / URLCheck.ZIP / webworld.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1997-11-26  |  2.1 KB  |  89 lines

  1. // Internet.cpp: implementation of the CInternet class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4.  
  5. #include "stdafx.h"
  6. #include "webworld.h"
  7.  
  8. #ifdef _DEBUG
  9. #undef THIS_FILE
  10. static char THIS_FILE[]=__FILE__;
  11. #define new DEBUG_NEW
  12. #endif
  13.  
  14. #define AGENT_NAME "MyBrowser1.0"
  15.  
  16. //////////////////////////////////////////////////////////////////////
  17. // Construction/Destruction
  18. //////////////////////////////////////////////////////////////////////
  19.  
  20. CWebWorld::CWebWorld()
  21. {
  22.     DWORD dwError;
  23.  
  24.     // Initialize the Win32 Internet functions 
  25.     m_Session = ::InternetOpen(AGENT_NAME, 
  26.         INTERNET_OPEN_TYPE_PRECONFIG, // Use registry settings. 
  27.         NULL, // Proxy name. NULL indicates use default.
  28.         NULL, // List of local servers. NULL indicates default. 
  29.         0) ;
  30.      
  31.     dwError = GetLastError();
  32. }
  33.  
  34. CWebWorld::~CWebWorld()
  35. {
  36.     // Closing the session
  37.     ::InternetCloseHandle(m_Session);
  38. }
  39.  
  40. CString CWebWorld::GetWebPage(const CString& Url)
  41. {
  42.     HINTERNET hHttpFile;
  43.     char szSizeBuffer[32];
  44.     DWORD dwLengthSizeBuffer = sizeof(szSizeBuffer); 
  45.     DWORD dwFileSize;
  46.     DWORD dwBytesRead;
  47.     BOOL bSuccessful;
  48.     CString Contents;
  49.  
  50.     // Setting default error message
  51.     Contents = m_ErrorMessage;
  52.  
  53.     // Opening the Url and getting a Handle for HTTP file    
  54.     hHttpFile = InternetOpenUrl(m_Session, (const char *) Url, NULL, 0,INTERNET_FLAG_RELOAD , 0);
  55.  
  56.     if (hHttpFile)
  57.     {    
  58.         // Getting the size of HTTP Files
  59.         BOOL bQuery = ::HttpQueryInfo(hHttpFile,HTTP_QUERY_CONTENT_LENGTH, szSizeBuffer, &dwLengthSizeBuffer, NULL) ;
  60.  
  61.         if(bQuery==TRUE)
  62.         {    
  63.             // Allocating the memory space for HTTP file contents
  64.             dwFileSize=atol(szSizeBuffer);
  65.             LPSTR szContents = Contents.GetBuffer(dwFileSize);
  66.  
  67.             // Read the HTTP file 
  68.             BOOL bRead = ::InternetReadFile(hHttpFile, szContents, dwFileSize, &dwBytesRead); 
  69.             
  70.             if (bRead) 
  71.                 bSuccessful = TRUE;
  72.  
  73.             ::InternetCloseHandle(hHttpFile); // Close the connection.
  74.         }
  75.  
  76.     }
  77.     else
  78.     {
  79.         // Connection failed.
  80.         bSuccessful = FALSE;
  81.     } 
  82.     return Contents;
  83. }
  84.  
  85. void CWebWorld::SetErrorMessage(CString s)
  86. {
  87.     m_ErrorMessage = s;
  88. }
  89.