URLencode och URLdecode funktioner för Visual Basic

URLencode och URLdecode funktioner för Visual Basic

 

Public Function UrlEncode(ByVal s As String, Optional ByVal _
use_plus_for_space As Boolean = True) As String
Dim result As String = “”

For Each ch As String In s
Select Case Asc(ch)
Case 48 To 57, 65 To 90, 97 To 122
result &= ch
Case 32
If use_plus_for_space Then
result &= “+”
Else
result &= “%20”
End If
Case Else
result &= “%” & Hex(Asc(ch)).PadLeft(2, _
“0”c)
End Select
Next ch

Return result
End Function

Public Function UrlDecode(ByVal s As String, Optional ByVal _
use_plus_for_space As Boolean = True) As String
Dim result As String = “”

Dim i As Integer = 0
Do While i < s.Length
Dim ch As String = s.Substring(i, 1)

Select Case ch
Case “+”
If use_plus_for_space Then
result &= “+”
Else
result &= ” “
End If
i += 1
Case “%”
Dim num As Integer = _
Val(“&H” & s.Substring(i + 1, 2))
result &= Chr(num)
i += 3
Case Else
result &= ch
i += 1
End Select
Loop

Return result
End Function