gpt4 book ai didi

c# - WebUtility.HtmlDecode 与 HttpUtilty.HtmlDecode

转载 作者:IT王子 更新时间:2023-10-29 04:27:47 25 4
gpt4 key购买 nike

我正在使用 WebUtilty.HtmlDecode 解码 HTML。事实证明它没有正确解码,例如 应该解码为“-”字符,但 WebUtilty.HtmlDecode 没有解码它。但是,HttpUtilty.HtmlDecode 可以。

Debug.WriteLine(WebUtility.HtmlDecode("–"));
Debug.WriteLine(HttpUtility.HtmlDecode("–"));


> –
> –

decode screenshot

这两者的文档是相同的:将已针对 HTTP 传输进行 HTML 编码的字符串转换为已解码的字符串。

为什么它们不同,我应该使用哪一个,如果我切换到 WebUtility.HtmlDecode 以获得“–”以正确解码,会有什么变化?

最佳答案

这两种方法的实现在Windows Phone上确实不同。

WebUtility.HtmlDecode:

public static void HtmlDecode(string value, TextWriter output)
{
if (value != null)
{
if (output == null)
{
throw new ArgumentNullException("output");
}
if (!StringRequiresHtmlDecoding(value))
{
output.Write(value);
}
else
{
int length = value.Length;
for (int i = 0; i < length; i++)
{
bool flag;
uint num4;
char ch = value[i];
if (ch != '&')
{
goto Label_01B6;
}
int num3 = value.IndexOfAny(_htmlEntityEndingChars, i + 1);
if ((num3 <= 0) || (value[num3] != ';'))
{
goto Label_01B6;
}
string entity = value.Substring(i + 1, (num3 - i) - 1);
if ((entity.Length <= 1) || (entity[0] != '#'))
{
goto Label_0188;
}
if ((entity[1] == 'x') || (entity[1] == 'X'))
{
flag = uint.TryParse(entity.Substring(2), NumberStyles.AllowHexSpecifier, NumberFormatInfo.InvariantInfo, out num4);
}
else
{
flag = uint.TryParse(entity.Substring(1), NumberStyles.Integer, NumberFormatInfo.InvariantInfo, out num4);
}
if (flag)
{
switch (_htmlDecodeConformance)
{
case UnicodeDecodingConformance.Strict:
flag = (num4 < 0xd800) || ((0xdfff < num4) && (num4 <= 0x10ffff));
goto Label_0151;

case UnicodeDecodingConformance.Compat:
flag = (0 < num4) && (num4 <= 0xffff);
goto Label_0151;

case UnicodeDecodingConformance.Loose:
flag = num4 <= 0x10ffff;
goto Label_0151;
}
flag = false;
}
Label_0151:
if (!flag)
{
goto Label_01B6;
}
if (num4 <= 0xffff)
{
output.Write((char) num4);
}
else
{
char ch2;
char ch3;
ConvertSmpToUtf16(num4, out ch2, out ch3);
output.Write(ch2);
output.Write(ch3);
}
i = num3;
goto Label_01BD;
Label_0188:
i = num3;
char ch4 = HtmlEntities.Lookup(entity);
if (ch4 != '\0')
{
ch = ch4;
}
else
{
output.Write('&');
output.Write(entity);
output.Write(';');
goto Label_01BD;
}
Label_01B6:
output.Write(ch);
Label_01BD:;
}
}
}
}

HttpUtility.HtmlDecode:

public static string HtmlDecode(string html)
{
if (html == null)
{
return null;
}
if (html.IndexOf('&') < 0)
{
return html;
}
StringBuilder sb = new StringBuilder();
StringWriter writer = new StringWriter(sb, CultureInfo.InvariantCulture);
int length = html.Length;
for (int i = 0; i < length; i++)
{
char ch = html[i];
if (ch == '&')
{
int num3 = html.IndexOfAny(s_entityEndingChars, i + 1);
if ((num3 > 0) && (html[num3] == ';'))
{
string entity = html.Substring(i + 1, (num3 - i) - 1);
if ((entity.Length > 1) && (entity[0] == '#'))
{
try
{
if ((entity[1] == 'x') || (entity[1] == 'X'))
{
ch = (char) int.Parse(entity.Substring(2), NumberStyles.AllowHexSpecifier, CultureInfo.InvariantCulture);
}
else
{
ch = (char) int.Parse(entity.Substring(1), CultureInfo.InvariantCulture);
}
i = num3;
}
catch (FormatException)
{
i++;
}
catch (ArgumentException)
{
i++;
}
}
else
{
i = num3;
char ch2 = HtmlEntities.Lookup(entity);
if (ch2 != '\0')
{
ch = ch2;
}
else
{
writer.Write('&');
writer.Write(entity);
writer.Write(';');
continue;
}
}
}
}
writer.Write(ch);
}
return sb.ToString();
}

有趣的是,WP7 上不存在 WebUtility。此外,WebUtility 的 WP8 实现与桌面实现相同。 HttpUtility.HtmlDecode 的桌面实现只是 WebUtility.HtmlDecode 的包装器。最后但同样重要的是,Silverlight 5 与 Windows Phone 具有相同的 HttpUtility.HtmlDecode 实现,但没有实现 WebUtility。

由此,我可以大胆猜测:由于 Windows Phone 7 运行时基于 Silverlight,WP7 继承了 HttpUtility.HtmlDecode 的 Silverlight 版本,并且不存在 WebUtility。然后是 WP8,其运行时基于 WinRT。 WinRT 带来了 WebUtility,并保留了旧版本的 HttpUtility.HtmlDecode 以确保与遗留 WP7 应用程序的兼容性。

至于知道你应该使用哪一个...如果你想以 WP7 为目标,那么你别无选择,只能使用 HttpUtility.HtmlDecode。如果您的目标是 WP8,那么只需选择其行为最适合您需求的方法。 WebUtility 可能是面向 future 的选择,以防万一 Microsoft 决定在即将推出的 Windows Phone 版本中放弃 Silverlight 运行时。但我只是选择 HttpUtility 的实际选择,而不必担心手动支持您在问题中提出的示例。

关于c# - WebUtility.HtmlDecode 与 HttpUtilty.HtmlDecode,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17352981/

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