Get web page using WinInet class wrapper


Desktop-as-a-Service Designed for Any Cloud ? Nutanix Frame

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
    void SetErrorMessage(CString s);
    CString GetWebPage(const CString& Url);
    virtual ~CWebWorld();

    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

#define AGENT_NAME  "CodeguruBrowser1.0"

// Construction/Destruction

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

    // Closing the 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) ;

            // Allocating the memory space for HTTP file contents
            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.

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


  • can't identify url of a webpage-sending data using post method

    Posted by ancy anna on 09/25/2004 09:46am

    i have a web page with 2 submit buttons(next and Previous) and the form action attribute is set as "post".Then how can we identify the url of next or previous page.Also how can we access to the source html of this URL.

  • HttpQueryInfo API is not retrieving the proper Cookie data.

    Posted by Legacy on 01/26/2004 08:00am

    Originally posted by: Shanmugam


    I want to connect to HTTPS password server to authenticate login&password and to get Cookie.
    Using HttpQueryInfo to get Cookie data from the handle of HttpOpenRequest after calling
    HttpSendRequest successfully. With the Cookie data, again I want to connect to the HTTPS web
    sever to Upload a file.
    The above whole concept is working fine in VC++ 6.0, Win2000 Prof. But, HttpQueryInfo fails
    to get Cookie data when I use the same code for Pocket PC 2002 with Windows CE and eVC++ 3.0.
    Even if HttpQueryInfo succeeds, it is not retrieving the proper Cookie data.
    I want to know whether Windows CE & Pocket PC 2000 supports HttpQueryInfo API. If it supports,
    Pls let me know the proper code.
    Pls help me.

    Thanks and Regards,
    Shan ( shanpalaniram@rediffmail.com )

  • Non-english characters?!

    Posted by Legacy on 06/19/2003 07:00am

    Originally posted by: znakeeye

    I have a simple question. How can you download a web page containing non-english characters without getting garbage text?

    I have a web page containing the letters '�', '�', and '�' and also the acute accent '�'. The accent looks like "´" when it is received in the buffer. WHY? The same goes for the Swedish letters above.

    Please, anyone! This is driving me nuts!


  • What about parsing content

    Posted by Legacy on 02/19/2003 08:00am

    Originally posted by: alan

    How would you find or extract something from PageContent?

    I noticet that if the URL source is very large CString::Find("text") doesn't work. It returns -1. Is this because PageContent doesn't hold large pages?

  • Internet

    Posted by Legacy on 09/20/2002 07:00am

    Originally posted by: idrees

    InternetWriteFile doesn't work and gives ERROR_INTERNET_INCORRECT_HANDLE_STATE error. I want to PUT data. HttpSendRequest(hHttpRequest, NULL, NULL, "Header", 6) works & write the word Header in the uploaded file but InternetWriteFile doesn't work. If any body can give a simple or modify my code so that it'll work. Following it the code.

    //////code starts
    void MyhttpSendRequestWrite(HWND hwnd){
    HINTERNET hInternetSession, hURL, hConnect, hHttpRequest;
    char buff[3000] = "This is the uploaded file";
    DWORD bytesWritten = 10, dwContext, err;

    hInternetSession = InternetOpen("Microsoft Internet Explorer",INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, INTERNET_FLAG_ASYNC);
    hConnect = InternetConnect(hInternetSession,"localhost", INTERNET_DEFAULT_HTTP_PORT,"idrees", NULL, INTERNET_SERVICE_HTTP, 0, 0);
    hHttpRequest = HttpOpenRequest(hConnect, "PUT", "pakistan/uploaded.txt", "1.1", "localhost", NULL, INTERNET_FLAG_HYPERLINK, dwContext); ///UltWeb/UltISAPI/ultISAPI.dll?default

    if(!HttpSendRequest(hHttpRequest, NULL, NULL, "Header", 6)){
    MessageBox(NULL,"HttpSendRequest Failed","http Failiur",MB_OK);
    short count=0;char tf, *errMsg;
    DWORD total=0;

    tf = InternetWriteFile(hHttpRequest, buff, sizeof(buff), &bytesWritten);
    err = GetLastError();
    errMsg = strerror(err);
    //TextOut(hdc,0,0,str,wsprintf(str," %10ld bytes written ",total));

    //////code ends

  • Sending binary data - POST form data

    Posted by Legacy on 07/10/2002 07:00am

    Originally posted by: Andy

    I am looking at how to send binary data - perhaps using the POST method to send form data to a server. So far I've not actually managed to work out how to do it.
    but ...

  • How to use HTTPQueryInfo with POST method

    Posted by Legacy on 07/09/2002 07:00am

    Originally posted by: Poonam Munshi


    how can I use HttpQueryInfo method to retrieve response headers after posting a file using POST method for the HttpRequest ?

    if i use the GET method then i can read response headers but cannot post files....so can anyone tell me how I can use HTTPQueryInfo and POST method together in my code to achieve both the things???


  • Can I do the Same without WinInet

    Posted by Legacy on 09/29/2001 07:00am

    Originally posted by: Mansoor Mohammed .S

    I want to create an application which can be executed as service and this is restricted while using <b>WinInet</b>, Do u have a solution for this. Where can I get more info about it?

  • Add http:// hypertext link in a dialog box

    Posted by Legacy on 07/26/2001 07:00am

    Originally posted by: Marc TERRIER

    I wish I could add a blue, underlined, hypertext link to
    a Web page in the About box of an application I'm writing.
    Does anyone know how I could do this ?

  • How to do PUT/POST with Chunked Encoding

    Posted by Legacy on 05/27/2001 07:00am

    Originally posted by: Dan Tran

    I would like to know the sequence of calls to WinInet to
    make a PUT/POST with chunked encoding



  • Loading, Please Wait ...

  • You must have javascript enabled in order to post comments.

Leave a Comment
  • Your email address will not be published. All fields are required.

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date