Uri.EscapeUriString with square braces
Asked Answered
R

3

9

Something of a strange question but let's see what kind of response it gets...

If I code a console app (VS 2013, .NET 4.5.1) and execute this line of code:

Uri.EscapeUriString("[")

I get this:

[

However if I execute the same thing (well, technically Uri.EscapeUriString("[").Dump()) in LINQPad on my machine I get this:

%5B

To further complicate things, according to this post Uri.EscapeUriString("[") should indeed return %5B.The post was written on 27/06/2012.

I'm thinking that perhaps LINQPad is referencing an older DLL than that used by VS, but that would imply that EscapeUriString has changed relatively recently, which I cannot find any record of. Does anyone have any ideas as to what could be causing this behaviour?

Rechaba answered 15/11, 2013 at 14:17 Comment(1)
WT? I can repro in 4.5.1!Ketone
F
6

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.

Fults answered 15/11, 2013 at 14:26 Comment(0)
K
2

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      = "{" | "}" | "|" | "\" | "^" | "[" | "]" | "`"
571. 
572.    Data corresponding to excluded characters must be escaped in order to
573.    be properly represented within a URI.

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

Ketone answered 15/11, 2013 at 14:26 Comment(1)
Incorrect. Up to .NET 4.0, RFC 2396 is followed correctly. The characters that are not escaped are listed on line 469, not 570. dot Net 4.5.1 actually follows the characters listed in the same section of RFC 3986, which are different.Recrudesce
M
0

Never use Uri.EscapeUriString() nor HttpUtility.UrlEncode() for encoding data passed on an URL

Use this instead:

Uri.EscapeDataString("[")

This does the correct thing and returns:

%5B

This works on both .NET >= 4.NET Core

Mattie answered 22/7, 2021 at 10:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.