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

2024-02-04 23:32

本文主要是介绍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);

来源: http://www.kehui.net/html/article/42/42928.html

这篇关于VC++中使用使用winnet类获取网页内容的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/679129

相关文章

JavaScript中的reduce方法执行过程、使用场景及进阶用法

《JavaScript中的reduce方法执行过程、使用场景及进阶用法》:本文主要介绍JavaScript中的reduce方法执行过程、使用场景及进阶用法的相关资料,reduce是JavaScri... 目录1. 什么是reduce2. reduce语法2.1 语法2.2 参数说明3. reduce执行过程

如何使用Java实现请求deepseek

《如何使用Java实现请求deepseek》这篇文章主要为大家详细介绍了如何使用Java实现请求deepseek功能,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1.deepseek的api创建2.Java实现请求deepseek2.1 pom文件2.2 json转化文件2.2

python使用fastapi实现多语言国际化的操作指南

《python使用fastapi实现多语言国际化的操作指南》本文介绍了使用Python和FastAPI实现多语言国际化的操作指南,包括多语言架构技术栈、翻译管理、前端本地化、语言切换机制以及常见陷阱和... 目录多语言国际化实现指南项目多语言架构技术栈目录结构翻译工作流1. 翻译数据存储2. 翻译生成脚本

C++初始化数组的几种常见方法(简单易懂)

《C++初始化数组的几种常见方法(简单易懂)》本文介绍了C++中数组的初始化方法,包括一维数组和二维数组的初始化,以及用new动态初始化数组,在C++11及以上版本中,还提供了使用std::array... 目录1、初始化一维数组1.1、使用列表初始化(推荐方式)1.2、初始化部分列表1.3、使用std::

C++ Primer 多维数组的使用

《C++Primer多维数组的使用》本文主要介绍了多维数组在C++语言中的定义、初始化、下标引用以及使用范围for语句处理多维数组的方法,具有一定的参考价值,感兴趣的可以了解一下... 目录多维数组多维数组的初始化多维数组的下标引用使用范围for语句处理多维数组指针和多维数组多维数组严格来说,C++语言没

在 Spring Boot 中使用 @Autowired和 @Bean注解的示例详解

《在SpringBoot中使用@Autowired和@Bean注解的示例详解》本文通过一个示例演示了如何在SpringBoot中使用@Autowired和@Bean注解进行依赖注入和Bean... 目录在 Spring Boot 中使用 @Autowired 和 @Bean 注解示例背景1. 定义 Stud

使用 sql-research-assistant进行 SQL 数据库研究的实战指南(代码实现演示)

《使用sql-research-assistant进行SQL数据库研究的实战指南(代码实现演示)》本文介绍了sql-research-assistant工具,该工具基于LangChain框架,集... 目录技术背景介绍核心原理解析代码实现演示安装和配置项目集成LangSmith 配置(可选)启动服务应用场景

使用Python快速实现链接转word文档

《使用Python快速实现链接转word文档》这篇文章主要为大家详细介绍了如何使用Python快速实现链接转word文档功能,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 演示代码展示from newspaper import Articlefrom docx import

oracle DBMS_SQL.PARSE的使用方法和示例

《oracleDBMS_SQL.PARSE的使用方法和示例》DBMS_SQL是Oracle数据库中的一个强大包,用于动态构建和执行SQL语句,DBMS_SQL.PARSE过程解析SQL语句或PL/S... 目录语法示例注意事项DBMS_SQL 是 oracle 数据库中的一个强大包,它允许动态地构建和执行

SpringBoot中使用 ThreadLocal 进行多线程上下文管理及注意事项小结

《SpringBoot中使用ThreadLocal进行多线程上下文管理及注意事项小结》本文详细介绍了ThreadLocal的原理、使用场景和示例代码,并在SpringBoot中使用ThreadLo... 目录前言技术积累1.什么是 ThreadLocal2. ThreadLocal 的原理2.1 线程隔离2