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

Comments
can't identify url of a webpage-sending data using post method
Posted by ancy anna on 09/25/2004 02:46ami 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.
ReplyHttpQueryInfo API is not retrieving the proper Cookie data.
Posted by Legacy on 01/26/2004 12:00amOriginally posted by: Shanmugam
Hi,
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,
ReplyShan ( shanpalaniram@rediffmail.com )
Non-english characters?!
Posted by Legacy on 06/19/2003 12:00amOriginally 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!
/Chris
ReplyWhat about parsing content
Posted by Legacy on 02/19/2003 12:00amOriginally 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?
ReplyInternet
Posted by Legacy on 09/20/2002 12:00amOriginally 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);
return;
}
short count=0;char tf, *errMsg;
DWORD total=0;
tf = InternetWriteFile(hHttpRequest, buff, sizeof(buff), &bytesWritten);
err = GetLastError();
errMsg = strerror(err);
if(err)
MessageBox(hwnd,errMsg,"Error",MB_OK);
//TextOut(hdc,0,0,str,wsprintf(str," %10ld bytes written ",total));
InternetCloseHandle(hInternetSession);
}
//////code ends
ReplySending binary data - POST form data
Posted by Legacy on 07/10/2002 12:00amOriginally posted by: Andy
ReplyHow to use HTTPQueryInfo with POST method
Posted by Legacy on 07/09/2002 12:00amOriginally posted by: Poonam Munshi
hi...
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???
thanks
ReplyPoonam
Can I do the Same without WinInet
Posted by Legacy on 09/29/2001 12:00amOriginally 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?
ReplyAdd http:// hypertext link in a dialog box
Posted by Legacy on 07/26/2001 12:00amOriginally posted by: Marc TERRIER
I wish I could add a blue, underlined, hypertext link to
Replya 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 12:00amOriginally posted by: Dan Tran
I would like to know the sequence of calls to WinInet to
make a PUT/POST with chunked encoding
Thanks
-D
ReplyLoading, Please Wait ...