7

I have a problem with the HttpClient in Android: By using the following code, I want to use the cookies which are already set before by logging in through a webview. So the login data should be there and is indeed there, I tested it. But when I use the cookies in an httppost or httpget it doesn't use the login data. but these cookies actually should be enough to receive that page for which a login is necessary, shouldn't they? I'm not really sure if I need to send the cookies in a special way to the server or so or if it is enough to load it into the httpcontext. Here is the code:

DefaultHttpClient httpclient = new DefaultHttpClient();
CookieStore lCS = new BasicCookieStore();


if (CookieManager.getInstance().getCookie(pUrl) != null) {  
    String cookieString = CookieManager.getInstance().getCookie(pUrl);

    String[] urlCookieArray = cookieString.split(";");
    for (int i = 0; i < urlCookieArray.length; i++) {           
        System.out.println(urlCookieArray[i]);          
        String[] singleCookie = urlCookieArray[i].split("=");
        Cookie urlCookie = new BasicClientCookie(singleCookie[0], singleCookie[1]);
        lCS.addCookie(urlCookie);           
    }

}

HttpContext localContext = new BasicHttpContext();
httpclient.setCookieStore(lCS);
localContext.setAttribute(ClientContext.COOKIE_STORE, lCS);

HttpPost httppost = new HttpPost(pUrl);        


    // get the url connection       
try {

    StringBuilder sb = new StringBuilder();     
    HttpResponse response = httpclient.execute(httppost, localContext);     
    InputStream is = response.getEntity().getContent();         
    InputStreamReader isr = new InputStreamReader(is);          

And if I run the code I only receive the login page of that site, so it didn't accept the cookie.

Thanks for help in advance

Greets, timo

timothy3001
  • 765
  • 2
  • 8
  • 17
  • You can find you answer here : http://stackoverflow.com/questions/4210846/android-httpclient-cookie – Guillaume Dec 12 '12 at 19:59
  • No I cannot. That's exactly what I've tried. I even tried to set the header in that kind of way. The only thing I didn't implement is that thing with the entity, because actually I have no data to post (except for the cookies). and the other post didn't help me either :/ – timothy3001 Dec 12 '12 at 20:19
  • "I want to use the cookies which are already set before by logging in through a webview" -- the cookies used by HttpClient are independent of the cookies used by any `WebView`, as `WebView` does not use HttpClient. – CommonsWare Dec 12 '12 at 20:52

3 Answers3

8

I had the same problem and I used similar approach as in the question with no luck. The thing that made it work for me was to add the domain for each copied cookie. (BasicClientCookie cookie.setDomain(String))

My util function:

public static BasicCookieStore getCookieStore(String cookies, String domain) {
    String[] cookieValues = cookies.split(";");
    BasicCookieStore cs = new BasicCookieStore();

    BasicClientCookie cookie;
    for (int i = 0; i < cookieValues.length; i++) {
        String[] split = cookieValues[i].split("=");
        if (split.length == 2)
            cookie = new BasicClientCookie(split[0], split[1]);
        else
            cookie = new BasicClientCookie(split[0], null);

        cookie.setDomain(domain);
        cs.addCookie(cookie);
    }
    return cs;
}

 String cookies = CookieManager.getInstance().getCookie(url);
 BasicCookieStore lCS = getCookieStore(cookies, MyApp.sDomain);

 HttpContext localContext = new BasicHttpContext();
 DefaultHttpClient httpclient = new DefaultHttpClient();
 httpclient.setCookieStore(lCS);
 localContext.setAttribute(ClientContext.COOKIE_STORE, lCS);
 ...
Daniel Backman
  • 5,121
  • 1
  • 32
  • 37
  • Great catch, it took us forever to figure this out. – trev9065 Aug 01 '13 at 21:09
  • Hi Daniel, Im having the same problem but a bit different, can you maybe take a look to help me? :) http://stackoverflow.com/q/25204423/3350765 – Lara Aug 08 '14 at 13:55
1

if you still have this problem, be careful with the given cookies, some might be malformed, check these two sites out:

http://www.codeproject.com/Articles/3106/On-The-Care-and-Handling-of-Cookies

this one helped me: Getting "Set-Cookie" header

Community
  • 1
  • 1
timothy3001
  • 765
  • 2
  • 8
  • 17
0

It seems you are copying the cookies correctly, and generally you don't need to do anything special for HttpClient to send the cookies. However, some of those may be bound to a session, and when you open a new connection with HttpClient you open a new session. The server will probably ignore cookies that don't match the current session. This might work if the session ID is in a cookie and you are able to get into the same session, but you really need to know exactly what the server does.

Nikolay Elenkov
  • 52,576
  • 10
  • 84
  • 84
  • I always thought that the SessionID IS saved in the cookies. Because I "analyzed" the cookies of a specific site already and came to the conclusion that there are 3 cookies. one is something with purity or so, rather unimportant, one is a generated number, which expires when the session is left and the last one, which is the important one i guess, is also a strange and human unreadable text BUT it expires after thirty days. I think that is the important one which makes the site remember the login and starts a session without alogin. – timothy3001 Dec 13 '12 at 14:59
  • Usually it is, yes. But it might be in a HTTP parameter as well. You really need to know what the server does, and if that is not an option, you just have to try different things to see what works. – Nikolay Elenkov Dec 13 '12 at 15:11
  • So you mean for example edit the header of that httpGet or httppost could help, right? Well ok thank you very much, then I have a new approach, because I always thought there would be something wrong with my code. – timothy3001 Dec 13 '12 at 15:52
  • Capture and replay a browser session with some tool. If that doesn't work, you are out of luck. If it does, try to emulate it as closely as possible in your code. – Nikolay Elenkov Dec 13 '12 at 15:55