"-_.!~*'()" Uri-6ren">
gpt4 book ai didi

c# - Uri.EscapeDataString 怪异

转载 作者:太空狗 更新时间:2023-10-29 22:02:05 25 4
gpt4 key购买 nike

为什么 EscapeDataString 在 .NET 4 和 4.5 之间的行为不同?输出是

  • Uri.EscapeDataString("-_.!~*'()") => "-_.!~*'()"

  • Uri.EscapeDataString("-_.!~*'()") => "-_.%21~%2A%27%28%29"

The documentation

By default, the EscapeDataString method converts all characters except for RFC 2396 unreserved characters to their hexadecimal representation. If International Resource Identifiers (IRIs) or Internationalized Domain Name (IDN) parsing is enabled, the EscapeDataString method converts all characters, except for RFC 3986 unreserved characters, to their hexadecimal representation. All Unicode characters are converted to UTF-8 format before being escaped.

供引用,非保留字符定义如下RFC 2396 :

unreserved    = alphanum | mark

mark = "-" | "_" | "." | "!" | "~" | "*" | "'" |
(" | ")"

并且在 RFC 3986 :

ALPHA / DIGIT / "-" / "." / "_" / "~"

The source code

EscapeDataString的每个字符是否转义貌似大概是这样判断的

is unicode above \x7F
? PERCENT ENCODE
: is a percent symbol
? is an escape char
? LEAVE ALONE
: PERCENT ENCODE
: is a forced character
? PERCENT ENCODE
: is an unreserved character
? PERCENT ENCODE

在 RFC2396 和 RFC3986 之间做出选择的最后检查“是一个未保留的字符”。方法源码一字不差

    internal static unsafe bool IsUnreserved(char c)
{
if (Uri.IsAsciiLetterOrDigit(c))
{
return true;
}
if (UriParser.ShouldUseLegacyV2Quirks)
{
return (RFC2396UnreservedMarks.IndexOf(c) >= 0);
}
return (RFC3986UnreservedMarks.IndexOf(c) >= 0);
}

那个代码指的是

    private static readonly UriQuirksVersion s_QuirksVersion = 
(BinaryCompatibility.TargetsAtLeast_Desktop_V4_5
// || BinaryCompatibility.TargetsAtLeast_Silverlight_V6
// || BinaryCompatibility.TargetsAtLeast_Phone_V8_0
) ? UriQuirksVersion.V3 : UriQuirksVersion.V2;

internal static bool ShouldUseLegacyV2Quirks {
get {
return s_QuirksVersion <= UriQuirksVersion.V2;
}
}

困惑

文档说 EscapeDataString 的输出取决于是否启用 IRI/IDN 解析,而源代码说输出由 TargetsAtLeast_Desktop_V4_5 的值决定,这似乎自相矛盾。有人可以解决这个问题吗?

最佳答案

与 4.0 相比,4.5 在系统功能和行为方面做了很多更改。你可以看看这个线程

Why does Uri.EscapeDataString return a different result on my CI server compared to my development machine?

你可以直接到下面的链接

http://msdn.microsoft.com/en-us/library/hh367887(v=vs.110).aspx

所有这一切都来自世界各地用户的意见。

关于c# - Uri.EscapeDataString 怪异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24962514/

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