I would like to preserve a session while connecting to server using HttpGet and I need to understand how it handles cookies.
The server developer says that he handles all cookies stuff by himself.
I use HttpGet request to access the server as follows:
InputStream isResponse = null;
HttpGet httpget = new HttpGet(strUrl);
HttpResponse response = mClient.execute(httpget);
HttpEntity entity = response.getEntity();
isResponse = entity.getContent();
responseBody = convertStreamToString(isResponse);
return responseBody;
-
Should I do something more? Does he put the cookie on my device automatically and the HttpGet method knows to use it in order to keep the sessionID using the cookie?
-
How can I check if the cookie exist on my device, in order to know if the session is "alive"?
-
In case I use the following code to get the cookie:
CookieStore cookieStore = new BasicCookieStore(); // Create local HTTP context HttpContext localContext = new BasicHttpContext(); // Bind custom cookie store to the local context localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore); HttpGet httpget = new HttpGet(strUrl); HttpResponse response = mClient.execute(httpget,localContext);
does the HttpGet still handles the cookies the same as before?
- I see that DefaultHttpClient (mClient in the code above) has its own CookieStore. How can I save its cookies and load them next time I create it?
Best Answer
So... After cracking my head for hours and implementing my own primitive CookieStore, I found Android Asynchronous Http Client implementation, that includes a nice PersistentCookieStore that works great! Simply added the jar to my project and used it as follows:
and that's it! The cookies are saved and reused when the application is open again.
Thank you James Smith, where ever you are. You made at least one man happy.
If anyone interested in my own primitive implementation (that also works) here it is: