我收到两个不同编码的URL,含中文参数,如:
http://xxx.com/?post=xxx&name=0:你好 -UTF-8编码
http://xxx.com/?post=xxx&name=0:你好 -GB2312编码怎么判断当前的请求编码?
请各位帮帮我
http://xxx.com/?post=xxx&name=0:你好 -UTF-8编码
http://xxx.com/?post=xxx&name=0:你好 -GB2312编码怎么判断当前的请求编码?
请各位帮帮我
解决方案 »
- 有没有人用.net(c#)搞过三维地理信息开发
- DataGridView
- C# 打包cab文件时,怎么设置在PDA的桌面生成快捷方式
- StringBuilder 修改值的问题
- comboBox.Items.Clear()这个是什么意思
- c# LParam 转结构体获取GUID问题, 苦寻方法望指点
- ASP.NET(C#)中怎么实现中英文版本(网站开发) ?
- .net面试
- C# winform 将某个路劲下txt文件转化成EXCEL
- 那位用c#为asp.net开发过服务器控件,在部署的时候都是怎么做的?
- 100分 新手求指点
- 怎么让一个很大的图片完整显示在一个比图片小的pictureBox控件里面
1 加标志来标识,如 01 utf-8 02 gb2312
http://xxx.com/?post=xxx&name=01:你好 -UTF-8编码
http://xxx.com/?post=xxx&name=02:你好 -GB2312编码
2 把一个应用分成2个应用:
http://xxx.com/?post=xxx&name=0:你好 -UTF-8编码
http://xxx2.com/?post=xxx&name=0:你好 -GB2312编码
/// <summary>
/// 返回解码后的字符串
/// </summary>
/// <param name="key">传参的参数名</param>
/// <returns></returns>
public static string UrlDecode(string key)
{
string input = GetUrlParam(key).ToLower();
if (input.Length == 0)
return string.Empty;
//首先用utf-8进行解码
string result = HttpUtility.UrlDecode(input, Encoding.UTF8);
// 将已经解码的字符再次进行编码.
string encode = HttpUtility.UrlEncode(result, Encoding.UTF8).ToLower();
//与原来编码进行对比,如果不一致说明解码未正确,用gb2312进行解码
if (input != encode)
result = HttpUtility.UrlDecode(input, Encoding.GetEncoding("gb2312"));
return result;
} private static string GetUrlParam(string key)
{
if (System.Web.HttpContext.Current != null)
{
string query = HttpContext.Current.Request.Url.Query;
if (query != null && query.Length > 0)
{
int index = 0;
index = query.IndexOf(key + "=");
if (index >= 0)
{
query = query.Substring(key.Length + 1 + index);
index = query.IndexOf('&');
if (index >= 0)
query = query.Substring(0, index);
return query;
}
}
}
return string.Empty;
}