Get web page using WinInet class wrapper | CodeGuru

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 […]

Written By
CodeGuru Staff
CodeGuru Staff
Oct 1, 2002
4 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

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


CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.