Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

System.Uri and encoded colon (:)

Before .Net 4.5, it seems that System.Uri would unencode encoded slashes, but this has since been fixed. Reference: https://stackoverflow.com/a/20733619/188740

I'm encountering the same problem with colons. System.Uri still unencodes encoded colons. Example:

        var uri = new Uri("http://www.example.com/?foo=http%3A%2F%2Fwww.example.com");
        var s = uri.ToString(); //http://www.example.com/?foo=http:%2F%2Fwww.example.com

Notice how %3A gets switched back to : by System.Uri. Is this a bug? What's the best workaround?

like image 719
Johnny Oshika Avatar asked May 04 '17 09:05

Johnny Oshika


People also ask

What is colon in Uri?

Colon IS an invalid character in URL unless it is used for its purpose (for eg http://). "...Only alphanumerics [0-9a-zA-Z], the special characters "$-_. +! *'()," [not including the quotes - ed], and reserved characters used for their reserved purposes may be used unencoded within a URL."

Can URI contain colon?

Colon is safe in the path part of a URI. In fact, all of these characters are safe, according to RFC3986: a-zA-Z0-9!$ &'()*+,;=:@~_. - However, in a relative path, you must use the ./ as in ./XYZ:ABCDEFG because a ':' may not be in the first part.

How do you get the colon out of the URL?

If you want to include a colon as it is within a REST query URL, escape it by url encoding it into %3A .

What is HTTP request colon?

The colon character is used to separate the protocol (e.g. http, ftp, etc.) from the server portion of your request.


1 Answers

How about using Uri.AbsoluteUri instead?

var s = uri.AbsoluteUri; 
// http://www.example.com/?foo=http%3A%2F%2Fwww.example.com

As per the source, uri.ToString() looks like it has logic to unescape certain parts which can be seen here whereas .AbsoluteUri has a much simpler implementation.

Uri.ToString()

As per the MSDN documentation for System.Uri.ToString():

A String instance that contains the unescaped canonical representation of the Uri instance. All characters are unescaped except #, ?, and %.

However as per the example and after trying out a few more strings, it looks like the actual implementation is somwhat like 'Only :, * and spaces are unescaped'

%3A (:) // gets unescaped
%20 ( ) // gets unescaped 
%2A (*) // gets unescaped

%2b, %26, %23, %24, %25 (+, &, #, $, %) // Remain as-is (escaped)

Other Links

  • System.Uri.AbsoluteUri
like image 188
degant Avatar answered Oct 11 '22 07:10

degant