Sridhar
Sridhar

Reputation: 31

Android: Example for using a cookie from HttpPost for HttpGet

I am able to use the example here: http://www.androidsnippets.org/snippets/36/index.html and successfully get the "HTTP/1.1 OK" response for a webesite I am sending the HttpPost along with the user credentials. However, I am unable to use an HttpGet to further browse other pages on this site.

Can anyone please let me know, what's going wrong. I am sorry - I am very new to Java.

Upvotes: 2

Views: 7627

Answers (1)

Torid
Torid

Reputation: 4196

My guess would be that when the website gets the Post and logs the user in, it sets cookies on the response to indicate that the user is logged in, and then requires those cookies on subsequent Get's.

You will need to do something like the following (this is borrowed from a bigger app so may not compile right out of the box)

DefaultHttpClient mHttpClient = new DefaultHttpClient();
BasicHttpContext mHttpContext = new BasicHttpContext();
CookieStore mCookieStore      = new BasicCookieStore();        
mHttpContext.setAttribute(ClientContext.COOKIE_STORE, mCookieStore);

This sets up a cookie store within the HTTP context, and you then use that context on Get's and Post's. For example...

HttpResponse response = mHttpClient.execute(mRequest, mHttpContext);

Under the covers the HTTP client will store cookies from responses, and add them to requests.

Upvotes: 6

Related Questions