日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

几种HtmlEncode的区别(转)

發(fā)布時間:2023/12/20 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 几种HtmlEncode的区别(转) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

一、C#中的編碼

HttpUtility.HtmlDecode、HttpUtility.HtmlEncodeServer.HtmlDecode、Server.HtmlEncodeHttpServerUtility.HtmlDecodeHttpServerUtility.HtmlEncode的區(qū)別?

它們與下面一般手工寫的代碼有什么區(qū)別?

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

答案:

HtmlEncode:是將html源文件中不容許出現(xiàn)的字符進行編碼,通常是編碼以下字符:"<"、">"、"&"、"""、"'"等;

HtmlDecode:跟HtmlEncode恰好相反,是解碼出原來的字符;

?

HttpServerUtility實體類的HtmlEncode(HtmlDecode)的簡便方式,用于在運行時從ASP.NET Web應(yīng)用程序訪問System.Web.HttpUtility.HtmlEncode(HtmlDecode)方法,HttpServerUtility實體類的HtmlEncode(HtmlDecode)方法在內(nèi)部是使用System.Web.HttpUtility.HtmlEncode(HtmlDecode)方法對字符進行編碼(解碼)的;

?

?

Server.HtmlEncode(Server.HtmlDecode)其實是System.Web.UI.Page類封裝了HttpServerUtility實體類的HtmlEncode(HtmlDecode)的方法;

System.Web.UI.Page類有這樣一個屬性:public HttpServerUtility Server{get;}

?

?

所以可以認為:

Server.HtmlEncode=HttpServerUtility實體類的HtmlEncode方法=HttpUtility.HtmlEncode;

Server.HtmlDecode=HttpServerUtility實體類的HtmlDecode方法=HttpUtility.HtmlDecode;

它們只不過是為了調(diào)用方便,進行了封裝而已;

?

下面是一個非常簡單的替換測試代碼,測試結(jié)果看注釋:?

protected void Page_Load(object sender, EventArgs e) { TestChar("<"); //小于號 替換為 < TestChar(">"); //大于號 替換為 > TestChar(" "); //英文半角空格 替換為 不做替換; TestChar(" "); //中文全角空格 替換為 不做替換; TestChar("&"); //& 替換為 & TestChar("/'"); //單引號 替換為 '; TestChar("/""); //雙引號 替換為 " TestChar("/r"); //回車 替換為 不做替換; TestChar("/n"); //回車 替換為 不做替換; TestChar("/r/n"); //回車 替換為 不做替換; } protected void TestChar(String str) { Response.Write(Server.HtmlEncode(str)); Response.Write("----------------------"); Response.Write(HttpUility.HtmlEncode(str)); Response.Write("<br/>"); } [c-sharp]

所以手工的替換方法還是很有必要的,處理一些HtmlEncode不支持的替換。

public static string htmlencode(string str) { str.Replace("<", "<"); str.Replace(">", ">"); str.Replace(" ", " "); str.Replace(" ", " "); str.Replace("/'", "'"); str.Replace("/"", """); str.Replace("/n", "<br/>"); } [c-sharp]

使用Reflector 查看 HttpUttility.HtmlEncode 的實現(xiàn),我們就可以看到,它只考慮的五種情況,空格,回車是沒有處理的:

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); } } } } } } [c-sharp]

二、JS中的編碼和解碼

  • 一、escape/unescape??
  • ????escape:escape?方法返回一個包含?charstring?內(nèi)容的字符串值(Unicode?格式)。所有空格、標(biāo)點、?重音符號以及任何其他非?ASCII?字符都用?%xx?編碼替換,其中?xx?等于表示該字符的十六進制數(shù)??
  • ????unescape:從用?escape?方法編碼的?String?對象中返回已解碼的字符串??
  • ????例外字符:?@?*?/?+??
  • ??
  • 二、encodeURI/decodeURI??
  • ????encodeURI:方法返回一個已編碼的?URI。如果將編碼結(jié)果傳遞給?decodeURI,則將返回初始的字符串。encodeURI?不對下列字符進行編碼:“:”、“/”、“;”和“?”。請使用?encodeURIComponent?對這些字符進行編碼??
  • ????decodeURI:從用encodeURI方法編碼的String對象中返回已解碼的字符串??
  • ????例外字符:!?@?#?$?&?*?(?)?=?:?/?;???+?'??
  • ??
  • 三、encodeURIComponent/decodeURIComponent??
  • ????encodeURIComponent:encodeURIComponent?方法返回一個已編碼的?URI。如果將編碼結(jié)果傳遞給decodeURIComponent,則將返回初始的字符串。因為?encodeURIComponent?方法將對所有字符編碼??
  • ????decodeURIComponent:從用encodeURIComponent方法編碼的String對象中返回已解碼的字符串??
  • ????例外字符:!?*?(?)?' ?
  • 原文:http://blog.csdn.net/wd330260402/article/details/5977989

    轉(zhuǎn)載于:https://www.cnblogs.com/MirageFox/p/4814164.html

    總結(jié)

    以上是生活随笔為你收集整理的几种HtmlEncode的区别(转)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。