Android DefaultHttpClient HttpResponse missing Set-Cookie header field

I have a server that uses spring security-me-authentication, which I log in using Android DefaultHttpClient in the POST method. I can successfully log in and even access session cookies (in my case, jsessionid cookies and spring security remember cookies).

But it’s strange that after executing the POST method, for example

mResponse = mDefaultHttpClient.execute(mHttpPost) 

I can get cookies, but only using the getCookieStore method in my DefaultHttpClient, for example

 mDefaultHttpClient.getCookieStore() 

the getAllHeaders method of the HttpResponse object is not used

 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 am returning some headers (Server, X-powered by By, Date, Content-Type, Content-Length), but not a Set-Cookie header or a few others (Access-control-Allow- *, etc.).

Thanks for any help!

UPDATE: It looks like DefaultHttpClient is not expanding some of the headers. I tried adding a response interceptor (as 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()); } } }); 
+6
source share
2 answers

Have you tried 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 smorgasbord objects needed to execute HTTP requests when processing cookies, authentication, connection control and other functions."

+1
source

if you set a cookie on the server side, for example session_start () in php, Set-Cookie is shown in the headers, but if you did not set a cookie, Set-Cookie is not displayed.

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=/ 
0
source

Source: https://habr.com/ru/post/891765/


All Articles