Android WebView无法提取不同设备上的所有Cookie

时间:2012-10-23 18:59:00

标签: android cookies android-webview cookiemanager

我正在尝试在登录后从网页中提取cookie的值。我正在使用cookie帮助程序类和android.webkit.CookieManager在网页加载后检索cookie,onPageFinished()

这一切都运行正常,我可以在我的测试设备,Galaxy S2(4.0.4),Galaxy S3(4.1.1)和HTC Explorer(2.3)上看到cookie名称和值。但是,相同的代码在Sony Xperia(2.3)上无法正常运行?

我已经注销了网址,以确保我在正确的页面上工作,出于某种原因,我只能从Xperia网站上获得3个cookie,我应该得到7个(大致)。

下面是我正在运行的代码的简化版本。感谢任何回复。提前谢谢!

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 个答案:

答案 0 :(得分:0)

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");

    }
}