如何在C#中对字符串进行URL编码
我们如何使用C#中的URL(RFC 1738)标准对字符串进行编码?
以下在线工具使用此标准转换字符串http://www.freeformatter.com/url-encoder.html
我想要转换的字符串的一个示例是test(brackets)
,编码的字符串应如下所示:
test%28brackets%29
根据RFC 1738 :
Thus, only alphanumerics, the special characters "$-_.+!*'(),", and reserved characters used for their reserved purposes may be used unencoded within a URL.
HttpUtility.UrlEncode
和WebUtility.UrlEncode
都不会编码这些字符,因为标准说括号()
可以使用未编码的。
我不知道为什么你链接的URL编码器/解码器对它们进行编码,因为它还将它们列为可以在URL中使用的字符。
Uri.EscapeDataString
做你想要的。 请参阅MSDN 。