http://xiangai.taobao.com
http://shop148612228.taobao.com

几种 HtmlEncode 的区别

问题:

HttpUtility.HtmlDecode ,HttpUtility.HtmlEncode 与 Server.HtmlDecode ,Server.HtmlEncode 与 HttpServerUtility.HtmlDecode , HttpServerUtility.HtmlEncode 有什么区别?

他们与下面一般手工写的代码有什么不一样的?

public static string htmlencode(string str)  
 { 
        if (str == null || str == "") 
            return ""; 
        str = str.Replace(">", ">"); 
        str = str.Replace(" <", "<"); 
        str = str.Replace(" ", " "); 
        str = str.Replace("  ", "  "); 
        str = str.Replace("/"", """); 
        str = str.Replace("/'", "'"); 
        str = str.Replace("/n", " 
"); return str; }

答案:

HtmlEncode: 将 Html 源文件中不允许出现的字符进行编码,通常是编码以下字符"<"、">"、"&" 等。

HtmlDecode: 刚好跟 HtmlEncode 相关,解码出来原本的字符。

HttpServerUtility 实体类的 HtmlEncode 方法 是一种简便方式,用于在运行时从 ASP.NET Web 应用程序访问 System.Web.HttpUtility.HtmlEncode 方法。HttpServerUtility 实体类的 HtmlEncode 方法 在内部使用 System.Web.HttpUtility.HtmlEncode 对字符串进行编码。

Server.HtmlEncode 其实就是 System.Web.UI.Page 类封装的 HttpServerUtility 实体类的 HtmlEncode 方法; System.Web.UI.Page 类有这样的一个属性: public HttpServerUtility Server { get; }

所以我们可以认为:
Server.HtmlDecode = HttpServerUtility 实体类的 HtmlDecode 方法 = HttpUtility.HtmlDecode ;
Server.HtmlEncode = HttpServerUtility 实体类的 HtmlEncode 方法 = HttpUtility.HtmlEncode ;

他们只不过是为了调用方便,做了封装而已。

在 ASP 中, Server.HTMLEncode Method 过滤的字符描述如下:

如果字符串不是 DBCS 编码。这个方法将转换下面字符:

less-than character (<)
<

greater-than character (>)
>

ampersand character (&)
&

double-quote character (")
"

Any ASCII code character whose code is greater-than or equal to 0x80
&#, where is the ASCII character value.

如果是 DBCS 编码

  • All extended characters are converted.
  • Any ASCII code character whose code is greater-than or equal to 0x80 is converted to &#, where is the ASCII character value.
  • Half-width Katakana characters in the Japanese code page are not converted.

相关资料:

Server.HTMLEncode Method
http://msdn.microsoft.com/en-us/library/ms525347.aspx

在ASP.net 中情况也类似

下面是一个简单的替换测试代码,测试结果看之后的注释:

protected void Page_Load(object sender, EventArgs e)
{

    TestChar("<"); // 小于号    替换   <
    TestChar(">"); // 大于号    替换   >
    TestChar("'"); // 单引号    替换   '
    TestChar(" "); // 半角英文空格    不做替换
    TestChar(" "); // 全角中文空格    不做替换
    TestChar("&"); // &    替换   &
    TestChar("/""); // 英文双引号    替换   "
    TestChar("/n"); // 回车    不做替换
    TestChar("/r"); // 回车    不做替换
    TestChar("/r/n"); // 回车    不做替换
}


public void TestChar(string t)
{
    Response.Write(Server.HtmlEncode(t));
    Response.Write("__");
    Response.Write(HttpUtility.HtmlEncode(t));
    Response.Write("
"); }

所以上面我们提到的常用替换方式还是非常有用的,他还处理了一些 HttpUtility.HtmlEncode 不支持的替换。

public static string htmlencode(string str)
{
    if (str == null || str == "")
        return "";
    str = str.Replace(">", ">");
    str = str.Replace(" <", "<");
    str = str.Replace(" ", " ");       // HttpUtility.HtmlEncode( 并不支持这个替换
    str = str.Replace("  ", "  ");     // HttpUtility.HtmlEncode( 并不支持这个替换
    str = str.Replace("/"", """);
    str = str.Replace("/'", "'");
    str = str.Replace("/n", " 
"); // HttpUtility.HtmlEncode( 并不支持这个替换 return str; }

我们使用 Reflector 查看 HttpUtility.HtmlEncode 的实现,我们就可以看到,它只考虑的五种情况,空格,回车是没有处理的:

使用 Reflector 查看 HttpUtility.HtmlEncode 实现代码其中最重要的代码如下:

public static unsafe void HtmlEncode(string value, TextWriter output)
{
    if (value != null)
    {
        if (output == null)
        {
            throw new ArgumentNullException("output");
        }
        int num = IndexOfHtmlEncodingChars(value, 0);
        if (num == -1)
        {
            output.Write(value);
        }
        else
        {
            int num2 = value.Length - num;
            fixed (char* str = ((char*) value))
            {
                char* chPtr = str;
                char* chPtr2 = chPtr;
                while (num-- > 0)
                {
                    chPtr2++;
                    output.Write(chPtr2[0]);
                }
                while (num2-- > 0)
                {
                    chPtr2++;
                    char ch = chPtr2[0];
                    if (ch <= '>')
                    {
                        switch (ch)
                        {
                            case '&':
                            {
                                output.Write("&");
                                continue;
                            }
                            case '/'':
                            {
                                output.Write("'");
                                continue;
                            }
                            case '"':
                            {
                                output.Write(""");
                                continue;
                            }
                            case '<':
                            {
                                output.Write("<");
                                continue;
                            }
                            case '>':
                            {
                                output.Write(">");
                                continue;
                            }
                        }
                        output.Write(ch);
                        continue;
                    }
                    if ((ch >= '/x00a0') && (ch < 'ā'))
                    {
                        output.Write("&#");
                        output.Write(((int) ch).ToString(NumberFormatInfo.InvariantInfo));
                        output.Write(';');
                    }
                    else
                    {
                        output.Write(ch);
                    }
                }
            }
        }
    }
} 

参考资料:

HttpUtility.HtmlDecode与Server.HtmlDecode区别
http://topic.csdn.net/u/20090220/11/110c8079-1632-418a-b43b-3ddb2f0a06e2.html

詳細解說幾個建置網站時常用的編碼方法
http://blog.miniasp.com/?tag=/htmlencode

用于 Silverlight 的 .NET Framework 类库HttpUtility.HtmlEncode 方法
http://msdn.microsoft.com/zh-cn/library/system.windows.browser.httputility.htmlencode(VS.95).aspx

HttpUtility.HtmlEncode() and HttpServerUtility.HtmlEncode() do not encode all non-ASCII characters
https://connect.microsoft.com/VisualStudio/feedback/details/102251/httputility-htmlencode-and-httpserverutility-htmlencode-do-not-encode-all-non-ascii-characters?wa=wsignin1.0

总结:
1)要激发Session_End(),必须正确配置Web.config,如<sessionState timeout="1" mode="InProc" />
2)Session_End()中是无法获得HttpContext对象的。
3)执行Session.Abandon()后,客户端必须至少有一次请求,才能正确反应Session的状态。


跑运输论坛,跑跑运输,搞活经济!!!  (还等什么!!!赶快猛点进入!!!)    跑运输站长博客,跑运输,搞活经济!!!  (还等什么!!!赶快猛点进入!!!)   

聚划算淘宝客外推精选商品、 集结聚划算外推高转化商品     品牌卖家折扣商品推广     天天疯狂购,低至3折,还等 神马     打造第一母婴达人馆,网罗更多母婴商品,为你打造属于你的温馨小屋!     淘宝优秀店铺精选,集合了高佣金高转换率的优秀店铺。     秒杀满立减    齐集淘宝疯狂促销食品、让你买得省心、吃得放心、而且还有高额佣金哦!     淘宝皇冠店铺精选,集合了高佣金高转换率的皇冠 店铺。     淘宝网-亚洲最大、最安全的网上交易平台,提供机票、票务服务,方便您的出行     想看宝岛台湾的时尚动态吗,想观宝岛台湾的潮流前沿吗,一切尽在台湾馆频道,高佣金、高转化,精彩不容错过    淘宝最权威的女装风向标,集合了淘宝最热卖的优质商品,给买家带来全新的购物体验。     淘宝商城,亚洲最大最全最专业的网上购物网站——淘宝网打造的在线B2C购物平台。 淘宝商城频道集合了全球最知名的品牌包括联想、惠普、迪士尼、Kappa、乐扣乐扣、JackJones等,给买家带来全新的一站式购物体验     正品行货保证!精选电器城最优品牌商品,让您有亲临商城的优良购物体验。     引领淘宝数码时尚,为你带来淘宝最受追捧的新潮数码产品,让你追随时尚永不out!     淘宝最权威的鞋包配饰风向标,集合了淘宝最热卖的优质商品,让买家了解每季最时尚的配饰。     淘宝客男人频道,精选淘宝网热销男装,高成交、高佣金、高转化,让您收益节节高!     集结淘宝最丰富的强势类目,精选最优质的卖家和商品,达到最广泛的买家覆盖率     淘宝美容馆,我的美容管家。精选商品、高额佣金、疯狂畅销、打造买家自己的美容小馆。     打造第一居家达人馆,网罗更多创意极品和居家商品,为你共同打造属于你的浪漫满屋!    


网购从这里开始   

   
posted @ 2012-06-05 21:42  万事俱备就差个程序员  阅读(456)  评论(0编辑  收藏  举报

http://xiangai.taobao.com
http://shop148612228.taobao.com
如果您觉得对您有帮助.领个红包吧.谢谢.
支付宝红包
微信打赏 支付宝打赏