VC++中使用使用winnet类获取网页内容
来源:互联网 发布:安徽网络电视台海豚tv 编辑:程序博客网 时间:2024/05/22 09:09
微软提供的Winnet类是一个应用层的网络通信组件,
它可以使你的应用程序很容易的实现http、ftp、gopher等协议而不需要
你去深入的了解协议本身的规范。而之前,如果要想做类似的应用,我们
必须了解socket编程并且要对协议本身非常熟悉,哪怕是一个非常非常
简单的程序。
下面是codeguru上的一个使用wininet类的例子,它能够从给定的url地址中
获取该文件。
这个例子实现了两个方法:
cstring getwebpage(const cstring& url);
void seterrormessage(cstring s);
getwebpage是主要的方法,后面跟的url地址必须是一个完整的url地址,
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);
- VC++中使用使用winnet类获取网页内容
- VC++中使用使用winnet类获取网页内容
- VC++中使用使用winnet类获取网页内容
- Winnet获取网页HTML内容-Code
- VC中使用CInternetSession抓取网页内容
- VC中使用CInternetSession抓取网页内容
- VC中使用CInternetSession抓取网页内容
- VC中使用CInternetSession抓取网页内容
- Winnet Api 使用
- 使用php获取网页内容
- 使用MFC获取网页内容
- 使用php获取网页内容
- 使用HttpURLConnection获取网页内容
- 使用cURL获取网页内容
- VC获取网页内容
- VC获取网页内容
- 在WebView中使用JavaScript获取网页内容
- vc 获取网页内容(HTTP)
- 你最后悔的是什么
- 利用 ubuntu 中 syslog 记录外部设备日志
- firefox 在 verycd 中点击“复制选中的链接”不管用
- 转!抽象类和接口应该各司其职。
- NiceTrack.cn快递物流跟踪系统接口说明(JAVA版本)
- VC++中使用使用winnet类获取网页内容
- 第N次被ACCESS的关键字涮
- VC中使用CInternetSession抓取网页内容
- 关于VPN的一些介绍
- VC通过HTTP方式获取网页
- 。。。。。。。。。
- php获取网页内容
- 网通路由表 2008-1-17数据
- Java基础:利用HttpClient获取网页内容