|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Visual Basic Internet Routines UrlUnescape: Encoding and Decoding URL Escape Characters |
|
Posted: | Monday July 09, 2001 |
Updated: | Monday December 26, 2011 |
Applies to: | VB4-32, VB5, VB6 |
Developed with: | VB6, Windows 2000 |
OS restrictions: | See prerequisites below |
Author: | VBnet - Randy Birch |
Related: |
UrlCanonicalize: Proper URL Path Encoding and Decoding UrlCreateFromPath: Proper URL Path Conversion from a DOS Path UrlGetPart: Determine the Constituent Parts of a URL |
Prerequisites | ||||||||||||||||||||||||||||||
Shlwapi.dll version 5.00 or greater, Windows XP, 2000, Windows NT4 with IE 5 or later, Windows 98, or Windows 95 with IE 5 or later. | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
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:
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. |
||||||||||||||||||||||||||||||
BAS Module Code | ||||||||||||||||||||||||||||||
None. | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
Form Code | ||||||||||||||||||||||||||||||
To a form add two command buttons (Command1, Command2), and three text boxes (Text1, Text2, Text2), along with the following code: | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
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 |
||||||||||||||||||||||||||||||
Comments | ||||||||||||||||||||||||||||||
|
|
|
|||||
|
|||||
|
|||||
Copyright ©1996-2011 VBnet and Randy Birch. All Rights Reserved. |