⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cpp1.cpp

📁 VC++调用winnet类,实现网页数据抓取
💻 CPP
字号:
#include "wininet.h"  //包含wininet.h ,在mfc中也可以是afxinet.h

class cwebworld 
{
public:
    void seterrormessage(cstring s); //声明seterrormessage方法
    cstring getwebpage(const cstring&www.cnbb.com.cn); //声明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;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -