0

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);
rohanpro
  • 132
  • 7

4 Answers4

0

One way to do it, though here cookies are saved manually (which I wouldn't suggest).

AbstractHttpClient client = new DefaultHttpClient();
        //setup url
        Uri.Builder uriBuilder = Uri.parse(mRestUrl).buildUpon();
        Uri uri = uriBuilder.build();
        // request is either HttpPost or HttpGet
        request.setURI(new URI(uri.toString()));

        // Here we add cookies to cookie store of a client 
        //(cookies saved manually)
        CookieStore cookieStore = client.getCookieStore();
        for (Cookie cookie : cookieList) {
            cookieStore.addCookie(cookie);
        }

        HttpResponse response = client.execute(request);
        //in case you want to save cookies manually
        cookieList = client.getCookieStore().getCookies();
        //response
        HttpEntity responseEntity = response.getEntity();
JuliusScript
  • 3,850
  • 1
  • 17
  • 17
0

I will advise you to use HttpUrlConnection class. It is the recommended one from Google.

HTTP cookies are sent using HTTP headers. Simply provide the "Cookie" header with the cookie itself as a value. And that's all. Sending a cookie with the request is just adding a new HTTP header to it:

URL url = new URL("http://www.alabala.com");
URLConnection connection = url.openConnection();
// set the cookie
connection.setRequestProperty("Cookie", "my-cookie-value");   
...
conn.getOutputStream();
Kiril Aleksandrov
  • 2,601
  • 20
  • 27
0

In HttpClient of Apache, it puts like this:

HttpMethod method = new GetMethod();
//method.getParams().setCookiePolicy(CookiePolicy.RFC_2109);
method.getParams().setCookiePolicy(CookiePolicy.IGNORE_COOKIES);
method.setRequestHeader("Cookie", "special-cookie=value");

And if you are using HttpUrlConnection, maybe you need to try:

conn.setRequestProperty("Cookie", "jsessionid=xxxxx;param1=a"); 
conn.connect();
SilentKnight
  • 13,761
  • 19
  • 49
  • 78
  • Sorry, but where have you associated method with client? And what does "special-cookie=value signify"? – rohanpro Apr 09 '15 at 14:28
  • First question, `HttpClient.execute(method)` while `method` could be `HttpGet` `HttpPost` and so that. Second, arguments of `Cookie` could be `key=value` pairs which you could send to server, and then sever could response to client. – SilentKnight Apr 10 '15 at 01:50
0

Well, I figured out the answer myself. There were different solutions posted in internet, but none of them mentioned things clearly, as I have no idea regarding backend development.

In the following snippet, lets assume you retrieved a cookie (something like "w8rhn29wr208n4rc2mr29cmn2rn2m12", some random value) in last response. Now if you want to attach it to next request, you have to add it as HEADER in the request. The headers in a URL request is of the form "some_key=some_value". The "some_key" is actually defined in server side programming. For multiple cookies in same request, you have to attach multiple headers in the same request.

Rest of the procedure is normal, about attaching the url, executing it and getting the response.

String url= "www.test.in/test_cookie_link";
HttpGet request = new HttpGet();
//cookie_received_in_last_response = w8rhn29wr208n4rc2mr29cmn2rn2m12
//add the cookie as header to the GET request
request.addHeader("cookie","my_key=" + cookie_received_in_last_response);
//attach the url to GET request
request.setURI(new URI(url));
//execute the request
HttpResponse r_response = client.execute(request);
rohanpro
  • 132
  • 7