简体   繁体   中英

Android webview read 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);

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

It was a quite late , but it might help someone

you can get the cookie value by this

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM