The project uses cookies to save Chinese characters, but the following error is reported:
Control Character in cookie value, consider base64 encoding your value
It indicates that the value saved to the cookie has a control character and cannot be saved. But in fact the data does not have this problem. Looking at the following sentence, it seems that the value to be saved is base64 encoded. It may be because of garbled Characters During Chinese encoding, which may lead to some control characters.
Solution: encode the value to be saved using urlencoder. encode (value, "UTF-8.
Decoding is also performed during extraction:
Java code
- /**
- * Add a cookie value
- * @ Param name
- * @ Param Value
- * @ Param time cookie Validity Period
- * @ Param response: the object that saves the cookie
- */
- Public Static VoidSetcookie (string name, string value, integer time, httpservletresponse response ){
- Try{
- // Key points
- Value = urlencoder. encode (value, "UTF-8 ");
- }Catch(Unsupportedencodingexception e ){}
- Cookie =NewCookie (name, value );
- Cookie. setpath ("/");
- Cookie. setmaxage (time );
- Response. addcookie (cookie );
- }
- /**
- * Values are taken from the cookie Based on the name value.
- *
- * @ Param name the name to be obtained
- * @ Param request cookie objects
- * @ Return: cookie value corresponding to name
- */
- Public StaticString getcookie (string name, httpservletrequest request ){
- Cookie [] cs = request. getcookies ();
- String value = "";
- If(Cs! =Null){
- For(Cookie C: CS ){
- If(Name. Equals (C. getname ())){
- Try{
- // Key points
- Value = urldecoder. Decode (C. getvalue (), "UTF-8 ");
- }Catch(Unsupportedencodingexception e ){
- }
- ReturnValue;
- }
- }
- }
- ReturnValue;
- }
From: http://amcucn.javaeye.com/blog/403857
Control Character in cookie value, consider base64 encoding your value-An error occurred while saving the cookie in Chinese. [go to]