QueryString converting %E1 to %ufffd

若如初见. 提交于 2020-01-04 02:56:06

问题


I have a URL like the following

http://mysite.com/default.aspx?q=%E1

Where %E1 is supposed to be á. When I call Request.QueryString from my C# page I receive

http://mysite.com/default.aspx?q=%ufffd

It does this for any accented character. %E1, %E3, %E9, %ED etc. all get passed as %ufffd. Normal encoded values (%2D, %2E, %27) all get passed correctly.

The config file already has the responseEncoding/requestEncoding in the globalization section set to UTF-8.

How could I read the correct values?

Please note that I'm not the one generating the query string and I have no control over it.


回答1:


While it's true that á is encoded as U+00E1, the UTF-8 encoding (which is relevant for URL parameters) is 0xC3 0xA1.

You can verify by called a Wikipedia entry on an accented letter, such as http://en.wikipedia.org/wiki/%C3%81

U+FFFD is the Unicode Replacement Character which indicates the a given character value cannot be correctly encoded in Unicode.

Update:

Your question has two points.

First: How do I encode a Unicode string as parameter. Use

"?q=" + HttpUtility.UrlEncode(value)

Second: How do I retrieve a Unicode value? Use:

Request["q"]

If you receive the %E1 from some other source you do not control, maybe the RawUrl can help you. (I have not tried)



来源:https://stackoverflow.com/questions/14327085/querystring-converting-e1-to-ufffd

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!