Android HttpClient and Cookies
Asked Answered
S

3

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

Septemberseptembrist answered 12/12, 2012 at 19:26 Comment(3)
You can find you answer here : https://mcmap.net/q/261228/-android-httpclient-cookieMagnesia
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 :/Septemberseptembrist
"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.Suspense
P
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);
 ...
Prehistory answered 10/4, 2013 at 11:46 Comment(2)
Great catch, it took us forever to figure this out.Talton
Hi Daniel, Im having the same problem but a bit different, can you maybe take a look to help me? :) https://mcmap.net/q/261229/-android-how-to-get-a-cookie-from-url-via-httpclient/3350765Edmundoedmunds
S
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

Septemberseptembrist answered 3/8, 2013 at 23:49 Comment(0)
O
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.

Obola answered 13/12, 2012 at 3:59 Comment(4)
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.Septemberseptembrist
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.Obola
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.Septemberseptembrist
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.Obola

© 2022 - 2024 — McMap. All rights reserved.