C# 从查询字符串中获取 U+fffd/65533 而不是特殊字符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/806133/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting U+fffd/65533 instead of special character from Query String
提问by nitech
I have a C# .net web project that have a globalization tag set to:
我有一个 C# .net web 项目,它的全球化标签设置为:
<globalization requestEncoding="utf-8" responseEncoding="utf-8" culture="nb-no" uiCulture="no"/>
When this URL a Flash application (you get the same problem when you enter the URL manually in a browser): c_product_search.aspx?search=kj?kken (alternatively: c_product_search-aspx?search=kj%F8kken
当此 URL 为 Flash 应用程序时(在浏览器中手动输入 URL 时会遇到同样的问题):c_product_search.aspx?search=kj?kken(或者:c_product_search-aspx?search=kj%F8kken
Both return the following character codes:
两者都返回以下字符代码:
k U+006b 107
j U+006a 106
? U+fffd 65533
k U+006b 107
k U+006b 107
e U+0065 101
n U+006e 110
I don't know too much about character encoding, but it seems that the ? has been given a unicode replacement character, right?
我不太了解字符编码,但似乎 ? 已经给了一个 unicode 替换字符,对吗?
I tried to change the globalization tag to:
我试图将全球化标签更改为:
<globalization requestEncoding="iso-8859-1" responseEncoding="utf-8" culture="nb-no" uiCulture="no"/>
That made the request work. However, now, other searches on my page stopped working.
这使请求有效。但是,现在,我页面上的其他搜索停止工作。
I also tried the following with similar results:
我还尝试了以下类似的结果:
NameValueCollection qs = HttpUtility.ParseQueryString(Request.QueryString.ToString(), Encoding.GetEncoding("iso-8859-1"));
string search = (string)qs["search"];
What should I do?
我该怎么办?
Kind Regards,
亲切的问候,
nitech
尼泰克
采纳答案by Etienne Coumont
The problem comes from the combination Firefox/Asp.Net. When you manually entered a URL in Firefox's address bar, if the url contains french or swedish characters, Firefox will encode the url with "ISO-8859-1" by default.
问题来自 Firefox/Asp.Net 组合。当您在 Firefox 的地址栏中手动输入 URL 时,如果该 URL 包含法语或瑞典语字符,则 Firefox 默认会使用“ISO-8859-1”对 URL 进行编码。
But when asp.net recieves such a url, it thinks that it's utf-8 encoded ... And encoded characters become "U+fffd". I couldn't find a way in asp.net to detect that the url is "ISO-8859-1". Request.Encoding is set to utf-8 ... :(
但是当asp.net收到这样的url时,它认为它是utf-8编码的......并且编码的字符变成了“U+fffd”。我在 asp.net 中找不到检测 url 是“ISO-8859-1”的方法。Request.Encoding 设置为 utf-8 ... :(
Several solutions exist :
存在几种解决方案:
put
<globalization requestEncoding="iso-8859-1" responseEncoding="iso-8859-1"/>
in your Web.config. But your may comme with other problems, and your application won't be standard anymore (it will not work with languages like japanese) ... And anyway, I prefer using UTF-8 !go to about:config in Firefox and set the value of
network.standard-url.encode-query-utf8
to true. It will now work for you (Firefox will encode all your url with utf-8). But not for anybody else ...The least worst solution I could come with was to handle this with code. If the default decoding didn't work, we reparse QueryString with iso8859-1 :
string query = Request.QueryString["search"]; if (query.Contains("%ufffd")) query = HttpUtility.ParseQueryString(Request.Url.Query, Encoding.GetEncoding("iso-8859-1"))["search"]; query = HttpUtility.UrlDecode(query);
放入
<globalization requestEncoding="iso-8859-1" responseEncoding="iso-8859-1"/>
你的 Web.config。但是您可能会遇到其他问题,并且您的应用程序将不再是标准的(它不适用于日语等语言)......无论如何,我更喜欢使用 UTF-8 !转到 Firefox 中的 about:config 并将 的值设置
network.standard-url.encode-query-utf8
为 true。它现在可以为您工作(Firefox 将使用 utf-8 对您的所有 url 进行编码)。但不是为其他人...我能想到的最糟糕的解决方案是用代码来处理这个问题。如果默认解码不起作用,我们用 iso8859-1 重新解析 QueryString :
string query = Request.QueryString["search"]; if (query.Contains("%ufffd")) query = HttpUtility.ParseQueryString(Request.Url.Query, Encoding.GetEncoding("iso-8859-1"))["search"]; query = HttpUtility.UrlDecode(query);
It works with hyperlinks and manually-entered url, in french, english, or japanese. But I don't know how it will handle other encodings like ISO8859-5 (russian) ...
它适用于超链接和手动输入的 url(法语、英语或日语)。但我不知道它将如何处理其他编码,如 ISO8859-5(俄语)......
Does anyone have a better solution ?
有没有人有更好的解决方案?
This solves only the problem of manually-entered url. In your hyperlinks, don't forget to encode url parameters with HttpUtility.UrlEncode on the server, or encodeURIComponent on the javascript code. And use HttpUtility.UrlDecode to decode it.
这只解决了手动输入url的问题。在您的超链接中,不要忘记在服务器上使用 HttpUtility.UrlEncode 对 url 参数进行编码,或者在 javascript 代码上使用 encodeURIComponent 进行编码。并使用 HttpUtility.UrlDecode 对其进行解码。
回答by cRichter
i think your problem is in the flash, not the .net. it sends the special character in a weird way. try to urlencode the search string bevore you send it to the server.
我认为您的问题出在 flash 上,而不是 .net 上。它以一种奇怪的方式发送特殊字符。在将搜索字符串发送到服务器之前,尝试对其进行 urlencode。
回答by Alan Moore
If the app is expecting the URL-encoded request to be based on UTF-8, the character "?
" should be "%C3%B8
", not "%F8
". Whatever function you're using to escape/encode that request, you probably need to pass it the name of the underlying character encoding, "UTF-8".
如果应用程序期望 URL 编码的请求基于 UTF-8,则字符“ ?
”应该是“ %C3%B8
”,而不是“ %F8
”。无论您使用什么函数来转义/编码该请求,您可能都需要将底层字符编码的名称“UTF-8”传递给它。
回答by nitech
It turns out that ActionScript 2.0 will send the URL encoded/escaped with UTF-8 while ActionScript 3.0 used ISO-8859-1. The way to solve this was to change the Request.Encoding value inside Global.asax if an encoding is specified in the URL:
事实证明,ActionScript 2.0 将发送使用 UTF-8 编码/转义的 URL,而 ActionScript 3.0 使用 ISO-8859-1。如果 URL 中指定了编码,则解决此问题的方法是更改 Global.asax 中的 Request.Encoding 值:
void Application_BeginRequest(object sender, EventArgs e)
{
HttpContext ctx = HttpContext.Current;
// encoding specified?
if (!String.IsNullOrEmpty(Request["encoding"]))
{
ctx.Request.ContentEncoding = System.Text.Encoding.GetEncoding(ctx.Request["encoding"]);
}
}
Could it be done differently?
可以做不同的事情吗?
Regards, nitech
问候, nitech
回答by BradLaney
public string GetEncodedQueryString(string key)
{
string query = Request.QueryString[key];
if (query != null)
if (query.Contains((char)0xfffd))
query = HttpUtility.ParseQueryString(Request.Url.Query, Encoding.GetEncoding("iso-8859-1"))[key];
return query;
}