本文主要是介绍VC++中使用使用winnet类获取网页内容,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
微软提供的Winnet类是一个应用层的网络通信组件,它可以使你的应用程序很容易的实现http、ftp、gopher等协议而不需要你去深入的了解协议本身的规范。而之前,如果要想做类似的应用,我们必须了解socket编程并且要对协议本身非常熟悉,哪怕是一个非常非常简单的程序。
下面是codeguru上的一个使用wininet类的例子,它能够从给定的url地址中获取该文件。
这个例子实现了两个方法:
cstring getwebpage(const cstring& url);
void seterrormessage(cstring s);
getwebpage是主要的方法,后面跟的url地址必须是一个完整的url地址,
比如http://blog.csdn.net/cqq。
seterrormessage方法, 能够处理程序中发生的错误。
下面来看代码:
/*
//------------------------------------------------------------------------------------------------------------------
// webworld.h: interface for the cwebworld class.
//一个封装的类cwebworld,webworld.h是该类的头文件
//------------------------------------------------------------------------------------------------------------------
*/
#include "wininet.h" //包含wininet.h ,在mfc中也可以是afxinet.h
class cwebworld
{
public:
void seterrormessage(cstring s); //声明seterrormessage方法
cstring getwebpage(const cstring& url); //声明getwebpage方法
cwebworld(); //构造函数
virtual ~cwebworld();
private:
cstring m_errormessage;
hinternet m_session; //声明一个http连接的句柄
};
/*
//------------------------------------------------------------------------------------------------------------------
// webworld.cpp: implementation of the cwebworld class.
// cwebworld类的具体实现
//------------------------------------------------------------------------------------------------------------------
*/
#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;
}
下面是关于上面的类的使用方法:
cwebworld a;
cstring pagecontent;
a.seterrormessage("there is some error in getting web page ... ");
pagecontent = a.getwebpage(m_url);
来源: http://www.kehui.net/html/article/42/42928.html
这篇关于VC++中使用使用winnet类获取网页内容的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!