asked on
Public Function URLEncode( _
ByVal URL As String, _
Optional ByVal SpacePlus As Boolean = True) As String
Dim cchEscaped As Long
Dim HRESULT As Long
If Len(URL) > INTERNET_MAX_URL_LENGTH Then
Err.Raise &H8004D700, "URLUtility.URLEncode", _
"URL parameter too long"
End If
cchEscaped = Len(URL) * 1.5
URLEncode = String$(cchEscaped, 0)
HRESULT = UrlEscape(URL, URLEncode, cchEscaped, URL_ESCAPE_PERCENT)
If HRESULT = E_POINTER Then
URLEncode = String$(cchEscaped, 0)
HRESULT = UrlEscape(URL, URLEncode, cchEscaped, URL_ESCAPE_PERCENT)
End If
If HRESULT <> S_OK Then
Err.Raise Err.LastDllError, "URLUtility.URLEncode", "System error"
End If
URLEncode = Left$(URLEncode, cchEscaped)
If SpacePlus Then
URLEncode = Replace$(URLEncode, "+", "%2B")
URLEncode = Replace$(URLEncode, " ", "+")
End If
End Function