Part
of the Windows shell provides a series or Shell Lightweight Utility APIs to target specific shell functionality. UrlEscape and UrlUnescape
are used to encode (and decode) a URL containing escape sequences, converting unsafe characters, such as spaces, into their corresponding escape sequences. Unsafe characters are those characters that may be altered during transport across the internet.
UrlEscape converts unsafe characters into their equivalent "%xy" escape
sequences; UrlUnescape converts them back.
When a URL containing spaces or other escape characters is passed to
UrlEscape, the returned string has the escape characters inserted into the string. A URL that might contain unsafe spaces, such as
"\vbnet code library\" is
converted to the browser-kind "\vbnet%20code%20library\". Passing an encoded string to UrlUnescape converts the escape characters back to
user-friendly form.
Several flags are provided to customize the behaviour of UrlEscape
and UrlUnescape:
Flag |
|
Description |
|
Applies To |
URL_ESCAPE_SPACES_ONLY |
|
Only escape space characters. This flag cannot be combined with
URL_ESCAPE_PERCENT or URL_ESCAPE_SEGMENT_ONLY. |
|
UrlEscape |
URL_ESCAPE_PERCENT |
|
Escape the % character. By default, this character is not escaped. |
|
UrlEscape |
URL_ESCAPE_SEGMENT_ONLY |
|
Escape the sections following the server component, but not the
extra information sections following a # or ? character. |
|
UrlEscape |
URL_DONT_ESCAPE_EXTRA_INFO |
|
Don't convert the # or ? character, or any characters following them
in the string. |
|
UrlEscape, UrlUnescape |
URL_UNESCAPE_INPLACE |
|
Use pszURL to return the converted string instead of pszUnescaped. |
|
UrlUnescape |
The string passed to UrlEscape and UrlUnescape can be any URL or file
string; they do not require a http:// or file:// prefix to work.
|
Option Explicit
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Copyright ©1996-2011 VBnet/Randy Birch, All Rights Reserved.
' Some pages may also contain other copyrights by the author.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Distribution: You can freely use this code in your own
' applications, but you may not reproduce
' or publish this code on any web site,
' online service, or distribute as source
' on any media without express permission.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Private Const MAX_PATH As Long = 260
Private Const ERROR_SUCCESS As Long = 0
'Treat entire URL param as one URL segment
Private Const URL_ESCAPE_SEGMENT_ONLY As Long = &H2000
Private Const URL_ESCAPE_PERCENT As Long = &H1000
Private Const URL_UNESCAPE_INPLACE As Long = &H100000
'escape #'s in paths
Private Const URL_INTERNAL_PATH As Long = &H800000
Private Const URL_DONT_ESCAPE_EXTRA_INFO As Long = &H2000000
Private Const URL_ESCAPE_SPACES_ONLY As Long = &H4000000
Private Const URL_DONT_SIMPLIFY As Long = &H8000000
'Converts unsafe characters,
'such as spaces, into their
'corresponding escape sequences.
Private Declare Function UrlEscape Lib "shlwapi" _
Alias "UrlEscapeA" _
(ByVal pszURL As String, _
ByVal pszEscaped As String, _
pcchEscaped As Long, _
ByVal dwFlags As Long) As Long
'Converts escape sequences back into
'ordinary characters.
Private Declare Function UrlUnescape Lib "shlwapi" _
Alias "UrlUnescapeA" _
(ByVal pszURL As String, _
ByVal pszUnescaped As String, _
pcchUnescaped As Long, _
ByVal dwFlags As Long) As Long
Private Sub Form_Load()
'fake address - don't try to navigate to it
Text1.Text = "http://vbnet.mvps.org/vbnet code lib/net code/ip address.htm"
Text2.Text = ""
Text3.Text = ""
Command1.Caption = "Encode"
Command2.Caption = "Decode"
End Sub
Private Sub Command1_Click()
Dim sUrl As String
Dim buff As String
'use the original string in Text1 for
'demo, and show encoded results in Text2
sUrl = Text1.Text
buff = EncodeUrl(sUrl)
Text2.Text = buff
End Sub
Private Sub Command2_Click()
Dim sUrl As String
Dim buff As String
'use the encoded string in text2 for
'demo, and show decoded results in Text3
sUrl = Text2.Text
buff = DecodeUrl(sUrl)
Text3.Text = buff
End Sub
Private Function EncodeUrl(ByVal sUrl As String) As String
Dim buff As String
Dim dwSize As Long
Dim dwFlags As Long
If Len(sUrl) > 0 Then
buff = Space$(MAX_PATH)
dwSize = Len(buff)
dwFlags = URL_DONT_SIMPLIFY
If UrlEscape(sUrl, _
buff, _
dwSize, _
dwFlags) = ERROR_SUCCESS Then
EncodeUrl = Left$(buff, dwSize)
End If 'UrlEscape
End If 'Len(sUrl)
End Function
Private Function DecodeUrl(ByVal sUrl As String) As String
Dim buff As String
Dim dwSize As Long
Dim dwFlags As Long
If Len(sUrl) > 0 Then
buff = Space$(MAX_PATH)
dwSize = Len(buff)
dwFlags = URL_DONT_SIMPLIFY
If UrlUnescape(sUrl, _
buff, _
dwSize, _
dwFlags) = ERROR_SUCCESS Then
DecodeUrl = Left$(buff, dwSize)
End If 'UrlUnescape
End If 'Len(sUrl)
End Function |