VC++中使用使用winnet类获取网页内容

王朝vc·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

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

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航