js和C#中的编码和解码

时间:2023-03-09 03:21:24
js和C#中的编码和解码

同一个字符串,用URL编码和HTML编码,结果是完全不同的。

JS中的URL编码和解码。对 ASCII 字母和数字及以下特殊字符无效: - _ . ! ~ * ' ( ) ,/?:@&=+$#

encodeURI("http://www.w3school.com") //url编码
decodeURI("http%3A%2F%2Fwww.w3school.com.cn") //url解码
encodeURIComponent("http://www.w3school.com") //url编码,含+
decodeURIComponent("http%3A%2F%2Fwww.w3school.com.cn") //url解码,含+

JS中的HTML编码和解码。对 ASCII 字母和数字及以下特殊字符无效: - _ . ! ~ * ' ( )

escape("text") //html编码
unescape("text") //html解码

C#中的URL编码和解码

HttpUtility.UrlEncode("http://www.w3school.com") //url编码
HttpUtility.UrlDecode("http%3A%2F%2Fwww.w3school.com.cn") //url解码

C#中的HTML编码和解码

HttpUtility.HtmlEncode("text") //html编码
HttpUtility.HtmlDecode("text") //html解码