分享
 
 
 

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

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有