Cookie management is an unavoidable problem in Android apps. I would like to make some humble remarks here.
Let's look at where the cookie might be stored.
1.Httpclient stores the cookie content at the time of the request, where the storage location is Httpclient.getcookiestore but Apache suggests customizing the cookie storage method, Because Cookiestore put cookies in the ArrayList can easily be recycled [1].
2.WebView will store cookies in Cookiemanager, specific usage, and subsequent articles will say this is not the point.
Cookies in normal httpclient are not shared with webview cookies, so here's an intermediate variable (called Cookiemap) to manage cookies. Every time I request to refresh Cookiemap, I recommend that the cookie management is only added, modify not delete. Each request completes a cookie synchronization, and some apps require the login status, so it is also necessary to put cookies into the preference.
list<cookie> cookielist = Httpclient.getcookiestore (). GetCookies ();
if (context = NULL | | cookielist = NULL | | cookielist.size () = 0) {return
;
}
Sharedpreferences preferences = Context.getsharedpreferences (Constant.httphead, context.mode_private);
Editor edit = Preferences.edit ();
for (Cookie cookie:cookielist) {
edit.putstring (Cookie.getname (), Cookie.getvalue ());
Httpcookiesmap.put (Cookie.getname (), cookie);
Edit.commit ();
See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/OS/extra/
Before the request, if you want to Setcookie in the header, then do not keep the cookies in the httpclient otherwise easily caused by the server to submit requests to the header contains two cookies, some will cause server confusion.
Two cookie headers are easy to clean with client-managed cookies as a result of handling cookies on your own
Httpclient.getcookiestore (). Clear ();
String Strheader =getcookiesforhttpheader (context);
Request.setheader (Constant.cookie, Strheader);
HttpResponse response = Httpclient.execute (request);
Occasionally encountering requests made by APIs and WebView in different domains, it requires the domain and path of the cookie to be reset, where the path range is encouraged or some requests such as (AJAX,JSONP) may not get a cookie.
Cookiemanager.setcookie (URL, key + "=" + cookie.getvalue () + ";d omain=" + "xxxx.com" + ";p ath=/");
The following is a cookie cleanup that needs to be done when logout, and it is not enough to clean up cookies in local cookiemap and preference if you use cookies to determine if you are logged in. You also need to clean out the corresponding cookies in the WebView. Otherwise the next landing remains the landing state.
Clean Memory Cookie
httpcookiesmap.clear ();
Clean Httpclientcookie
Httpclient.getcookiestore (). Clear ();
Clean out Webviewcookie
cookiemanager.getinstance () Removeallcookie ();
Cookiesyncmanager.getinstance (). sync ();
Clean up Preferencecookie related items
sharedpreferences 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, I'll write an article about httpclient configuration.
Author: cnblogs Sharp Chen Xiang