65.9K
CodeProject 正在变化。 阅读更多。
Home

CSplitURL - 将 URL 拆分成组成部分

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.17/5 (9投票s)

2002 年 11 月 11 日

viewsIcon

119260

downloadIcon

1807

WinInet InternetCrackUrl 函数的包装类。

Sample Image - SplitURL.jpg

引言

最近我需要一段代码,可以将 URL 分解为组成部分(方案、主机、文件夹等),我发现了一个 WinInet 函数,名为 InternetCrackUrl,它可以完成这项工作。该函数本身相当简单明了,但我认为 MSDN 提供的演示可能对新手来说有点令人困惑,所以我创建了 CSplitURL 来封装该调用。

这个类不会赢得任何奖项,但它非常易于使用 - 首先包含头文件

#include "url.h"

然后,要使用该类,只需声明一个 CSplitURL 对象,并将要分解的 URL 传递给它,例如:

CSplitURL url(_T("https://codeproject.org.cn"));

创建 CSplitURL 后,可以使用以下方法访问各种 URL 组件

  • GetScheme
  • GetPort
  • GetSchemeName
  • GetHostName
  • GetUserName
  • GetPassword
  • GetURLPath
  • GetExtraInfo

所有这些都非常容易理解。请注意,在您的应用程序中使用此类将创建对 WININET.DLL 的依赖,希望这不会成为问题。 另外请注意,此类可以与包含 CString 类的任何框架一起使用 - 包括 MFC、WTL 和 ATL7。

CSplitURL

// Implementation of the CURLComponents and CSplitURL classes.

#pragma once
#pragma comment(lib, "wininet.lib")

#include "wininet.h"

// Class to wrap the Win32 URL_COMPONENTS structure
class CURLComponents : public URL_COMPONENTS
{
public:    
    CURLComponents(void)
    {
        memset(this, 0, sizeof(URL_COMPONENTS));
        dwStructSize = sizeof(URL_COMPONENTS);
    }
};

// Class used to split a URL into component parts.
// Note: Uses WININET InternetCrackUrl function.
class CSplitURL
{
private:
    CString m_strScheme;
    INTERNET_SCHEME m_nScheme;
    CString m_strHostName;
    INTERNET_PORT m_nPort;
    CString m_strUserName;
    CString m_strPassword;
    CString m_strURLPath;
    CString m_strExtraInfo;
public:    
    CSplitURL(void)
        : m_nScheme(INTERNET_SCHEME_DEFAULT)
        , m_nPort(0)
    {
    }
    
    CSplitURL(LPCTSTR lpsz)
        : m_nScheme(INTERNET_SCHEME_DEFAULT)
        , m_nPort(0)
    {
        Split(lpsz);
    }

    ~CSplitURL(void)
    {
    }

    // Split a URL into component parts
    bool Split(LPCTSTR lpsz)
    {
        // Be defensive
        ATLASSERT(lpsz != NULL && *lpsz != '\0');
        // Get the URL length
        DWORD dwLength = _tcslen(lpsz);

        CURLComponents url;        
        // Fill structure
        url.lpszScheme = m_strScheme.GetBuffer(dwLength);
        url.dwSchemeLength = dwLength;
        url.lpszHostName = m_strHostName.GetBuffer(dwLength);
        url.dwHostNameLength = dwLength;
        url.lpszUserName = m_strUserName.GetBuffer(dwLength);
        url.dwUserNameLength = dwLength;
        url.lpszPassword = m_strPassword.GetBuffer(dwLength);
        url.dwPasswordLength = dwLength;
        url.lpszUrlPath = m_strURLPath.GetBuffer(dwLength);
        url.dwUrlPathLength = dwLength;
        url.lpszExtraInfo = m_strExtraInfo.GetBuffer(dwLength);
        url.dwExtraInfoLength = dwLength;
        // Split
        bool bRet = InternetCrackUrl(lpsz, 0, 0, &url) != FALSE;
        // Release buffers
        m_strScheme.ReleaseBuffer();
        m_strHostName.ReleaseBuffer();
        m_strUserName.ReleaseBuffer();
        m_strPassword.ReleaseBuffer();
        m_strURLPath.ReleaseBuffer();
        m_strExtraInfo.ReleaseBuffer();
        // Get the scheme/port
        m_nScheme = url.nScheme;
        m_nPort = url.nPort;
        // Done
        return bRet;
    }

    // Get the scheme number
    inline INTERNET_SCHEME GetScheme(void) const { return m_nScheme; }
    // Get the port number
    inline INTERNET_PORT GetPort(void) const { return m_nPort; }
    // Get the scheme name
    inline LPCTSTR GetSchemeName(void) const { return m_strScheme; }
    // Get the host name
    inline LPCTSTR GetHostName(void) const { return m_strHostName; }
    // Get the user name
    inline LPCTSTR GetUserName(void) const { return m_strUserName; }
    // Get the password
    inline LPCTSTR GetPassword(void) const { return m_strPassword; }
    // Get the URL path
    inline LPCTSTR GetURLPath(void) const { return m_strURLPath; }
    // Get the extra info
    inline LPCTSTR GetExtraInfo(void) const { return m_strExtraInfo; }
};
© . All rights reserved.