gpt4 book ai didi

c# - System.Uri 和编码冒号 ( :)

转载 作者:太空狗 更新时间:2023-10-29 17:53:13 25 4
gpt4 key购买 nike

在 .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 切换回 :。这是一个错误吗?最好的解决方法是什么?

最佳答案

使用 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 文档:

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

然而,根据这个例子,在尝试了更多的字符串之后,看起来实际的实现有点像'Only :, * and spaces 未转义'

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

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

其他链接

关于c# - System.Uri 和编码冒号 ( :),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43778900/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com