ASP.NET 2中的ISO-8859-1到UTF8
发布时间:2020-12-16 09:14:21 所属栏目:asp.Net 来源:网络整理
导读:我们有一个页面将数据发布到ISO-8859-1中的ASP.NET应用程序 head META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1" title`Sample Search Invoker`/title/headbodyform name="advancedform" method="post" action="SearchResults.asp
我们有一个页面将数据发布到ISO-8859-1中的ASP.NET应用程序
<head> <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1"> <title>`Sample Search Invoker`</title> </head> <body> <form name="advancedform" method="post" action="SearchResults.aspx"> <input class="field" name="SearchTextBox" type="text" /> <input class="button" name="search" type="submit" value="Search >" /> </form> 并在后面的代码(SearchResults.aspx.cs) System.Collections.Specialized.NameValueCollection postedValues = Request.Form; String nextKey; for (int i = 0; i < postedValues.AllKeys.Length; i++) { nextKey = postedValues.AllKeys[i]; if (nextKey.Substring(0,2) != "__") { // Get basic search text if (nextKey.EndsWith(XAEConstants.CONTROL_SearchTextBox)) { // Get search text value String sSentSearchText = postedValues[i]; System.Text.Encoding iso88591 = System.Text.Encoding.GetEncoding("iso-8859-1"); System.Text.Encoding utf8 = System.Text.Encoding.UTF8; byte[] abInput = iso88591.GetBytes(sSentSearchText); sSentSearchText = utf8.GetString(System.Text.Encoding.Convert(iso88591,utf8,abInput)); this.SearchText = sSentSearchText.Replace('<',' ').Replace('>',' '); this.PreviousSearchText.Value = this.SearchText; } } } 当我们通过Merkbl?tter时,它会被释放出来作为Merkbl tter的PostedValues [i] 有任何想法吗? 解决方法
你有这行代码: –
String sSentSearchText = postedValues[i]; 这里发生了对帖子中八位字节的解码. 问题是META http-equiv不告诉服务器有关编码的信息. 您可以将RequestEncoding =“ISO-8859-1”添加到@Page指令中,并停止尝试自己解码(因为它已经发生). 这也无济于事.看来你只能在web.config中指定Request encoding. 最好是完全停止使用ISO-8859-1,并保留默认的UTF-8编码.我看到使用限制性编码没有任何好处和痛苦. 编辑 如果看起来不太可能改变发布形式编码,那么我们似乎只能自己处理解码.为此,在接收代码隐藏中包含这两种静态方法: – private static NameValueCollection GetEncodedForm(System.IO.Stream stream,Encoding encoding) { System.IO.StreamReader reader = new System.IO.StreamReader(stream,Encoding.ASCII); return GetEncodedForm(reader.ReadToEnd(),encoding); } private static NameValueCollection GetEncodedForm(string urlEncoded,Encoding encoding) { NameValueCollection form = new NameValueCollection(); string[] pairs = urlEncoded.Split("&".ToCharArray(),StringSplitOptions.RemoveEmptyEntries); foreach (string pair in pairs) { string[] pairItems = pair.Split("=".ToCharArray(),2,StringSplitOptions.RemoveEmptyEntries); string name = HttpUtility.UrlDecode(pairItems[0],encoding); string value = (pairItems.Length > 1) ? HttpUtility.UrlDecode(pairItems[1],encoding) : null; form.Add(name,value); } return form; } 现在而不是分配: – postedValues = Request.Form; 使用:- postValues = GetEncodedForm(Request.InputStream,Encoding.GetEncoding("ISO-8859-1")); 您现在可以从其余代码中删除编码marlarky. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net-mvc – 存储库模式 – 如何正确处理JOIN和复杂查询
- asp-classic – 如何清除所有cookie?
- asp.net – Windows身份验证会话超时
- asp.net-mvc-5 – 如何在没有数据库的情况下使用ASP.NET Id
- 如何检测当前应用程序池是否在IIS7.5和Asp.Net 3.5中清理
- asp.net-mvc – System.Web.Optimization使函数参数名对于某
- asp.net-mvc – ActionLink MVC中的图像按钮
- asp.net-core – 什么应该是WEB API Action Method的返回类
- asp.net-mvc – 选择在razor html.dropdownlistfor的pagelo
- asp.net-mvc – ASP.Net C#Razor显示编码的HTML而不是原始的
推荐文章
站长推荐
热点阅读