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

📄 使用mfc的wininet 类得到web页.txt

📁 几个Internet网络编程的资料和源码
💻 TXT
字号:
使用MFC的WinInet 类得到WEB页

编译:刘峰

 WinInet是一个高层次的编程接口,包含了更为复杂的Internet 底层协议(HTTP,
FTP,GOPHER),它使用户在不非常了解底层协议的情况下,也能方便的编写Internet
应用程序。MFC提供了WinInet 封装类,包含了相应的WinInet API函数,它们
分布在不同的层次。下面是我编写的一个小类,通过它,你能很容易的根据指
定的URL地址来获得相应的WEB页。

这个类包括两个方法:

    CString GetWebPage(const CString& Url);
    void SetErrorMessage(CString s);


GetWebPage 方法根据指定的URL来获取网页(如:http://www.vcsky.net).

SetErrorMessage 方法用来接受错误信息。当发生错误时,GetWebpage 将返回错误信息。

/*
//------------------------------------------------------------------------------------------------------------------
// WebWorld.h: interface for the CWebWorld class.
//------------------------------------------------------------------------------------------------------------------
*/

#include "wininet.h"

class CWebWorld 
{
public:
    void SetErrorMessage(CString s);
    CString GetWebPage(const CString& Url);
    CWebWorld();
    virtual ~CWebWorld();

private:
    CString m_ErrorMessage;
    HINTERNET m_Session;
};



/*
//------------------------------------------------------------------------------------------------------------------
// WebWorld.cpp:  implementation of the CWebWorld class.
//------------------------------------------------------------------------------------------------------------------
*/

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

⌨️ 快捷键说明

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