Tomas
Tomas

Reputation: 18097

Encoded unicode characters lost

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?

Upvotes: 2

Views: 2784

Answers (2)

Cocowalla
Cocowalla

Reputation: 14331

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 :)

Upvotes: 2

baaroz
baaroz

Reputation: 19587

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())

Upvotes: 0

Related Questions