Cookie value includes equal sign

Source: Internet
Author: User

Some values are stored in cookies after base64 encoding. When there is one or two equal signs (=) at the end of the encoded string, request is used. getcookies (). getvalue () will lose the equal sign and cause an error when base64 decoding is performed.

Https://issues.apache.org/bugzilla/show_bug.cgi? Id = 44679. You can see the discussion in this link.

It mentioned:

Org. Apache. tomcat. util. http. servercookie.Allow_equals_in_value
System Property
That was introduced in Tomcat 6.0.24. Looks like Mark has seen the light.
I just wanted to update this issue so everyone that needs this feature is aware
Of It.

Solution:
1) directly retrieve the cookie value from the HTTP head, resolve it by yourself, and keep the equal sign in the value. (cookie value format: Name = value; name2 = value2; name3 = value3

2) Before base64 decoding, calculate the length and supplement the equal sign before decoding.

Public static string decodebase64 (string s) {Switch (S. length () % 4) {Case 3: S + = "="; break; // Note: you only need to add one or two equal signs, case 2: S + = "="; break; Case 1: S + = "="; break; default :} return new base64encoder (). decode (S. getbytes ();} public static string encodebase64 (string s) {string encoded = base64encoder (). encode (S. getbytes (); Return encoded. replaceall ("[\ n =]", "");}

Note:

Cookie specifications

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.