Public Class Class1 ' *********************************************************************** ' CLASS : clsCoder.cls ' PURPOSE : Provide access to the URL Coding / Decoding routines ' WRITTEN BY : Alon Hirsch ' COMPANY : Debtpack (Pty) Ltd. - Development ' DATE : 11 February 2002 ' converted from VB6 to VB.Net by Herman Wiegman 2023 (removed the byte conversions, ChrB and AscB) ' characters allowed in a URL without needing to be encoded Private Const URLValid = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" Public Function sURLEncode(sWork As String) As String ' check if we have a string to work with If Len(sWork) > 0 Then ' we do - determine the length of the string Dim iLen As Integer = Len(sWork) ' prepare the result string Dim sRet As String = "" ' This function will URLEncode sWork and return it as the value of the function ' check all the characters (one by one) For iLoop = 1 To iLen ' check each character in turn ' get the next character Dim sTemp As String = Mid$(sWork, iLoop, 1) ' is the character a valid one or not If InStr(1, URLValid, sTemp, vbBinaryCompare) = 0 Then If sTemp = Chr(32) Then ' convert space to + sTemp = "+" Else ' not valid - use HEX representation of it sTemp = "%" & Right$("0" & Hex(Asc(sTemp)), 2) End If End If ' add this to the returned string sRet &= sTemp Next iLoop ' return the final result sURLEncode = sRet Else sURLEncode = nothing End If End Function Public Function sURLDecode(sWork As String) As String Dim lPos2 As Long = 1 Dim bFirst As Boolean = True ' start by replacing all + with spaces sWork = Replace(sWork, "+", Chr(32)) ' *** now handle the actuall encoded stuff ' find the first occurrence Dim lPos1 As Long = InStr(1, sWork, "%", vbTextCompare) ' This function will scan through the entire sWork and replace all valid ' URL Encoded character with their ASCII character value ' start with an empty string Dim sTemp As String = "" If lPos1 = 0 Then ' none found - return the entire string sTemp = sWork Else ' check as long as there are still encoeded characters. Do While lPos1 <> 0 ' find the first % ' check if we found one or not If lPos1 <> 0 Then ' we found 1 - decode it and add it to the result If bFirst Then ' this is the first time in - stemp is all data up to the first % sTemp = Left$(sWork, lPos1 - 1) bFirst = False Else ' add all the data from the last position to the current position sTemp &= Mid$(sWork, lPos2 + 2, lPos1 - lPos2 - 2) End If Dim sChar As String = Mid$(sWork, lPos1 + 1, 2) Dim lChar As Long = "&H" & sChar sTemp &= Chr(lChar) ' start at the next position lPos2 = lPos1 + 1 End If ' check for the next one lPos1 = InStr(lPos2, sWork, "%", vbTextCompare) If lPos1 = 0 Then ' no more - add the rest of the string to be checked sTemp &= Mid$(sWork, lPos2 + 2) End If Loop End If ' return the string we have decoded sURLDecode = sTemp End Function End Class