The correct way to update Android cookies

Source: Internet
Author: User

How to update before

The use of a search of cookies, it is easy to search many articles, the basic steps are roughly the same, such as:

Basically all require you to call Cookiemanager.removeallcookie () or call Cookiemanager.removesessioncookie (), It's true that every Setcookie can write a new cookie, which is simply rude and effective.

Problems encountered

The way we look at Setcookies:

/** * Sets a cookie for  the givenUrl. Any existing cookie with  theSame host, * path and namewould be replaced with  theNew Cookie. The cookie beingSet* would be ignoredif it  isExpired. * * @param URL theUrl forwhich theCookies is  toBeSet* @param value theCookies asAstring, using theFormat of  the' Set-cookie ' * HTTP response header */public void Setcookie (string url, String value) {Throw    New Mustoverrideexception (); }

Each cookie is a URL that corresponds to a

Look at Removesessioncookie again.

/**     * Removes all session cookies, which are cookies without an expiration     * date.     * @deprecated use {@link #removeSessionCookies(ValueCallback)} instead.     */    publicvoidremoveSessionCookie() {        thrownew MustOverrideException();    }

It's not just the URL you're using, it's clearing all the cookies, it's not right, how can you erase all the cookies at will, and maybe the other coworkers write the corresponding cookie for another URL, Perhaps H5 's classmates also wrote a lot of useful things in the cookie, and after you called Cookiemanager.removesessioncookie (), you emptied all the cookies.
Cookiemanager.removeallcookie () is the same truth

Solutions

Take a closer look at the method annotations in the API Setcookie:

afortheURLwiththe same host,     andwiththenewset     ifit is expired.

Obviously, if you just want to update your own use of the URL of the cookie, do not delete cookies, as long as the Setcookie again, the system will automatically check the previous URL has no cookie, if there is, replace, if not, write directly.

Attention

Cookiesyncmanager and Cookiemanager.removeallcookie (), Cookiemanager.removesessioncookie () have been discarded, The sync cookie in the Sok above API21 provides Cookiemanager.flush (), so the correct setcookie should be as follows:

String URL = null;URL ="Http://higo.xxx.xxx";Cookiemanager Cookiemanager = Cookiemanager. getinstance();Cookiemanager. Setacceptcookie(true);String cookies ="Cookie";Cookiemanager. Setcookie(URL,"xxx="+cookies);if (Android. OS. Build. VERSION. SDK_int >= Android. OS. Build. VERSION_codes. LOLLIPOP) {Cookiemanager. Flush();} else {Cookiesyncmanager. CreateInstance(This. Getapplicationcontext());Cookiesyncmanager. getinstance(). Sync();}
Use of semicolons in cookies

The default in a cookie is a semicolon-delimited
Like when you write cookies like this,

"name=cookie;year=2015";setCookie("http://xxx.xxx",cookie);

That actually only Name=cookie write to http://xxx.xxx This domain name, why year=2015 not write, because the semicolon ";" is the default delimiter of the cookie, the cookie is considered to appear ";" The value of the current cookie is over.

Can you not use ";" to go; Connection string?

It's best not to do this because everyone thinks that the cookie delimiter is ";" And if you replace it with another one, when some colleagues don't know it, there's a problem.

What should be the right way?

Use base64 transcoding a bit on it, so you can also encrypt the information once, of course, you need to communicate with H5 's classmates, his side to get the value of the cookie need to base64 a bit, this is perfect.

I hope I can help you.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

The correct way to update Android cookies

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.