本文介绍了Android webview阅读cookies的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码在网页视图中显示网页:

I have the following code to display a webpage in a webview:

WebView myWebView = (WebView) findViewById(R.id.webView1);
myWebView.loadUrl("http://the.url.com");
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);



现在我想读取webview的cookie。这是可能吗?

Now I want to read cookie of the webview. Is this possible?

推荐答案

这是一个很晚,但它可能会帮助某人

It was a quite late , but it might help someone

您可以通过此

public String getCookie(String siteName,String CookieName){
    String CookieValue = null;

    CookieManager cookieManager = CookieManager.getInstance();
    String cookies = cookieManager.getCookie(siteName);
    if(cookies != null){
        String[] temp=cookies.split(";");
        for (String ar1 : temp ){
            if(ar1.contains(CookieName)){
                String[] temp1=ar1.split("=");
                CookieValue = temp1[1];
            }
        }
     }
     return CookieValue;
}



编辑



注意事项:

Edit

Point to notice:

如果您正在载入 http://sitedomain.com $ c> www ), siteName www 将无法使用此方法。

If you are loading URL like http://sitedomain.com (without www), the siteName with www will not work with this method.

这篇关于Android webview阅读cookies的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-27 02:41