Android Http get Session Cookie

40,188

Solution 1

Ok the code above actually does work, I just didn't retrieve the header correctly to see all the fields. The issue was I had an invalid session Id but the webserver I was using has the WORST error handling mechanisms :-) thanks for listening!

Solution 2

Try getCookieStore().addCookie() on your DefaultHttpClient object, before sending the request.

Share:
40,188
Blundell
Author by

Blundell

Software Engineer and Android addict Twitter @blundell_apps StackOverflow Career Profile My Android Developer Blog for Tutorials LinkedIn: http://uk.linkedin.com/in/blundell Google Developer Expert for Android & AndroidThings Author of the book Learning Android Application Testing

Updated on May 10, 2020

Comments

  • Blundell
    Blundell almost 4 years

    I didn't really want to post here as there is so much information on the net, but I've trawled the depths and can't figure it out.

    Ok so I can't get this to work in two scenario's hopefully the answer is the same for both.

    My problem is I set the request header but it doesn't seem to send it.

    I have a session id s=e32ff223fwefd3 , and I want to store this under "Cookie" , but it doesn't seem to be working.

    Here is the quickest code example I have

    import org.apache.http.client.HttpClient;
    import org.apache.http.client.methods.HttpGet;
    import org.apache.http.HttpResponse;
    
    private static String sessionCookie = "s=12342342352354234";
    
    public static void get(String url) {
        HttpClient client = new DefaultHttpClient();
        HttpGet request = new HttpGet(url);
            if(sessionCookie != null){
                Log.d(TAG, "Setting Cookie: "+sessionCookie);
                request.setHeader("Cookie", sessionCookie);
            } else {
                Log.i(TAG, "Null session request get()");
            }
            HttpResponse response = client.execute(request);
    
            Header[] headers = response.getAllHeaders();
            for (int i=0; i < headers.length; i++) {
                Header h = headers[i];
                Log.i(TAG, "Header names: "+h.getName());
                Log.i(TAG, "Header Value: "+h.getValue());
            }
    }
    

    So when my response comes out the other side it doesn't have my s=232342w3f23f id attached to it!

    I think I've explained this right, any help is appreciated

  • Blundell
    Blundell over 13 years
    I had to change from org.apache.http.client.HttpClient; to org.apache.http.impl.client.AbstractHttpClient; in order to call getCookieStore() , but even then I can't see how to instantiate a cookie? It's doesn't seem possible, I just want to set a header attribute...
  • CommonsWare
    CommonsWare over 13 years
    @Blundell: My point is that your Cookie header may be trumped by HttpClient's built-in cookie support. To create a Cookie, try ` org.apache.http.impl.cookie.BasicClientCookie`. See: hc.apache.org/httpcomponents-client-ga/tutorial/html/…