Encoded unicode characters lost

一曲冷凌霜 提交于 2019-12-12 20:27:20

问题


I submitting

MyApp.aspx?url=http://product.cn.china.cn/selling-leads/%C8%BC%C6%F8%D4%EE%BE%DF/ 

url as GET request to my ASP.NET application. The Chinese characters are encoded in url. I am trying to read URL parameter value with

httpRequest.Params["url"] 

and get result like this

http://product.cn.china.cn/selling-leads/ȼ�����/

The encoded part is lost. Where is the problem?


回答1:


Are you sure that the characters have been encoded correctly in the URL?

If I URL enode 公共汽車, I get %E5%85%AC%E5%85%B1%E6%B1%BD%E8%BB%8A

If I pass in %E5%85%AC%E5%85%B1%E6%B1%BD%E8%BB%8A as a query parameter it works fine, but doesn't with your string, %C8%BC%C6%F8%D4%EE%BE%DF.

If I try decoding your string with an online URL encoder/decoder, it also doesn't work, likewise if I try System.Text.Encoding.UTF8.GetString.

So I think the problem is that the string you are submitting has been encoded incorrectly.

** UPDATE **

Upon closer inspection it appears that the characters in your URL string are GBK encoded (the page you linked to also says the character set in use is GBK).

I'm not sure exactly how to do it, but if you want the URL parameter in another encoding you will need to convert from GBK to that other encoding.

** UPDATE **

OK, I think I've got it :)

It looks like ASP.NET is decoding the URL using the wrong encoding. You can force ASP.NET to decode requests as GBK by adding this to your web.config file:

<system.web>
  <globalization requestEncoding="gbk" />
</system.web>

If for some reason you don't want to do that, then you will need to parse and decode the raw URL yourself:

// TODO: Grab this from Request.RawUrl
string urlParam = "%C8%BC%C6%F8%D4%EE%BE%DF";

// Source encoding is GBK
Encoding gbk = Encoding.GetEncoding("gbk");

string decodedParam = HttpUtility.UrlDecode(urlParam, gbk);

decodedParam will now contain what you want, 燃气灶具 ("gas stove", I think :)




回答2:


Ok, I had the same problem with hebrew

You have to use HttpUtility.UrlDecode and HttpUtility.UrlEncode()

Like this

dim str=HttpUtility.UrlEncode("the string that add to the url")

(And of course to send the str in the url)

And in the receive page use this

dim temp as string=HttpUtility.UrlDecode(httpRequest.Params["url"] , System.Text.Encoding.Default())

You can do it in javascript too

escape(value) is a function to encode the text

and you can use on server side like this:

dim temp as string=HttpUtility.UrlDecode(httpRequest.Params["url"] , System.Text.Encoding.Default())


来源:https://stackoverflow.com/questions/8076538/encoded-unicode-characters-lost

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