Util.UrlEncode Method
Encodes an input string to produce a valid URL string for use in the browser.
For instance, space characters and punctuation are not allowed in the URL, so
all spaces are converted to a plus sign and all
non-alphanumeric characters are converted to their hexadecimal value of format %nn.
The UrlEncode method produces the same output as Server.UrlEncode
in ASP. Usage:
You may use the UrlEncode method to encode a full URL, however
its main use is to encode the query string
values of the URL.
strName = "André & Hélène"
strURL = "http://www.myserver.com/login/?Name=" & Util.UrlEncode(strName)
Report.Write "<a href='" & strURL & "'>Login</a>"
' Output: <a href='http://www.myserver.com/login/?Name=Andr%C3%A9+%26+H%C3%A9l%C3%A8ne'>Login</a>
Double Encoding Warning:
The method UrlEncode should be used with care. An non-encoded
URL does produce an error, however a double-encoded URL does produce incorrect
result. For instance, the following piece of code shows the
double-encoding of an URL.
strURL = "https://www.genopro.com/?id=abc"
Report.WriteBr strURL
Report.WriteBr Util.UrlEncode(strURL)
Report.WriteBr Util.UrlEncode(Util.UrlEncode(strURL))
Report.WriteBr Util.HrefEncode(strURL)
Report.WriteBr Util.HrefEncode(Util.HrefEncode(strURL))
Report.WriteBr Util.HrefEncode(Util.UrlEncode(strURL))
' Output:
' https://www.genopro.com/?id=abc
' http%3A%2F%2Fwww%2Egenopro%2Ecom%2F%3Fid%3Dabc
' http%253A%252F%252Fwww%252Egenopro%252Ecom%252F%253Fid%253Dabc
'
' https://www.genopro.com/?id=abc
' https://www.genopro.com/?id=abc
' http%3A%2F%2Fwww%2Egenopro%2Ecom%2F%3Fid%3Dabc
As you can see, the double-encoding of the URL does produce a different
result. One must be careful not to double-encode in the following
scenario:
strName = "André & Hélène"
strURL = "http://www.myserver.com/login/?Name=" & Util.UrlEncode(strName)
Report.Write "<a href='" & Util.UrlEncode(strURL) & "'>Login</a>"
In this example, the strName has been encoded twice which is
incorrect. The safe solution to this is to use
HrefEncode, because
HrefEncode does not encode any
character if the URL is already valid.To see more samples of
URL encoding, please consult the method
HrefEncode.
See Also:
HrefEncode
|