About wirelessly's cookie management

Source: Internet
Author: User

Cookie management is an unavoidable problem in the Android app for everyone. I would like to make some humble remarks here.

First look at where the cookie might be stored.

1.Httpclient stores the cookie content at the time of request, the storage location is httpclient.getcookiestore But Apache recommends a custom cookie storage method, [1] because Cookiestore put cookies in ArrayList, it is easy to be recycled by the system.

2.WebView will store cookies in Cookiemanager, specific usage methods, and subsequent articles will say here is not the point.

Cookies in normal httpclient cannot be shared with WebView cookies, so a middle variable (here called Cookiemap) is required to manage cookies. Every time the request to refresh Cookiemap, I recommend the cookie management method is only add, modify do not delete. A cookie synchronization is required for each request to complete, and some apps require the login status, so it is also necessary to put the cookie into preference.

        list<cookie> cookielist = httpclient.getcookiestore (). GetCookies ();         if NULL Null | | Cookielist.size () = = 0)            {return;        }         = context.getsharedpreferences (constant.httphead, context.mode_private);         = Preferences.edit ();          for (Cookie cookie:cookielist) {            edit.putstring (Cookie.getname (), Cookie.getvalue ());            Httpcookiesmap.put (Cookie.getname (), cookie);        }        Edit.commit ();

Before the request, if you want to Setcookie in the header, then do not keep the cookie in the httpclient, it is easy to cause the server to submit the request header contains two cookies, some can cause server confusion.

        // because cookies are processed on their own, it is easy to clear two cookie headers if you want to clean up a cookie that is managed by client         Httpclient.getcookiestore (). Clear ();         =Getcookiesforhttpheader (context);        Request.setheader (Constant.cookie, Strheader);         = Httpclient.execute (request);  

Occasionally encountering requests made by the API and WebView in different domains, the domain and path of the cookie are required to be reset, and it is encouraged to set the path range to a larger number or some requests may not be cookie-like.

Cookiemanager.setcookie (URL, key + "=" + cookie.getvalue () + ";d omain=" + "xxxx.com" + ";p ath=/");

Here are the cookie cleanup work that needs to be done when logout, and if you use cookies to determine if you are logged in, it is not enough to clean up only the cookies in local cookiemap and preference. You also need to clean up the corresponding cookie in the WebView. Otherwise the next landing will still be logged in the state.

        //Clean up Memory Cookieshttpcookiesmap.clear (); //Clean HttpclientcookieHttpclient.getcookiestore (). Clear (); //clean off the Webviewcookie .cookiemanager.getinstance (). Removeallcookie ();        Cookiesyncmanager.getinstance (). sync (); //cleanup of Preferencecookie related projectsSharedpreferences preferences =context.getsharedpreferences (Constant.httphead, context.mode_private); Editor Editor=Preferences.edit ();        Editor.remove (Constant.account);        Editor.remove (Constant.nickname);        Editor.remove (CONSTANT.PHPSESSID); Editor.commit ();

I hope this article is helpful to everyone's work. Next you will write an article about the httpclient configuration.

[1].http://hc.apache.org/httpcomponents-client-ga/tutorial/html/statemgmt.html

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.