dcsimg
 

Get web page using WinInet class wrapper

Tuesday Oct 1st 2002 by Asif Rashid

Get web page using WinInet class wrapper

WinInet is a high-level interface to the more complicated underlying Internet protocols (including HTTP, FTP, and Gopher).
WinInet allows your application to act as an HTTP, FTP, or Gopher client without its having to understand or, more importantly, keep up with the ever-evolving protocol standards. If you use WinInet in your applications, when standards change you can let WinInet worry about the changes while your interface to the protocol remains the same. WinInet can be used to write product-ordering systems, stock tickers/analyzers, online banking systems, FTP clients, your own Internet browser, and so on. Before WinInet, adding Internet communications to Windows-based applications required expertise in sockets and protocol specifications. Even simple communications required considerable development time. WinInet lets you quickly and easily add Internet communications to your applications.

MFC also implemented some class which uses these APIs. These classes are distributed in different hierarchies. I develop a small class for that which has only two methods. By introducing this class in project and calling one method, one can easily download the web page from given url.

This class has two methods,

    CString GetWebPage(const CString& Url);
    void SetErrorMessage(CString s);

GetWebPage method is used for accepting the url (it must me complete i.e., http:\\www.codeguru.com) and returning the desired page.

SetErrorMessage method receives the Default error message. When there was some error due to any reason, GetWebpage method will return this message. I am working on it and in future beside default error message, a actual error message will be also transmitted.

 

/*
//------------------------------------------------------------------------------------------------------------------
// WebWorld.h: interface for the CWebWorld class.
//------------------------------------------------------------------------------------------------------------------
*/

#include "wininet.h"

class CWebWorld
{
public:
    void SetErrorMessage(CString s);
    CString GetWebPage(const CString& Url);
    CWebWorld();
    virtual ~CWebWorld();

private:
    CString m_ErrorMessage;
    HINTERNET m_Session;
};

/*
//------------------------------------------------------------------------------------------------------------------
// WebWorld.cpp:  implementation of the CWebWorld class.
//------------------------------------------------------------------------------------------------------------------
*/

#include "stdafx.h"
#include "WebThief.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

#define AGENT_NAME  "CodeguruBrowser1.0"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CWebWorld::CWebWorld()
{
    DWORD dwError;

    // Initialize the Win32 Internet functions
    m_Session = ::InternetOpen(AGENT_NAME,
        INTERNET_OPEN_TYPE_PRECONFIG, // Use registry settings.
        NULL, // Proxy name. NULL indicates use default.
        NULL, // List of local servers. NULL indicates default.
        0) ;

    dwError = GetLastError();
}

CWebWorld::~CWebWorld()
{
    // Closing the session
    ::InternetCloseHandle(m_Session);
}

CString CWebWorld::GetWebPage(const CString& Url)
{
    HINTERNET hHttpFile;
    char szSizeBuffer[32];
    DWORD dwLengthSizeBuffer = sizeof(szSizeBuffer);
    DWORD dwFileSize;
    DWORD dwBytesRead;
    BOOL bSuccessful;
    CString Contents;

    // Setting default error message
    Contents = m_ErrorMessage;
   
    // Opening the Url and getting a Handle for HTTP file
    hHttpFile = InternetOpenUrl(m_Session, (const char *) Url, NULL, 0, 0, 0);

    if (hHttpFile)
    {   
        // Getting the size of HTTP Files
        BOOL bQuery = ::HttpQueryInfo(hHttpFile,HTTP_QUERY_CONTENT_LENGTH, szSizeBuffer, &dwLengthSizeBuffer, NULL) ;

        if(bQuery==TRUE)
        {   
            // Allocating the memory space for HTTP file contents
            dwFileSize=atol(szSizeBuffer);
            LPSTR szContents = Contents.GetBuffer(dwFileSize);

            // Read the HTTP file
            BOOL bRead = ::InternetReadFile(hHttpFile, szContents, dwFileSize, &dwBytesRead);
           
            if (bRead)
                bSuccessful = TRUE;

            ::InternetCloseHandle(hHttpFile); // Close the connection.
        }

    }
    else
    {
        // Connection failed.
        bSuccessful = FALSE;
    }
    return Contents;
}

void CWebWorld::SetErrorMessage(CString s)
{
    m_ErrorMessage = s;
}

Following is a use of above class.

    CWebWorld a;
    CString PageContent;

    a.SetErrorMessage("There is some error in getting web page ... ");
    PageContent = a.GetWebPage(m_Url);

Home
Mobile Site | Full Site
Copyright 2018 © QuinStreet Inc. All Rights Reserved