gpt4 book ai didi

c# - HttpUtility.HtmlEncode 不会对所有内容进行编码

转载 作者:技术小花猫 更新时间:2023-10-29 12:13:02 25 4
gpt4 key购买 nike

我正在使用 C# 和 .Net 3.5 中的桌面客户端程序与 Web 服务器进行交互。我正在使用 Fiddler 来查看 Web 浏览器发送的流量并进行模拟。可悲的是,这个服务器很旧,并且对字符集和 utf-8 的概念有点困惑。它主要使用 Latin-1。

当我在 Web 浏览器中输入包含“特殊”字符的数据时,例如“Ω π ℵ ∞ ♣ ♥♈ ♉ ♊ ♋ ♌ ♍ ♎ ♏ ♐ ♑ ♒ ♓"fiddler 告诉我它们从浏览器到服务器的传输方式如下:"♈ ♉ ♊ ♋ ♌ ♍ ♎ ♏ ♐ ♑ ♒ ♓ "

但对于我的客户,HttpUtility.HtmlEncode 不会转换这些字符,而是让它们保持原样。我需要调用什么才能将“♈”转换为♈等等?

最佳答案

Rich Strahl 刚刚发布了一篇博文,Html and Uri String Encoding without System.Web ,他也有一些自定义代码来编码字符的上限。

/// <summary>
/// HTML-encodes a string and returns the encoded string.
/// </summary>
/// <param name="text">The text string to encode. </param>
/// <returns>The HTML-encoded text.</returns>
public static string HtmlEncode(string text)
{
if (text == null)
return null;

StringBuilder sb = new StringBuilder(text.Length);

int len = text.Length;
for (int i = 0; i < len; i++)
{
switch (text[i])
{

case '<':
sb.Append("&lt;");
break;
case '>':
sb.Append("&gt;");
break;
case '"':
sb.Append("&quot;");
break;
case '&':
sb.Append("&amp;");
break;
default:
if (text[i] > 159)
{
// decimal numeric entity
sb.Append("&#");
sb.Append(((int)text[i]).ToString(CultureInfo.InvariantCulture));
sb.Append(";");
}
else
sb.Append(text[i]);
break;
}
}
return sb.ToString();
}

关于c# - HttpUtility.HtmlEncode 不会对所有内容进行编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/547634/

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