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());
}
}
});
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.
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.
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.
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."
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With