benkdev
benkdev

Reputation: 673

AndroidHttpClient and Cookies

Using the AndroidHttpClient with cookies gives me intermixed 200 ok and 403 forbidden responses. I'm not sure what I am doing wrong.

I'm using the AndroidHttpClient in the following manner:

I have several background thread classes, and each one does the following:

HttpGet get...
HttpClient client = AndroidHttpClient.newInstance("Android");
HttpContext http_context = HttpSupport.getHttpContextInstance(); 
CookieStore cookie_store = HttpSupport.getCookieStoreInstance();
http_context.setAttribute(ClientContext.COOKIE_STORE, cookie_store);
client.execute(

HttpSupport is a class with two static fields; a CookieStore and a HttpContext:

public class HttpSupport {

private static HttpContext _context;
private static CookieStore _cookieStore;

public static synchronized HttpContext getHttpContextInstance() {
    if (_context == null) {
        _context = new BasicHttpContext();
    }
    return _context;
}

public static synchronized CookieStore getCookieStoreInstance() {
    if (_cookieStore == null) {
        _cookieStore = new BasicCookieStore();
    }
    return _cookieStore;
}

}

Is it ok to have multiple instances of the AndroidHttpClient in the application? Am I storing the cookies correctly?

Upvotes: 0

Views: 3582

Answers (2)

Matt Wolfe
Matt Wolfe

Reputation: 9284

I would recommend using the Android Asynchronous HTTP Client

It implements its own persistent cookie store (PersistentCookieStore) which is really easy to use. In fact, if you don't want to use the library for its asynchronous abilities you could just use it for the Persistent Cookie Store that it provides.. However the asynchronous abilities of it are highly worth while.

So to use it with your current class you could simply make a change in your HttpSupport class that has

public static synchronized CookieStore getCookieStoreInstance(Context context) {
    if (_cookieStore == null) {
        _cookieStore = new PersistentCookieStore(context);
    }
    return _cookieStore;
}

Note that you will have to add the context to the method as it is required for the PersistentCookieStore to work.

Upvotes: 5

FabianCook
FabianCook

Reputation: 20557

HttpClient automattically stores the cookies I have found, you should really be doing all this in an async task (as to Android 3.0> requirments).

The 200 response is when everything has been sent through right and the file you want is avaliable to you, this isn't a problem.

The 403 error is pretty obvious, does this server require some type of log in to get the file or webpage? This is what you should investigate, a way to work out how to log in is to use something like google chrome, right click on the webpage, go inspect element, then watch the network files going through, log in, watch for a post request. Find out how the website works.

Have a look here for a async task: HTTP POST request ANDROID 4 (working in 2.3)?

I believe you can have multiple instances of HttpClient, but to be using the same cookies you should be using one for everything that is connected

For eg:

You should have one HttpClient to log in, then request a page using get, do what ever you need to do etc.


Sorry I relised you were using AndriodHttpClient not HttpClient, AndroidHttpClient does not store cookies by default so yeah, Any reason why you are using AndroidHttpClient over HttpClient?

Upvotes: 1

Related Questions