string text= System.Web.HttpUtility.UrlEncode("heart", System.Text.Encoding.UTF8);
//UrlEncode编码
string data = System.Web.HttpUtility.UrlDecode(text, System.Text.Encoding.UTF8);
//UrlDecode解码
HttpUtility.UrlEncode(text); //utf-8 编码
HttpUtility.UrlDecode(text); //utf-8 解码
HttpUtility.UrlEncode(text, System.Text.Encoding.GetEncoding(936)); //gb2312编码
HttpUtility.UrlDecode(text, System.Text.Encoding.GetEncoding(936)); //gb2312解码
================
利用URL中传长中文的地方
选择用HttpUtility.UrlEncode来进行编码,HttpUtility.UrlEncode来解码,方便且安全.
Default.aspx页
HyperLink1.NavigateUrl = "~/test/Default2.aspx?id=" + HttpUtility.UrlEncode("这里是北京");
Default2.aspx页
string value = Request.QueryString["id"];
Response.Write(HttpUtility.UrlDecode(value));
===============
在php中
urlencode()函数用于编码URL字符串,
echo urlencode('不要迷恋哥');//输出:%B2%BB%D2%AA%C3%D4%C1%B5%B8%E7
urlencode()函数原理就是首先把中文字符转换为十六进制,然后在每个字符前面加一个标识符%,了解了这个原理,可以实现自定义的URL编码函数。
使用urldecode()函数解码已编码的 URL 字符串,实例如下
echo urldecode('%B2%BB%D2%AA%C3%D4%C1%B5%B8%E7');//输出:不要迷恋哥
urldecode()函数与urlencode()函数原理相反,用于解码已编码的 URL 字符串,其原理就是把十六进制字符串转换为中文字符,