Sam wpadłem na ten problem i zamiast dodawać do mojego projektu zestaw System.Web, napisałem klasę do kodowania/dekodowania adresów URL (jest to bardzo proste, a ja zrobiłem kilka testów, ale nie wiele). Załączam kod źródłowy poniżej. Proszę: zostaw komentarz na górze, jeśli użyjesz tego ponownie, nie obwiniaj mnie, jeśli się złamie, ucz się z kodu.
''' <summary>
''' URL encoding class. Note: use at your own risk.
''' Written by: Ian Hopkins (http://www.lucidhelix.com)
''' Date: 2008-Dec-23
''' </summary>
Public Class UrlHelper
Public Shared Function Encode(ByVal str As String) As String
Dim charClass = String.Format("0-9a-zA-Z{0}", Regex.Escape("-_.!~*'()"))
Dim pattern = String.Format("[^{0}]", charClass)
Dim evaluator As New MatchEvaluator(AddressOf EncodeEvaluator)
' replace the encoded characters
Return Regex.Replace(str, pattern, evaluator)
End Function
Private Shared Function EncodeEvaluator(ByVal match As Match) As String
' Replace the " "s with "+"s
If (match.Value = " ") Then
Return "+"
End If
Return String.Format("%{0:X2}", Convert.ToInt32(match.Value.Chars(0)))
End Function
Public Shared Function Decode(ByVal str As String) As String
Dim evaluator As New MatchEvaluator(AddressOf DecodeEvaluator)
' Replace the "+"s with " "s
str = str.Replace("+"c, " "c)
' Replace the encoded characters
Return Regex.Replace(str, "%[0-9a-zA-Z][0-9a-zA-Z]", evaluator)
End Function
Private Shared Function DecodeEvaluator(ByVal match As Match) As String
Return "" + Convert.ToChar(Integer.Parse(match.Value.Substring(1), System.Globalization.NumberStyles.HexNumber))
End Function
End Class
Nie wiem, dlaczego został poddany pod głosowanie; może ta edycja coś naprawiła? W każdym razie jest to dobra odpowiedź. Aby uzyskać więcej informacji, zobacz [odpowiedź] (http://stackoverflow.com/a/8451941/530545) z innego podobnego pytania. – Granger
Działa wspaniale i nie trzeba dodawać żadnych odwołań. –
dziękuję za udzielenie odpowiedzi, a NIE za odpowiedź. – sonjz