System.Uri and encoded colon (:)

后端 未结 1 1469
感情败类
感情败类 2021-01-04 18:53

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

相关标签:
1条回答
  • 2021-01-04 19:53

    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
    0 讨论(0)
提交回复
热议问题