📄 get_webpage.shtml
字号:
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<META NAME="Author" CONTENT="Zafir Anjum">
<TITLE>Internet - Get web page using WinInet class wrapper</TITLE>
</HEAD>
<body background="../fancyhome/back.gif" bgcolor="#FFFFFF" link="#B50029" vlink="#8E2323" alink="#FF0000">
<table WIDTH="100%">
<tr WIDTH="100%">
<td align=center><!--#exec cgi="/cgi/ads.cgi"--><td>
</tr>
</table>
<CENTER><H3><FONT COLOR="#AOAO99">Get web page using WinInet class wrapper</FONT></H3></CENTER>
<HR>
<p>This article was contributed by <a href="mailto:aasif@khi.compol.com">Asif Rasheed</a>.
</p>
<p>WinInet is a high-level interface to the more complicated underlying Internet protocols
(including HTTP, FTP, and Gopher).<br>
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.</p>
<p>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.</p>
<p>This class has two methods,</p>
<p><font color="#800000"> CString GetWebPage(const CString& Url);<br>
void SetErrorMessage(CString s);<br>
</font></p>
<p>GetWebPage method is used for accepting the url (it must me complete i.e.,
http:\\www.codeguru.com) and returning the desired page.</p>
<p>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.</p>
<p><font color="#800000"> </font></p>
<p><font color="#800000">/*<br>
//------------------------------------------------------------------------------------------------------------------<br>
// WebWorld.h: interface for the CWebWorld class.<br>
//------------------------------------------------------------------------------------------------------------------<br>
*/</font></p>
<p><font color="#800000">#include "wininet.h"<br>
<br>
class CWebWorld <br>
{<br>
public:<br>
void SetErrorMessage(CString s);<br>
CString GetWebPage(const CString& Url);<br>
CWebWorld();<br>
virtual ~CWebWorld();<br>
<br>
private:<br>
CString m_ErrorMessage;<br>
HINTERNET m_Session;<br>
};<br>
</font><br>
</p>
<p><font color="#800000">/*<br>
//------------------------------------------------------------------------------------------------------------------<br>
// WebWorld.cpp: implementation of the CWebWorld class.<br>
//------------------------------------------------------------------------------------------------------------------<br>
*/</font></p>
<p><font color="#800000">#include "stdafx.h"<br>
#include "WebThief.h"<br>
</font></p>
<p><font color="#800000">#ifdef _DEBUG<br>
#undef THIS_FILE<br>
static char THIS_FILE[]=__FILE__;<br>
#define new DEBUG_NEW<br>
#endif<br>
<br>
#define AGENT_NAME "CodeguruBrowser1.0"<br>
<br>
//////////////////////////////////////////////////////////////////////<br>
// Construction/Destruction<br>
//////////////////////////////////////////////////////////////////////<br>
<br>
CWebWorld::CWebWorld()<br>
{<br>
DWORD dwError;<br>
<br>
// Initialize the Win32 Internet functions <br>
m_Session = ::InternetOpen(AGENT_NAME, <br>
INTERNET_OPEN_TYPE_PRECONFIG, // Use registry
settings. <br>
NULL, // Proxy name. NULL indicates use
default.<br>
NULL, // List of local servers. NULL indicates
default. <br>
0) ;<br>
<br>
dwError = GetLastError();<br>
}<br>
<br>
CWebWorld::~CWebWorld()<br>
{<br>
// Closing the session<br>
::InternetCloseHandle(m_Session);<br>
}<br>
<br>
CString CWebWorld::GetWebPage(const CString& Url)<br>
{<br>
HINTERNET hHttpFile;<br>
char szSizeBuffer[32];<br>
DWORD dwLengthSizeBuffer = sizeof(szSizeBuffer); <br>
DWORD dwFileSize;<br>
DWORD dwBytesRead;<br>
BOOL bSuccessful;<br>
CString Contents;<br>
<br>
// Setting default error message<br>
Contents = m_ErrorMessage;<br>
<br>
// Opening the Url and getting a Handle for HTTP file<br>
hHttpFile = InternetOpenUrl(m_Session, (const char *) Url, NULL, 0, 0,
0);<br>
<br>
if (hHttpFile)<br>
{ <br>
// Getting the size of HTTP Files<br>
BOOL bQuery =
::HttpQueryInfo(hHttpFile,HTTP_QUERY_CONTENT_LENGTH, szSizeBuffer,
&dwLengthSizeBuffer, NULL) ;<br>
<br>
if(bQuery==TRUE)<br>
{ <br>
// Allocating the
memory space for HTTP file contents<br>
dwFileSize=atol(szSizeBuffer);<br>
LPSTR szContents =
Contents.GetBuffer(dwFileSize);<br>
<br>
// Read the HTTP file <br>
BOOL bRead =
::InternetReadFile(hHttpFile, szContents, dwFileSize, &dwBytesRead); <br>
<br>
if (bRead) <br>
bSuccessful = TRUE;<br>
<br>
::InternetCloseHandle(hHttpFile); // Close the connection.<br>
}<br>
<br>
}<br>
else<br>
{<br>
// Connection failed.<br>
bSuccessful = FALSE;<br>
} <br>
return Contents;<br>
}<br>
<br>
void CWebWorld::SetErrorMessage(CString s)<br>
{<br>
m_ErrorMessage = s;<br>
}</font><br>
</p>
<p>Following is a use of above class.</p>
<p><font color="#800000"> CWebWorld a; <br>
CString PageContent;</font></p>
<p><font color="#800000"> a.SetErrorMessage("There is some error in
getting web page ... ");<br>
PageContent = a.GetWebPage(m_Url);</font><br>
</p>
<P>
<P>
<HR>
<TABLE BORDER=0 WIDTH="100%" >
<TR>
<TD WIDTH="33%"><FONT SIZE=-1><A HREF="http://www.codeguru.com">Goto HomePage</A></FONT></TD>
<TD WIDTH="33%">
<CENTER><FONT SIZE=-2>© 1998 Zafir Anjum</FONT> </CENTER>
</TD>
<TD WIDTH="34%">
<DIV ALIGN=right><FONT SIZE=-1>Contact me: <A HREF="mailto:zafir@home.com">zafir@home.com</A> </FONT></DIV>
</TD>
</TR>
</TABLE>
</BODY>
</HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -