发新话题
打印

[转载]VC++中使用使用winnet类获取网页内容

[转载]VC++中使用使用winnet类获取网页内容

文章来源:blog.csdn.net
作者blog:http://blog.csdn.net/cqq/archive/2005/02/08/284393.aspx

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);
我用青春赌明天

TOP

我自己写了一个抓网页的……唔……便宜好用量又足呢。
对待处女,谁污染,谁治理。 对待内存,谁分配,谁释放。

TOP

发新话题