Control Character in cookie value, consider base64 encoding your value-An error occurred while saving the cookie in Chinese. [go to]

Source: Internet
Author: User
Tags control characters

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
  1. /**
  2. * Add a cookie value
  3. * @ Param name
  4. * @ Param Value
  5. * @ Param time cookie Validity Period
  6. * @ Param response: the object that saves the cookie
  7. */
  8. Public Static VoidSetcookie (string name, string value, integer time, httpservletresponse response ){
  9. Try{
  10. // Key points
  11. Value = urlencoder. encode (value, "UTF-8 ");
  12. }Catch(Unsupportedencodingexception e ){}
  13. Cookie =NewCookie (name, value );
  14. Cookie. setpath ("/");
  15. Cookie. setmaxage (time );
  16. Response. addcookie (cookie );
  17. }
  18. /**
  19. * Values are taken from the cookie Based on the name value.
  20. *
  21. * @ Param name the name to be obtained
  22. * @ Param request cookie objects
  23. * @ Return: cookie value corresponding to name
  24. */
  25. Public StaticString getcookie (string name, httpservletrequest request ){
  26. Cookie [] cs = request. getcookies ();
  27. String value = "";
  28. If(Cs! =Null){
  29. For(Cookie C: CS ){
  30. If(Name. Equals (C. getname ())){
  31. Try{
  32. // Key points
  33. Value = urldecoder. Decode (C. getvalue (), "UTF-8 ");
  34. }Catch(Unsupportedencodingexception e ){
  35. }
  36. ReturnValue;
  37. }
  38. }
  39. }
  40. ReturnValue;
  41. }

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]

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.