一、C#中的 Html编码:
HtmlEncode和 HtmlDecode:
1.Server.HtmlEncode(s):对指定的字符串应用 HTML 编码(将s中的特殊字符编码(转义)后输出):
eg: <%=Server.HtmlEncode("<font color='red'>The paragraph tag:</font>") %>
输出:<font color='red'>The paragraph tag:</font>
在浏览器中显示:<font color='red'>The paragraph tag:</font>(直接输出html字符串,不会执行脚本和应用样式)
2.Server.HtmlDecode(s):将编码后的字符串解码成原来的字符串输出。
eg: <%=Server.HtmlEncode("<font color='red'>The paragraph tag:</font>") %>
输出:<font color='red'>The paragraph tag:</font>
在浏览器中显示:红色字体(此时会执行脚本,应用样式)
二、C#中的URI编码:
JavaScript中URI编码有三种方法:escape、encodeURI、encodeURIComponent
C#中URI编码主要方法:HttpUtility.UrlEncode、Server.UrlEncode、Uri.EscapeUriString、Uri.EscapeDataString
1.escape:不推荐使用:
escape是BOM中的方法,只能对ASCII符号正确编码,而encodeURI、encodeURIComponent可以对所有的Unicode符号编码。
escape不编码字符有69个:*,+,-,.,/,@,_,0-9,a-z,A-Z。
document.write(escape("Visit W3School!") + "<br />")
输出:Visit%20W3School%21
2.encodeURI:用于对网址编码(不包含网址后面带的参数)
encodeURI不编码字符有82个:!,#,$,&,',(,),*,+,,,-,.,/,:,;,=,?,@,_,~,0-9,a-z,A-Z。
encodeURI就是为这个而设计的。encodeURI不对URI中的特殊字符进行编码,如冒号(:)、斜杠(/)、#
document.write(encodeURI("http://www.cnblogs.com\/a file with spaces.html"))
输出:http://www.cnblogs.com/a%20file%20with%20spaces.html
document.write(encodeURI("http://www.cnblogs.com\/a.html"))
原样输出:http://www.cnblogs.com/a.html。
由于encodeURI不对冒号(:)、斜杠(/)进行编码,所以如果参数(如把网址作为参数)中包含冒号(:)、斜杠(/),就会解析出错,所以此方法不能对网址参数进行编码。
3.encodeURIComponent:用于对网址参数进行编码:
encodeURIComponent不编码字符有71个:!, ',(,),*,-,.,_,~,0-9,a-z,A-Z
可以看到此方法对:/都进行了编码,所以不能用它来对网址进行编码。由于此方法对中文,空格,井号(#),斜线(/),冒号(:)都进行了编码,所以适合对URI中的参数进行编码。
var param="博客园";
var url="http://www.cnblogs.com/?key="+encodeURIComponent(param)+"&page=1";
console.log(url);//outputs http://www.cnblogs.com/?key=%E5%8D%9A%E5%AE%A2%E5%9B%AD&page=1
4.Server.UrlEncode 或 HttpUtility.UrlEncode:将URL编码规则,包括转义字符应用到指定的字符串。--不推荐使用
字符 特殊字符的含义 URL UrlEncod编码后
# 用来标志特定的文档位置 %23
% 对特殊字符进行编码 %25
& 分隔不同的变量值对 %26
+ 在变量值中表示空格 %2B
\ 表示目录路径 %2F
= 用来连接键和值 %3D
? 表示查询字符串的开始 %3F
当url中含有以上列表中的一些字符时就无法准确的接收其中的值。 要使用UrlEncode将其转义;
<%Response.Write(Server.URLEncode(http://www.updateweb.cn)) %>
输出:http%3a%2f%2fwww.updateweb.cn
Server.UrlEncode && HttpUtility.UrlEncode在绝大多数情况下是一样的,区别是HttpUtility.UrlEncode默认使用UTF8格式编码,而Server.UrlEncode是使用系统预设格式编码,Server.UrlEncode使用系統预设编码做为参数调用HttpUtility.UrlEncode编码,所以如果系统全局都用UTF8格式编码,这两个方法就是一样的。
string url1 = "http://www.cnblogs.com/a file with spaces.html?a=1&b=博客园#abc";
Response.Write(HttpUtility.UrlEncode(url1) );
//output
http%3a%2f%2fwww.cnblogs.com%2fa+file+with+spaces.html%3fa%3d1%26b%3d%e5%8d%9a%e5%ae%a2%e5%9b%ad%23abc
HttpUtility.UrlEncode对冒号(:)和斜杠(/)进行了编码,所以
不能用来对网址进行编码。也不能用来对参数进行编码,因为在参数中空格应该被编码为%20,而不是被HttpUtility.UrlEncode编码为加号(+),所以
不推荐用这两个方法对URI进行编码。
5.Uri.EscapeUriString 用于对网址进行编码(不包含参数)
string url1 = "http://www.cnblogs.com/a file with spaces.html?a=1&b=博客园#abc";
Response.Write( Uri.EscapeUriString(url1));
//outputs:
http://www.cnblogs.com/a%20file%20with%20spaces.html?a=1&b=%E5%8D%9A%E5%AE%A2%E5%9B%AD#abc
Uri.EscapeUriString对空格进行了编码,也对中文进行了编码,但对冒号(:)、斜杠(/)和井号(#)未编码,所以此方法可以用于网址进行编码,但不能对参数进行编码,作用类似JavaScript中的encodeURI方法。
6.Uri.EscapeDataString:用于对网址参数进行编码
string url1 = "http://www.cnblogs.com/a file with spaces.html?a=1&b=博客园#abc";
Response.Write(Uri.EscapeDataString(url1));
//outputs:
http%3A%2F%2Fwww.cnblogs.com%2Fa%20file%20with%20spaces.html%3Fa%3D1%26b%3D%E5%8D%9A%E5%AE%A2%E5%9B%AD%23abc
Uri.EscapeDataString对冒号(:)、斜杠(/)、空格、中文、井号(#)都进行了编码,所以此方法不可以用于网址进行编码,但可以用于对参数进行编码,作用类似JavaScript中的encodeURIComponent方法。
7.总结:
在JavaScript中推荐的做法是用encodeURI对URI的网址部分编码,用encodeURIComponent对URI中传递的参数进行编码。
在C#中推荐的做法是用Uri.EscapeUriString对URI的网址部分编码,用Uri.EscapeDataString对URI中传递的参数进行编码。
解码部分与编码方法相对应。