C#URL中⽂编码与解码参考资料
⾮常蛋疼的事情, google 和 baidu 在编码是分别采⽤了 UTF-8 和 GB2312
基础知识
UTF-8中,⼀个汉字对应三个字节,GB2312中⼀个汉字占⽤两个字节。
不论何种编码,字母数字都不编码,特殊符号编码后占⽤⼀个字节。
⾃动解码
public static string MyUrlDeCode(string str, Encoding encoding)
{
if (encoding == null)
{
Encoding utf8 = Encoding.UTF8;
在线url网址编码解码/
/⾸先⽤utf-8进⾏解码
string code = HttpUtility.UrlDecode(str.ToUpper(), utf8);
//将已经解码的字符再次进⾏编码.
string encode = HttpUtility.UrlEncode(code, utf8).ToUpper();
if (str == encode)
encoding = Encoding.UTF8;
else
encoding = Encoding.GetEncoding("gb2312");
}
return HttpUtility.UrlDecode(str, encoding);
}
More
谨慎使⽤ Request["foo"] 获取参数,反正就是各种bug
还是⾃⼰⼀个参数⼀个参数的提取安全些

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。