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
#pragma once
#pragma comment(lib, "wininet.lib")
#include "wininet.h"
class CURLComponents : public URL_COMPONENTS
{
public:
CURLComponents(void)
{
memset(this, 0, sizeof(URL_COMPONENTS));
dwStructSize = sizeof(URL_COMPONENTS);
}
};
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)
{
}
bool Split(LPCTSTR lpsz)
{
ATLASSERT(lpsz != NULL && *lpsz != '\0');
DWORD dwLength = _tcslen(lpsz);
CURLComponents url;
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;
bool bRet = InternetCrackUrl(lpsz, 0, 0, &url) != FALSE;
m_strScheme.ReleaseBuffer();
m_strHostName.ReleaseBuffer();
m_strUserName.ReleaseBuffer();
m_strPassword.ReleaseBuffer();
m_strURLPath.ReleaseBuffer();
m_strExtraInfo.ReleaseBuffer();
m_nScheme = url.nScheme;
m_nPort = url.nPort;
return bRet;
}
inline INTERNET_SCHEME GetScheme(void) const { return m_nScheme; }
inline INTERNET_PORT GetPort(void) const { return m_nPort; }
inline LPCTSTR GetSchemeName(void) const { return m_strScheme; }
inline LPCTSTR GetHostName(void) const { return m_strHostName; }
inline LPCTSTR GetUserName(void) const { return m_strUserName; }
inline LPCTSTR GetPassword(void) const { return m_strPassword; }
inline LPCTSTR GetURLPath(void) const { return m_strURLPath; }
inline LPCTSTR GetExtraInfo(void) const { return m_strExtraInfo; }
};