【问题标题】:System.Uri and encoded colon (:)System.Uri 和编码冒号 (:)
【发布时间】:2017-10-02 09:33:18
【问题描述】:

在 .Net 4.5 之前,System.Uri 似乎会取消编码已编码的斜杠,但这已得到修复。参考:https://stackoverflow.com/a/20733619/188740

我在使用冒号时遇到了同样的问题。 System.Uri 仍然未编码已编码的冒号。示例:

        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

注意 %3A 如何被 System.Uri 切换回 :。这是一个错误吗?最好的解决方法是什么?

【问题讨论】:

    标签: c# .net uri urlencode


    【解决方案1】:

    改用Uri.AbsoluteUri 怎么样?

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

    根据来源,uri.ToString() 看起来有逻辑来取消转义某些部分,可以看到 here.AbsoluteUri 有一个更简单的implementation

    Uri.ToString()

    根据 System.Uri.ToString() 的 MSDN 文档:

    包含 Uri 实例的非转义规范表示的 String 实例。除 #、? 和 % 外,所有字符均未转义。

    但是,根据示例并尝试了更多字符串后,实际实现看起来有点像 '只有 :*spaces 未转义'

    %3A (:) // gets unescaped
    %20 ( ) // gets unescaped 
    %2A (*) // gets unescaped
    
    %2b, %26, %23, %24, %25 (+, &, #, $, %) // Remain as-is (escaped)
    

    其他链接

    【讨论】:

    • AbsoluteUri 就像一个魅力。为什么没有 AbsoluteUri 冒号会未编码?对我来说似乎是一个错误。
    • 是的,看起来像。我相信错误在某处 here 继续进行并且未转义某些部分。而.AbsoluteUri 有一个更简单的implementation
    猜你喜欢
    • 1970-01-01
    • 2020-03-20
    • 1970-01-01
    • 1970-01-01
    • 2012-04-21
    • 2013-06-13
    • 2023-04-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多