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

74,646

Solution 1

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.

Solution 2

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);
Share:
74,646
Alejandro Huerta
Author by

Alejandro Huerta

Updated on April 07, 2020

Comments

  • Alejandro Huerta
    Alejandro Huerta about 4 years

    I am trying to login to a site and maintain that session/cookie so that the server will recognize my login, but I am struggling to figure out a way of extracting the cookie from the response and setting into a request to maintain my login. I'm wondering if I should go about taking the header "Set-Cookie" or using a CookieStore. Any help is greatly appreciated. Here is my code that I have, with comments where I think the getHeader/getCookie methods would go.

    public class Http
    {
    DefaultHttpClient client = new DefaultHttpClient();
    HttpGet request;
    HttpEntity entity;
    HttpResponse response;
    HttpPost post;
    CookieStore cookieStore = new BasicCookieStore();
    HttpContext localContext = new BasicHttpContext();
    
    public static void setContext()
    {
        localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
    }
    
    public static void getPage(String url) throws Exception
    {
        request = new HttpGet(url);
        response = client.execute(request, localContext);
        PARSER.preParse(url, response);
    }
    
    public static HttpResponse postPage(List<NameValuePair> params, String host, String action) throws Exception
    {
        post = new HttpPost(host + action);
        post.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
    
        response = client.execute(post, localContext);
    
        entity = response.getEntity();
        if(entity != null)
        {
            entity.consumeContent();
        }
    
        return response;
    }
    
    
    public void destoyHttp()
    {
        client.getConnectionManager().shutdown();
    }
    }
    

    In hopes of others better understanding my confusion am adding code that I know DOES work and maintains a session but when I tried to move the code into my actual application it broke somewhere down the line.

    public class HttpClientTest extends Activity{
    
    DefaultHttpClient client = new DefaultHttpClient();
    HttpGet request;
    HttpEntity entity;
    List<Cookie> cookies;
    HttpResponse response;
    HttpPost post;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        try {
            getRequest();
        } catch (Exception e) {
            Log.d("My Activity", "Failed");
            e.printStackTrace();
        }
    }
    
    public void getRequest() throws Exception
    {
        final String TAG = "MyActivity";
        request = new HttpGet("http://gc.gamestotal.com/i.cfm?p=login&se=4");
        response = client.execute(request);
    
        String action = "i.cfm?&1028&p=login&se=4";
        String yourServer = "http://gc.gamestotal.com/";
        post = new HttpPost(yourServer + action);
    
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("nic", "myusername"));
        params.add(new BasicNameValuePair("password", "mypassword"));
        params.add(new BasicNameValuePair("server", "4"));
    
        post.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
    
    
        response = client.execute(post);
        entity = response.getEntity();
    
        if(entity != null){
            entity.consumeContent();
        }
    
        request = new HttpGet("http://gc.gamestotal.com/i.cfm?f=com_empire&cm=3");
    
        response = client.execute(request);
    
        if(entity != null)
        {
            entity.consumeContent();
        }    
    
    }
    
    }
    

    I know for a fact it works, and you can see that in this code I consumeContent() but adding that to the top code didn't seem to make a difference so I left it out. Any Ideas?

    Edit: I am still unable to get the code working in keeping a session going. I am posting the cookies from my current code, along with the cookies of the second code that is working. Perhaps someone will notice an issue, I certainly do not.

    08-31 06:53:50.318: VERBOSE/SFGC(496): - [version: 0][name: CFID][value: 26651316][domain: gc.gamestotal.com][path: /][expiry: Sun Sep 27 00:00:00 GMT+00:00 2037]
    08-31 06:53:50.329: VERBOSE/SFGC(496): - [version: 0][name: CFTOKEN][value: 96917381][domain: gc.gamestotal.com][path: /][expiry: Sun Sep 27 00:00:00 GMT+00:00 2037]
    08-31 06:53:50.329: VERBOSE/SFGC(496): - [version: 0][name: CFCLIENT_SFGC_106Y][value: CFTOKEN2%3D735125416524430%23COUNTRY%3D56%23DOWNLOADFLAG%3D0%23OCHAT%3D1%23PM%5FDATE%3D%7Bts+%272010%2D08%2D31+14%3A53%3A45%27%7D%23REGION%3D3%23SCREEN%3D800%23S%5FGC%5FIMAGELOC%3Di%2Fw%2F%23TIMEZONE%3D%2D600%23][domain: gc.gamestotal.com][path: /][expiry: Sun Sep 27 00:00:00 GMT+00:00 2037]
    08-31 06:53:50.339: VERBOSE/SFGC(496): - [version: 0][name: CFGLOBALS][value: HITCOUNT%3D2%23LASTVISIT%3D%7Bts+%272010%2D08%2D31+14%3A53%3A45%27%7D%23TIMECREATED%3D%7Bts+%272010%2D08%2D31+14%3A53%3A39%27%7D%23][domain: gc.gamestotal.com][path: /][expiry: Sun Sep 27 00:00:00 GMT+00:00 2037]
    
    08-31 06:53:51.938: VERBOSE/SFGC(496): - [version: 0][name: CFID][value: 26651316][domain: gc.gamestotal.com][path: /][expiry: Sun Sep 27 00:00:00 GMT+00:00 2037]
    08-31 06:53:51.938: VERBOSE/SFGC(496): - [version: 0][name: CFTOKEN][value: 96917381][domain: gc.gamestotal.com][path: /][expiry: Sun Sep 27 00:00:00 GMT+00:00 2037]
    08-31 06:53:51.938: VERBOSE/SFGC(496): - [version: 0][name: CFCLIENT_SFGC_106Y][value: CFTOKEN2%3D735125416524430%23COUNTRY%3D56%23DOWNLOADFLAG%3D0%23OCHAT%3D1%23PM%5FDATE%3D%7Bts+%272010%2D08%2D31+14%3A53%3A45%27%7D%23REGION%3D3%23SCREEN%3D800%23S%5FGC%5FIMAGELOC%3Di%2Fw%2F%23TIMEZONE%3D%2D600%23][domain: gc.gamestotal.com][path: /][expiry: Sun Sep 27 00:00:00 GMT+00:00 2037]
    08-31 06:53:51.948: VERBOSE/SFGC(496): - [version: 0][name: CFGLOBALS][value: HITCOUNT%3D3%23LASTVISIT%3D%7Bts+%272010%2D08%2D31+14%3A53%3A46%27%7D%23TIMECREATED%3D%7Bts+%272010%2D08%2D31+14%3A53%3A39%27%7D%23][domain: gc.gamestotal.com][path: /][expiry: Sun Sep 27 00:00:00 GMT+00:00 2037]
    
    
    08-31 06:52:41.628: DEBUG/MyActivity(469): - [version: 0][name: CFID][value: 26651274][domain: gc.gamestotal.com][path: /][expiry: Sun Sep 27 00:00:00 GMT+00:00 2037]
    08-31 06:52:41.638: DEBUG/MyActivity(469): - [version: 0][name: CFTOKEN][value: 58361320][domain: gc.gamestotal.com][path: /][expiry: Sun Sep 27 00:00:00 GMT+00:00 2037]
    08-31 06:52:41.638: DEBUG/MyActivity(469): - [version: 0][name: CFCLIENT_SFGC_106Y][value: CFTOKEN2%3D735125815099420%23COUNTRY%3D56%23DOWNLOADFLAG%3D0%23OCHAT%3D1%23PM%5FDATE%3D%7Bts+%272010%2D08%2D31+14%3A52%3A36%27%7D%23REGION%3D3%23SCREEN%3D800%23S%5FGC%5FIMAGELOC%3Di%2Fw%2F%23TIMEZONE%3D%2D600%23][domain: gc.gamestotal.com][path: /][expiry: Sun Sep 27 00:00:00 GMT+00:00 2037]
    08-31 06:52:41.648: DEBUG/MyActivity(469): - [version: 0][name: CFGLOBALS][value: HITCOUNT%3D2%23LASTVISIT%3D%7Bts+%272010%2D08%2D31+14%3A52%3A36%27%7D%23TIMECREATED%3D%7Bts+%272010%2D08%2D31+14%3A52%3A33%27%7D%23][domain: gc.gamestotal.com][path: /][expiry: Sun Sep 27 00:00:00 GMT+00:00 2037]
    
    08-31 06:52:44.138: DEBUG/MyActivity(469): - [version: 0][name: CFID][value: 26651274][domain: gc.gamestotal.com][path: /][expiry: Sun Sep 27 00:00:00 GMT+00:00 2037]
    08-31 06:52:44.138: DEBUG/MyActivity(469): - [version: 0][name: CFTOKEN][value: 58361320][domain: gc.gamestotal.com][path: /][expiry: Sun Sep 27 00:00:00 GMT+00:00 2037]
    08-31 06:52:44.138: DEBUG/MyActivity(469): - [version: 0][name: CFCLIENT_SFGC_106Y][value: CFTOKEN2%3D735125815099420%23COUNTRY%3D56%23DOWNLOADFLAG%3D0%23OCHAT%3D1%23PM%5FDATE%3D%7Bts+%272010%2D08%2D31+14%3A52%3A36%27%7D%23REGION%3D3%23SCREEN%3D800%23S%5FGC%5FIMAGELOC%3Di%2Fw%2F%23TIMEZONE%3D%2D600%23][domain: gc.gamestotal.com][path: /][expiry: Sun Sep 27 00:00:00 GMT+00:00 2037]
    08-31 06:52:44.138: DEBUG/MyActivity(469): - [version: 0][name: CFGLOBALS][value: HITCOUNT%3D4%23LASTVISIT%3D%7Bts+%272010%2D08%2D31+14%3A52%3A38%27%7D%23TIMECREATED%3D%7Bts+%272010%2D08%2D31+14%3A52%3A33%27%7D%23][domain: gc.gamestotal.com][path: /][expiry: Sun Sep 27 00:00:00 GMT+00:00 2037]
    
  • Alejandro Huerta
    Alejandro Huerta over 13 years
    Ok, how would I go about getting the header? Do I take it from the response, such as, response.getHeader("Set-Cookie")? Because I have tried to do something similar but there is only a getHeaders(); method which I guess returns an array? I'd have to look it up again.
  • Alejandro Huerta
    Alejandro Huerta over 13 years
    Ok I managed to use the Header[] object and have successfully set the headers including the Cookie header but that didn't seem to solve my issue. I have edited my question with code that DOES work that I made, but for some reason as I moved it over to my actual application it broke. FYI the working code does nothing with headers, it just works? lol
  • YoK
    YoK over 13 years
    Ok ;) I managed to find something which I see missing from your code and updated (rather changed ) my answer.
  • Alejandro Huerta
    Alejandro Huerta over 13 years
    I appreciate the quick responses. I added the missing code but still no luck. Could splitting up the GET and POST section into 2 different methods somehow cause the client to no retain a session?
  • YoK
    YoK over 13 years
    did you try to set cookiestore to HttpContext and passing HttpContext along with your method in execute of httpclient ?
  • Alejandro Huerta
    Alejandro Huerta over 13 years
    Yep, I created a method that sets the CookieStore to the context, and then made sure that I passed along the context client.execute(request, local context), but still no luck. I have no idea what it is at this point.
  • YoK
    YoK over 13 years
    Strange... I am out now but will certainly give it a try sometime in next few days (when I am back). Can you post updated code ?
  • Alejandro Huerta
    Alejandro Huerta over 13 years
    Yes I will do just that, again I appreciate the help.
  • YoK
    YoK over 13 years
    In your code I don't see "setContext" defined being called anywhere ? Also why everything in your class static ? If this is only for testing then its fine.
  • Alejandro Huerta
    Alejandro Huerta over 13 years
    setContext is called from the main activity at startup. It calls these methods in this order, setContext(); and getPage(url); at onCreate. Then when I press the login button, postPage(params, host, action). At compile I get a, cannot make a static reference to non static method, when I do HTTP.getPage(url), therefore the static.
  • Alejandro Huerta
    Alejandro Huerta over 13 years
    I've since edited and updated my code and have gotten rid of all static declarations. Everything compiles and runs, but of course no luck with the session issue.
  • Ovidiu Latcu
    Ovidiu Latcu almost 12 years
    Will this persist acroos application re-start ?
  • YoK
    YoK almost 12 years
    @Ovidiu Latcu Yes.. but for sessions your server needs to support session persistence.