Click here to Skip to main content
12,898,174 members (57,684 online)
Click here to Skip to main content
Add your own
alternative version

Stats

101.3K views
38 bookmarked
Posted 10 Nov 2002

CSplitURL - split a URL into component parts

, 10 Nov 2002
Rate this:
Please Sign up or sign in to vote.
Wrapper class for the WinInet InternetCrackUrl function
<!-- Download Links --> <!-- Article image -->

Sample Image - SplitURL.jpg

<!-- Add the rest of your HTML here -->

Introduction

Recently I needed some code that would split a URL into component parts (scheme, host, folder, etc.) and I came across a WinInet function called InternetCrackUrl which does the job. The function itself is fairly straighforward, but I thought the demo supplied on the MSDN might be a tad confusing for new users - so I have created CSplitURL to wrap the call.

This class is not going to win any prizes, but it is very easy to use - first include the header:

#include "url.h"

Then, to use the class, simply declare a CSplitURL object, passing the URL to split, e.g.:

CSplitURL url(_T("http://www.codeproject.com"));

Once you have created a CSplitURL, you can access the various URL components using the following methods:

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

All pretty self-explanatory. Note that using this class in your application will create a dependency on WININET.DLL, which hopefully won't be a problem. Note also that this class can be used with any framework that includes a CString class - including MFC, WTL and 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; }
};

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author


You may also be interested in...

Comments and Discussions

 
GeneralGreat class Pin
lekrot10-Aug-06 13:57
memberlekrot10-Aug-06 13:57 
GeneralRe: Great class Pin
Robert Edward Caldecott10-Aug-06 22:32
memberRobert Edward Caldecott10-Aug-06 22:32 
GeneralGreat Article Pin
ThatsAlok22-Sep-04 19:55
memberThatsAlok22-Sep-04 19:55 
GeneralRe: Great Article Pin
Robert Edward Caldecott22-Sep-04 20:38
memberRobert Edward Caldecott22-Sep-04 20:38 
GeneralCUrl in ATL 7.0 atlutil.h Pin
Norm Almond12-Nov-02 9:52
memberNorm Almond12-Nov-02 9:52 
GeneralRe: CUrl in ATL 7.0 atlutil.h Pin
Robert Edward Caldecott12-Nov-02 21:56
memberRobert Edward Caldecott12-Nov-02 21:56 
GeneralRe: CUrl in ATL 7.0 atlutil.h Pin
Bjornar Henden30-Nov-02 6:03
memberBjornar Henden30-Nov-02 6:03 
GeneralExtra feature request Pin
Chris Hambleton12-Nov-02 6:27
memberChris Hambleton12-Nov-02 6:27 
GeneralRe: Extra feature request Pin
Robert Edward Caldecott12-Nov-02 6:38
memberRobert Edward Caldecott12-Nov-02 6:38 
GeneralRe: Extra feature request Pin
Houdini13-Nov-02 5:50
memberHoudini13-Nov-02 5:50 
GeneralAmazing Pin
Adrian Edmonds11-Nov-02 18:37
memberAdrian Edmonds11-Nov-02 18:37 
GeneralRe: Amazing Pin
Anonymous11-Nov-02 18:44
sussAnonymous11-Nov-02 18:44 
GeneralRe: Amazing Pin
Adrian Edmonds11-Nov-02 19:36
memberAdrian Edmonds11-Nov-02 19:36 
GeneralRe: Amazing Pin
Anonymous11-Nov-02 20:16
sussAnonymous11-Nov-02 20:16 
GeneralRe: Amazing Pin
Robert Edward Caldecott11-Nov-02 22:01
memberRobert Edward Caldecott11-Nov-02 22:01 
GeneralGreat Pin
SRK11-Nov-02 15:33
sussSRK11-Nov-02 15:33 
GeneralCool Pin
Uwe Keim11-Nov-02 6:13
sitebuilderUwe Keim11-Nov-02 6:13 
GeneralRe: Cool Pin
Robert Edward Caldecott11-Nov-02 6:16
memberRobert Edward Caldecott11-Nov-02 6:16 
QuestionAfxParseUrl? Pin
twask11-Nov-02 3:54
membertwask11-Nov-02 3:54 
AnswerRe: AfxParseUrl? Pin
Robert Edward Caldecott11-Nov-02 4:04
memberRobert Edward Caldecott11-Nov-02 4:04 
GeneralRe: AfxParseUrl? Pin
twask11-Nov-02 4:05
membertwask11-Nov-02 4:05 
Jokehttp://camp.tangoing.info/ Pin
http://camp.tangoing.info/4-Dec-07 15:21
susshttp://camp.tangoing.info/4-Dec-07 15:21 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170424.1 | Last Updated 11 Nov 2002
Article Copyright 2002 by Rob Caldecott
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid