I am using HttpCookies to maintain online session on the server. On login, through a POST request, I am getting the cookies using the following code.
HttpPost httpPost = new HttpPost(url);
CookieStore store = ((DefaultHttpClient)client).getCookieStore();
if(params != null)
httpPost.setEntity(new UrlEncodedFormEntity(params));
httpResponse = httpClient.execute(httpPost);
cookies= httpClient.getCookieStore().getCookies();
if(cookies.isEmpty())
{
Log.d("Cookies", "None!");
}
else
{
for (Cookie c : cookies) {
Log.d("Cookies","Name ["+c.getName()+"] - Val ["+c.getValue()+"] - Domain ["+c.getDomain()+"] - Path ["+c.getPath()+"]");
store.addCookie(c);
}
}
But the problem is, I am unable to attach the cookies in the next GET request, so as to maintain the session. I have tried the following code, but it's not working.
HttpContext ctx = new BasicHttpContext();
store.addCookie(singleCookie);
ctx.setAttribute(ClientContext.COOKIE_STORE, store);
Log.d("Servicehandler", singleCookie.getValue());
HttpGet request = new HttpGet();
request.setURI(new URI(url));
HttpResponse r_response = client.execute(request, ctx);