HttpUtility.HtmlEncode不编码一切 [英] HttpUtility.HtmlEncode doesn't encode everything

查看:222
本文介绍了HttpUtility.HtmlEncode不编码一切的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我与使用C#和.NET 3.5桌面客户端程序的Web服务器进行交互。我使用小提琴手看到Web浏览器发送哪些流量,并模拟了。可悲的是这台服务器是旧的,并且是一个有点困惑的字符集和UTF-8的概念。主要是它采用的Latin-1。

I am interacting with a web server using a desktop client program in C# and .Net 3.5. I am using Fiddler to see what traffic the web browser sends, and emulate that. Sadly this server is old, and is a bit confused about the notions of charsets and utf-8. Mostly it uses Latin-1.

当我输入数据包含特殊的字符,如Ωπℵ∞♣♥
♈Web浏览器♉ ♊♋♌♍♎♏♐♑♒♓提琴手告诉我,他们正在传输从浏览器进行如下服务器:&放大器;#9800;&放大器;#9801;&放大器;#9802;&安培; #9803;&放大器;#9804;&放大器;#9805;&放大器;#9806;&放大器;#9807;&放大器;#9808;&放大器;#9809;&放大器;#9810;&放大器;#9811;

When I enter data into the Web browser containing "special" chars, like "Ω π ℵ ∞ ♣ ♥ ♈ ♉ ♊ ♋ ♌ ♍ ♎ ♏ ♐ ♑ ♒ ♓" fiddler show me that they are being transmitted as follows from browser to server: "♈ ♉ ♊ ♋ ♌ ♍ ♎ ♏ ♐ ♑ ♒ ♓ "

但对于我的客户,HttpUtility.HtmlEncode不转换这些字符,这使他们原样。什么我需要调用转换♈到&放大器;#9800;等等?

But for my client, HttpUtility.HtmlEncode does not convert these characters, it leaves them as is. What do I need to call to convert "♈" to ♈ and so on?

推荐答案

这似乎太没效率了,但我觉得这样做的唯一途径是通过每个人物看:

It seems horribly inefficient, but the only way I can think to do that is to look through each character:

public static string MyHtmlEncode(string value)
{
   // call the normal HtmlEncode first
   char[] chars = HttpUtility.HtmlEncode(value).ToCharArray();
   StringBuilder encodedValue = new StringBuilder();
   foreach(char c in chars)
   {
      if ((int)c > 127) // above normal ASCII
         encodedValue.Append("&#" + (int)c + ";");
      else
         encodedValue.Append(c);
   }
   return encodedValue.ToString();
}

这篇关于HttpUtility.HtmlEncode不编码一切的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆