A solution to the problem that when Chinese characters are written into Asp.net's HttpCookie, the characters are garbled when read!
Author:Eve Cole
Update Time:2009-07-01 15:54:00
Today, a student sister asked me: When I write Chinese characters in Asp.net's HttpCookie, why are all the read values garbled? In fact, this is caused by character encoding. Chinese characters have two encodings, which is why such garbled characters are produced! In fact, the solution is very simple: as long as when writing the Cookie, first encode it with Url, then write it, and then decode it when we read it. I hope more student sisters can solve it, haha!
example:
Cookie writing:
1HttpCookie cookie=new HttpCookie("Simple");
2cookie.Values.Add("Simple1",HttpUtility.UrlEncode("Hello, uncle!"));
3cookie.Values.Add("Simple2","English is OK! Nothing we should do!");
4Response.AppendCookie(cookie); Cookie reading:
1HttpCookie cookie=Request.Cookies["Simple"];
2string simple1=HttpUtility.UrlDecode(cookie["Simple1"]);
3string simple2=cookie["Simple2"]; This way simple1="Hello, uncle!";simple2="English is OK!Nothing we should do!";
It's very simple! ! !
http://www.cnblogs.com/xdotnet/archive/2006/09/27/get_right_cookie_value.html