Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android DefaultHttpClient HttpResponse missing Set-Cookie header field

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());
            }
        }

    });
like image 457
Json Avatar asked Jun 30 '11 21:06

Json


People also ask

How to set cookies in header?

The Set-Cookie HTTP response header is used to send a cookie from the server to the user agent, so that the user agent can send it back to the server later. To send multiple cookies, multiple Set-Cookie headers should be sent in the same response.

How do I set-cookie in HTTP request?

To send cookies to the server, you need to add the "Cookie: name=value" header to your request. To send multiple Cookies in one cookie header, you can separate them with semicolons. In this Send Cookies example, we are sending HTTP cookies to the ReqBin echo URL.

What is cookie and set-cookie?

The Set-Cookie header is sent by the server in response to an HTTP request, which is used to create a cookie on the user's system. The Cookie header is included by the client application with an HTTP request sent to a server, if there is a cookie that has a matching domain and path.


1 Answers

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."

like image 106
Massimo Avatar answered Sep 28 '22 07:09

Massimo