根据 http://www.w3schools.com/tags/ref_urlencode.asp 当请求被提交时,它们是 URL 编码的,因此例如空间被转换为 %20。到目前为止一切顺利。
我对 ! 有疑问。以表单形式提交会将其转换为 %21。
但是 HttpUtility.UrlEncode
(或其 WebUtility
伙伴)或 Uri.EscapeDataString
都会返回!
这是预期的行为吗?我应该如何对来自 C# 的输入进行编码,以便将其转换为正确的值?
最佳答案
感叹号被认为是 URL 安全的 ASCII 字符,因此不是百分比编码。
来自 MSDN
The UrlEncode method URL-encodes any character that is not in the set of ASCII characters that is considered to be URL-safe. Spaces are encoded as the ASCII "+" character. URL-safe ASCII characters include the ASCI characters (A to Z and a to z), numerals (0 to 9), and some punctuation marks. The following table lists the punctuation marks that are considered URL-safe ASCII characters.
表格包含- _。 ! * ( )
更新
根据 this answer , Uri.EscapeDataString
应该在针对 .NET 4.5 项目时对 !
进行编码,但我无法在我当前的机器上对其进行测试。以前的 .NET 框架上的 EscapeDataString
不会对上述字符进行百分比编码。您可能只需要使用 String.Replace
并从转义的 URI 中替换上面的字符。
关于c# - .NET UrlEncode 不工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30932903/