URL参数GB2312和UTF-8编码 自动识别

时间:2023-03-09 18:03:57
URL参数GB2312和UTF-8编码 自动识别

网上找的,以备后用。

直接上代码:

public static string QueryStringDecode(string key)
{
HttpRequest Request = System.Web.HttpContext.Current.Request; if (Regex.IsMatch(
HttpUtility.UrlDecode(Request.Url.Query, Encoding.GetEncoding("iso-8859-1")),
@"^(?:[\x00-\x7f]|[\xfc-\xff][\x80-\xbf]{5}|[\xf8-\xfb][\x80-\xbf]{4}|[\xf0-\xf7][\x80-\xbf]{3}|[\xe0-\xef][\x80-\xbf]{2}|[\xc0-\xdf][\x80-\xbf])+$"
))
{
return Request.QueryString[key];
}
else
{
System.Collections.Specialized.NameValueCollection Rq = HttpUtility.ParseQueryString(Request.Url.Query, System.Text.Encoding.GetEncoding("gb2312"));
return Rq[key];
}
}