Android: Handle Cookie from HTTP Get-Request

2020-07-26 11:22发布

问题:

I am working on an app which shall log in to a web site (via http://......?password=xyz). I use DefaultHttpClient for this. Along with the GET response, the website sends a cookie, which I want to store for further POST requests.

My problem is that client.getCookieStore().getCookies() always receives an empty list of cookies.

If I open http://www.google.com (insted of my intended website), I receive the cookies properly, but the website I am working with, seems to send the cookie in some other way (it's a MailMan mailing list moderating page)

I can see the respective cookie in Firefox cookie manager, but not in Firebug network/cookie panel (why?). InternetExplorer HttpWatchProfessional however shows the cookie when recording the traffic....

There is some small difference, I observed between the cookies www.google.com sent and my target website: In HttpWatchProfessional, those cookies from google are marked as "Direction: sent", while the cookie from my website are marked as "Direction: Received". (how can the google cookies be sent, while I cleared browser/cookie cache just before?)

Can someone explain the difference to me?

My code is the following:

DefaultHttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
HttpResponse execute = client.execute(httpGet);
List<Cookie> cookies = client.getCookieStore().getCookies();

回答1:

After further investigation, I found out that the cookie was received, but actually rejected by the httpclient, due to a path the cookie, which differed to that from the called URL.

I found the solution at: https://stackoverflow.com/a/8280340/1083345