1

I'm trying to use cookies to hold my session on my Android app, but it seems I'm getting something wrong, because I never receive the expected response from my server.

At first I have a login routine that runs as expected and return all expected data.

My login request:

        HttpContext httpContext = new BasicHttpContext();
        HttpResponse response;
        HttpClient client = new DefaultHttpClient();
        String url = context.getString(R.string.url_login);
        HttpPost  connection = new HttpPost(url); 
        connection.setHeader("Content-Type","application/x-www-form-urlencoded");
        List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>(2);
        nameValuePair.add(new BasicNameValuePair(PARAM_LOGIN,params[0]));
        nameValuePair.add(new BasicNameValuePair(PARAM_PASSWORD,params[1]));
        connection.setEntity(new UrlEncodedFormEntity(nameValuePair,"UTF-8"));
        response = client.execute(connection,httpContext);
        data = EntityUtils.toString(response.getEntity());

After I've my response I just make what ever I need with the data and then things start to fall a part. Because now I'm just trying to call my server in the same AsyncTask to test if my cookies got properly saved on my HttpContext.

At first I've just called my URL without any change, just reusing my current HttpContext:

        HttpPost httpPost = new HttpPost(context.getString(R.string.url_cookie_test)); 
        HttpResponse response = client.execute(httpPost, httpContext);

Since this test fails I tested to add my cookie value on my HttpPost header:

       httpPost.addHeader(context.getString(R.string.domain),PHPSESSID+"="+cookieID+";");

Then I tried creating a new HttpContext and force the COOKIE_STORE:

        CookieStore cookieStore = new BasicCookieStore();
        BasicClientCookie cookie = new BasicClientCookie(PHPSESSID, cookieID);
        cookieStore.addCookie(cookie);
        HttpContext localContext = new BasicHttpContext();
        localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);

        HttpResponse response;
        HttpClient client = new DefaultHttpClient();
        HttpPost  httpPost = new HttpPost(context.getString(R.string.url_cookie_test));
        response = client.execute(connection,localContext);

All fails, and I've already confirmed that when I first receive my login response I got the data expected from the cookies as can see below:

        List<Cookie> cookies = ((AbstractHttpClient) client).getCookieStore().getCookies();
        for (Cookie cookie: cookies){
             Log.i("Cookie Value",cookie.toString());
             /*
              Prints:[[version: 0][name: PHPSESSID][value: 2ebbr87lsd9077m79n842hdgl3][domain: mydomain.org][path: /][expiry: null]]
             */
        }

I've already searched on StackOverflow and I've found a ton of solutions that doesn't really worked for me, will share all solutions I've already tried:

Android: Using Cookies in HTTP Post request

HttpPost request with cookies

Sending cookie with http post android

Apache HttpClient 4.0.3 - how do I set cookie with sessionID for POST request

Community
  • 1
  • 1
GhostDerfel
  • 1,533
  • 1
  • 11
  • 18
  • Why don't you add your headers with httpPost.setHeader("key","value") insteadOf addHeader? I've solved several issues with this change and it made me lose many hours... I don't know if it is the problem, but I think it's worthy to proof – Juan Aguilar Guisado Oct 15 '14 at 17:02
  • Thanks for the suggestion, just tested it and I keep getting the same response (cookies not available). – GhostDerfel Oct 15 '14 at 17:04
  • I think I have a piece of code that could be good for you. It for connecting Android to a Spring mvc server with cookies but I think tgat maybe it could help you. I don't mind share it with you. It's not your answer, but let me help you :). I'm coming with it later with it – Juan Aguilar Guisado Oct 15 '14 at 19:25

1 Answers1

1

As I told you, here you are this piece of code in order to make httpPost to a server developed in Spring MVC, with an API REST. Please, consider to build your request on this way:

Please, pay attention to the comments. You should adapt it to your case ;). You can also enclose this code into a method or whatever you prefer.

try {

        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("yourPath");

        //NameValuePairs is  build with the params for your request

        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        httppost.setHeader("Content-Type",
                "application/x-www-form-urlencoded");

        CookieStore cookieStore = new BasicCookieStore();

        //cookie is a variable that I stored in my shared preferences.
        //You have to send in it every request
        //In your case, JSESSIONID should change, because it's for Java.
        //Maybe it could be "PHPSESSID"
        BasicClientCookie c = new BasicClientCookie("JSESSIONID", cookie);

        //JSESSIONID: same comment as before.
        httppost.setHeader("Cookie", "JSESSIONID="+cookie);

        cookieStore.addCookie(c);
        ((AbstractHttpClient)httpclient).setCookieStore(cookieStore);

        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity = response.getEntity();
        is = entity.getContent();
    } catch (Exception e) {
        Log.e("log_tag", "Error in http connection" + e.toString());
    }

I hope this helps!! It was hard to find it among "old" projects :)

Juan Aguilar Guisado
  • 1,687
  • 1
  • 12
  • 21