I am learning to store cookies
in Android and came across several ways of implementing it. One of them being the use of CookieManager and CookieStore
.
As I was going through Android docs, I came across the following statement:
To establish and maintain a potentially long-lived session between client and server, HttpURLConnection includes an extensible cookie manager. Enable VM-wide cookie management using CookieHandler and CookieManager:
CookieManager cookieManager = new CookieManager();
CookieHandler.setDefault(cookieManager);
I don't understand the meaning of VM-wide cookie management
. I know that VM means Virtual Machine
.
My Interpretations:
One way I interpreted it is, creating a
CookieManager
and passing it tosetDefault()
makes it available throughout the application. Hence, I tried the following to test it.URL url = new URL("http://something.com"); URI uri=new URI("http://something.com"); urlConnection = (HttpURLConnection) url.openConnection(); cks=urlConnection.getHeaderField("Set-Cookie"); //cks is a String cookieManager=new CookieManager(); CookieHandler.setDefault(cookieManager); HttpCookie hc=new HttpCookie("Cookie1",cks); cookieManager.getCookieStore().add(uri,hc); cks1=cookieManager.getCookieStore().getCookies().get(0).getValue(); //cks1 is another String
I set
cks and cks1
toTextViews
and it printed cookiecontent/value
as expected. Based on my interpretation, I triedcookieManager.getCookieStore().getCookies().get(0).getValue();
in another activity but it didn't recognise the object which means it is out of scope and not accessible. Also, created a newCookieManager
and tried to get the cookies but it returnednull
. So, I assume this interpretation of VM-wide being accessible across activities is incorrect.Second Interpretation was Cookies will be automatically stored when
CookieManager
is set up. I got it from a solution to another question on SO: Cookie management with Java URLConnection
One of the statements in the solution that suggested so:
When HttpURLConnection receives a cookie from the server the CookieManager will receive the cookie and store it. Future requests to the same server will automatically send the previously set cookies.
I removed cookieManager.getCookieStore().add(uri,hc);
to test it and discovered that cookies are not stored automatically. So, that interpretation fails too.
ANOTHER DOUBT THAT HAUNTS ME:
Most of the solutions to storing cookies for later use suggests using SharedPreferences
. The thing that haunts me is all of them stores cookies in CookieManager
initially and later moves it to SharedPreferences
. Why not use SharedPreferences
directly?
For example:
URL url = new URL("http://something.com");
urlConnection = (HttpURLConnection) url.openConnection();
cks=urlConnection.getHeaderField("Set-Cookie");
SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", MODE_PRIVATE);
Editor editor = pref.edit();
editor.putString("cookie_name", cks); // Saving cookie
editor.commit();
So what is the point of using CookieManager
and then moving it to SharedPreferences
?