Uri.EscapeUriString with square braces

橙三吉。 提交于 2019-12-05 03:12:44

This changed between .Net 4 and .Net 4.5, which you can verify by retargeting the framework version to .Net 4 and running the test program.

.Net 4 -> outputs "%5B" .net 4.5 (or later) -> outputs "["

This is mentioned here: Application Compatibility in the .NET Framework 4.5

under the section for Uri.EscapeDataString , Uri.EscapeUriString, and Uri.UnescapeDataString, which states that (with .Net 4.5):

The list of reserved and unreserved characters now supports RFC 3986.

Specific changes:

Unreserved escaped characters are un-escaped.
EscapeDataString escapes reserved characters based on RFC 3986.
EscapeUriString does not escape reserved characters.
UnescapeDataString does not throw an exception if it encounters an invalid escape sequence.

In particular, it is the EscapeUriString does not escape reserved characters which is significant.

The new behavior seems to be the correct one according to RFC 2396. In line 566 it states:

566.    Other characters are excluded because gateways and other transport
567.    agents are known to sometimes modify such characters, or they are
568.    used as delimiters.
569. 
570.    unwise      = "{" | "}" | "|" | "\" | "^" | "[" | "]" | "`"

In Uri.EscapeUriString's documentation it states that

By default, the EscapeUriString method converts all characters, except RFC 2396 unreserved characters, to their hexadecimal representation

So it would seem that there was a bug up to .NET 4.0 that was fixed in 4.5.1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!