6

I have a server that uses spring security remember-me-authentication to which I login using Android DefaultHttpClient in a POST method. I am able to successfully login and even able to retrieve the session cookies created (In my case a jsessionid cookie and a spring security remember me cookie).

But the weird thing is after executing the POST method like

mResponse = mDefaultHttpClient.execute(mHttpPost)

I am able to retrieve the cookies, but only using getCookieStore method in my DefaultHttpClient like

mDefaultHttpClient.getCookieStore()

not using getAllHeaders method of the HttpResponse object

headers = mResponse.getAllHeaders();
HeaderIterator headerIterrator = new BasicHeaderIterator(headers, null);
while (headerIterrator.hasNext()) {
  Header header = headerIterrator.nextHeader();
  headerStringBuilder.append(" " + header.getName() + ":" + header.getValue());
}
Log.e("Post response headers: ", headerStringBuilder.toString());

I get some headers back in here( Server, X-powered-By, Date, Content-Type, Content-Length) but not the Set-Cookie header or a few others( Access-control-Allow-* etc)

Thanks for any help!

UPDATE: Looks like the DefaultHttpClient does not expose some of the headers. I tried adding response interceptor (like shown below) and getAllHeaders returned all the headers I wanted. Thanks for reading my question!

    mDefaultHttpClient.addResponseInterceptor(new HttpResponseInterceptor() {
     public void process(final HttpResponse response, final HttpContext context)
                throws HttpException, IOException {
            Header[] headers = response.getAllHeaders();
            for (int i = 0; i < headers.length; i++) {
                Header header = headers[i];
                Log.e("HTTP: ", "name: " + header.getName());
                Log.e("HTTP: ", "value: " + header.getValue());
            }
        }

    });
Colin 't Hart
  • 7,372
  • 3
  • 28
  • 51
Json
  • 61
  • 1
  • 3
  • You could try the example in my question here: http://stackoverflow.com/questions/3858593/android-http-get-session-cookie see if you get the same issue – Blundell Jun 30 '11 at 21:20
  • @Blundell , thanks for the comment, yeah I tried the example but just the same set of headers as above are returned. – Json Jul 01 '11 at 13:32
  • I think some headers are populated during the actual request, in particular those that may have a limited lifetime. – njzk2 Sep 06 '13 at 15:32
  • Out of curiosity, why do you need to read the cookie? It will be set automatically on your next request (if you don't destroy the instances) – Loda Nov 19 '13 at 09:15

2 Answers2

1

Did you try using HttpURLConnection rather than HttpClient? HttpClient, as the name says, is a client, then it manages cookies for you, HttpURLConnection does not.

Doc says "HTTP clients encapsulate a smorgasbord of objects required to execute HTTP requests while handling cookies, authentication, connection management, and other features."

Massimo
  • 3,436
  • 4
  • 40
  • 68
0

if you set cookie in server side , like session_start() in php , the Set-Cookie have show in the headers , but if you have don't set any cookie , the Set-Cookie have not shown.

after:

05-25 17:23:16.616: I/HTTP:(575): name: Set-Cookie
05-25 17:23:16.616: I/HTTP:(575): value: PHPSESSID=g29i01av8ddvpgsi7lpaj12lc3; path=/