Android WebView not extracting all cookies on different devices

1.1k views Asked by At

I'm trying to extract the value of a cookie from a webpage after logging in. I am using a cookie helper class and the android.webkit.CookieManager to retreive cookies once the webpage has finished loading i.e. onPageFinished()

This all works fine and I can see the cookie names and values on my test devices, Galaxy S2 (4.0.4), Galaxy S3 (4.1.1) and a HTC Explorer (2.3). However, the same code is not functioning on a Sony Xperia (2.3)?

I have logged out the url to make sure I'm working from the correct page and for some reason I only get 3 cookies from the site on the Xperia, where I should be getting 7 (roughly).

Below is a simplified version of the code I'm running. Appreciate any responses. Thanks in advance!

WebViewActivity

@Override
public void onPageFinished(WebView view, String url) {
    if(!cookieCreated){
        CookieHelper cookieHelper = new CookieHelper(getApplicationContext());

        if(cookieHelper.processCookie(url)){
            cookieCreated = true;
        }
    }
}

CookieHelper

public CookieHelper(Context cont){
    this.cookieManager = CookieManager.getInstance();
}


public void processCookie(String url){
    if(manager.getCookie(url).contains("cookie_name_required")){
       Log.d(TAG, "Got cookie");
    }
    else{
       Log.d(TAG, "No cookie");
    }
}
1

There are 1 answers

3
Yousef Zakher On
public static void setCookies(Context context) {
    //gets all cookies from the HttpClient's cookie jar


    List<Cookie> cookies = httpClient.getCookieStore().getCookies();

    if (! cookies.isEmpty()) {

        CookieSyncManager.createInstance(context);
        CookieManager cookieManager = CookieManager.getInstance();
        //sync all the cookies in the httpclient with the webview by generating cookie string

        for (Cookie cookie : cookies) {

            Cookie sessionInfo = cookie;
            String cookieString = sessionInfo.getName() + "=" + sessionInfo.getValue() + "; domain=" + sessionInfo.getDomain();
            cookieManager.setCookie("zoboon.com", cookieString);
            CookieSyncManager.getInstance().sync();
        }

    } else {

        Log.i("httpclient","has no cookies");

    }
}