How do I manage cookies with HttpClient in Android and/or Java?

Solution 1:

You need to use HttpContext. Set cookie store to context and pass context long with HttpGet/HttpPost in execute method. Hope this should help.

See example: Complete code can be found here

   // Create a local instance of cookie store
    CookieStore cookieStore = new BasicCookieStore();

    // Create local HTTP context
    HttpContext localContext = new BasicHttpContext();
    // Bind custom cookie store to the local context
    localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);

    HttpGet httpget = new HttpGet("http://www.google.com/"); 

    System.out.println("executing request " + httpget.getURI());

    // Pass local context as a parameter
    HttpResponse response = httpclient.execute(httpget, localContext);

Solution 2:

I was unable to get my own code working (I might work on it again later), but I found useful code here Android project using httpclient --> http.client (apache), post/get method and I am using the class built by Charlie Collins, which is similar to the Http Code in the ZXing Android example. I may eventually move to the ZXing code.