Source: Script House, Baidu Space, NetEase blog
Http://www.jb51.net/article/34055.htm
Http://hi.baidu.com/honfei
http://tianminqiang.blog.163.com/blog/#m =0
==============================================================================
How to save Chinese in a cookie
When the user name is saved with a cookie, it is found that the cookie value cannot be stored in Chinese, as follows:
Control character in cookie value, consider BASE64 encoding your value
Error occurred in: Response.addcookie (cookie);
In the previous programming has also encountered such problems, mainly the existence of illegal parameters in the cookie value, such as the existence of "\ r \ n", "\ n" and other characters when reported such a mistake, but I have a user name Ah, does not exist like these characters ah, regardless, I set the cookie value as a default Chinese user name, Run to see if there is a problem, sure enough, the same error, the results know that the cookie can not be saved in Chinese.
Finally think of the conversion of Chinese to UTF-8 string to save should be no problem, that is, with Urlencoder.encode ("Chinese User name", "Utf-8″"); This converts the Chinese user name to a UTF-8 string, which is passed by the runtime. At the end of receiving this value, use Urldecoder.decode (Cookies.getvalue (), "Utf-8″") to decode the Chinese user name I want.
UrlEncode and UrlDecode in the bag java.net inside.
。。。。。。。。。。。。。。
Other languages have URL character normalization transcoding functions, such as:
ASP:server.URLEncode ()
Php:urlencode ()
JAVA:java.net.URLEncoder.encode ()
Above from NetEase Blog
====================================================================================
How to solve the problem of the Chinese garbled value of ASP
The inability to write Chinese in a cookie is caused by the innate encoding of the cookie. So there needs to be an intermediate code to transition. UrlEncode is the best choice.
We take the example of ASP., the code is as follows:
The code is as follows:
When setting cookies:
HttpCookie cookie =NewHttpCookie ("name", System.Web.HttpContext.Current.Server.UrlEncode ("home of the script")); RESPONSE.COOKIES.ADD (cookie);
when reading cookies: if(request.cookies["name"] !=NULL) {Response.Write (System.Web.HttpContext.Current.Server.UrlDecode (request.cookies["name"]. Value)); }
Note : Encoding and decoding are consistent
System.Web.HttpContext.Current.Server.UrlDecode and System.Web.HttpContext.Current.Server.UrlEncode System.Web.HttpUtility.UrlDecode and System.Web.HttpUtility.UrlEncode
The above part comes from the Script House article "ASP. NET cookie value Chinese garbled problem solving method"
==================================================================================================
A workaround for using cookies in asp.net2.0 to save Chinese garbled in the recent work encountered in asp.net2.0 using cookies to save Chinese will appear garbled, the solution is to encode and decode Chinese content, for example:
When setting cookies:
HttpCookie cookie = request.cookies["UserName"]; if(session["UserName"] !=NULL) { stringUserName = Server.URLEncode (session["UserName"]. ToString ()); if(Cookie! =NULL) {cookie. Value=UserName; //cookies. Expires = DateTime.Now.AddDays (1);Cookies. Domain =Basepage.domainname; Response.Cookies.Set (cookie); } Else{Cookie=NewHttpCookie ("UserName"); Cookies. Value=UserName; //cookies. Expires = DateTime.Now.AddDays (1);Cookies. Domain =Basepage.domainname; RESPONSE.COOKIES.ADD (cookie); } }
When obtaining a cookie:
Private string_loginusername; Public stringLoginusername {Get { if(session["UserName"] !=NULL&& session["UserName"]. ToString ()! ="") {_loginusername= session["UserName"]. ToString (); } Else if(request.cookies["UserName"] !=NULL&& request.cookies["UserName"]. Value! ="") {_loginusername= Server.urldecode (request.cookies["UserName"]. Value); } Else{_loginusername=NULL; } return_loginusername; } }
There is also a condition that the Chinese format needs to be set in the configuration file, such as:
<!--Globalization This section sets the globalization settings for the application. -
<globalization requestencoding= "GB2312" responseencoding= "GB2312"/>
--------------------------------------------------------------------------------------------------------------- ------------
The question of Chinese cookies,
In Windows 2000 Normal,
In Windows 2003 SP1 will occasionally appear garbled (encountered double-byte special characters, Example: "' 蹆"),
Basic garbled in Windows 2003 SP2
Workaround:
Using
Server.URLEncode ();
Server.urldecode ();
encoding on Write : cookie["MyCookie"] = Server.URLEncode ("Chinese")
decoding on read: Response.Write (Server.urldecode (Request.Cookies ("MyCookie"). Value ()))
In addition the encoding and decoding to be consistent
System.Web.HttpUtility.UrlDecode and System.Web.HttpUtility.UrlEncode
System.Web.HttpContext.Current.Server.UrlDecode and System.Web.HttpContext.Current.Server.UrlEncode
The above from Baidu space