Thursday, August 23, 2007

Unicode content in Cookies

This is another small small things but can get to the nerves ;)

So basically, I was doing some changes with a Taiwan web application, previously it was using Session to store some information and right now because of we're integrating with other application, we need to use the cookie instead so the information can be shared.
Note: When using cookies, if the information is confidential, you may need to consider encryption.

After i modified the application and tested it in one of the server, everything is working perfectly, until I tried it in the other server, just to make sure that this is working fine as well. And the unexpected actually happened, the unicode characters were corrupted, they are becoming question marks (????).
Firstly I thought that the server probably missing some language pack, and then I installed the language pack to the server and turned out, it was not helping either. Then I tried this application into another server again (it is fun to have a lot of servers to play and test with hohohoho), and some of them were able to save the unicode correctly and some of them didn't, including my own PC :(

I was kept thinking, what is the difference between these machines, why some of them are working and some of them aren't. I looked into the cookie file in the my local machine, the cookies which were saved correctly are having UTF-8 as the encoding and the cookies which are corrupted were having ANSI as the encoding.

So in the end, actually instead of thinking why UTF-8 and ANSI, there is a simple solution.
Use HttpUtility.UrlEncode when saving cookie, and use HttpUtility.UrlDecode when reading back from the cookie.
Using this way, we don't need to worry whether the characters will be corrupted because of some machine settings :)

Sounds easy rite? hhhmm... it took me almost 2 days browsing and browsing and then have this solution come to my mind. Getting older I guess hahaha ;)

5 comments:

swathi said...

Thanks a lot of posting this information. Its very helpful and save lot of time. I used Server.UrlEncode instead of HttpUtility.UrlEncode. HttpUtility.UrlEncode didnot work for me.

Bembeng Arifin said...

Hi swathi,
Glad that helps for you as well ;)

Anonymous said...

You can try to use Server.UrlEncode:
HttpCookie cookie = new HttpCookie("userSearch");
cookie.Values.Add("SearchName", Server.UrlEncode(txtFriendSearch.Text));
Response.Cookies.Add(cookie);

Anonymous said...

Mhhh...

Sounds like a pretty good idea, but unfortunately, when adding the "HtmlEncoded" value in the cookie, the transformed semicolons escapes the value, so this simple xml :

" "
is stocked as
&lt

... :(

And I can't get the Server.UrlEncode working. May it solve the problem ? Wich spacenae is it from ?

Thanks,

ShX

Anonymous said...

lol brackets've been understood as real tags by the commentary engine! :)

The xml was : [test] test [/test] but with "<" instead of "[" of course ! :)

Post a Comment