' ------------------------------------------------------------------------------------- ' Name: URLEncode ' Author: Markus Diersbock ' Created: 09/26/2002 ' ' Description: Encodes a string to create legally formatted ' QueryString for URL. ' ' Arguments: sRawURL - String to Encode ' Returns: Encoded String ' Notes: This function is more flexible than the IIS Server.URLEncode ' function because you can pass in the WHOLE URL and only ' the QueryString data will be converted. ' ------------------------------------------------------------------------------------- Public Function URLEncode(sRawURL As String) As String On Error GoTo Catch Dim iLoop As Integer Dim sRtn As String Dim sTmp As String Dim sValidChars As String sValidChars = "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz$,*!@:/.?=_-$(){}~&" If Len(sRawURL) > 0 Then ' Loop through each char For iLoop = 1 To Len(sRawURL) sTmp = Mid(sRawURL, iLoop, 1) If InStr(1, sValidChars, sTmp, vbBinaryCompare) = 0 Then ' If not ValidChar, convert to HEX and prefix with % sTmp = Hex(Asc(sTmp)) If sTmp = "20" Then sTmp = "+" ElseIf Len(sTmp) = 1 Then sTmp = "%0" & sTmp Else sTmp = "%" & sTmp End If End If sRtn = sRtn & sTmp Next URLEncode = sRtn End If Finally: Exit Function Catch: URLEncode = "" Resume Finally End Function