简体   繁体   中英

How To Get Cookies From WebView?

How To Get Cookies 在此处输入图片说明

the My Code, but Not From WebView

CookieManager cookieManager = CookieManager.getInstance();
cookieManager.setAcceptCookie(true);
cookieManager.setCookie("http://xx.xxx.xxx.com","mid=GO ; Domain=.xxx.com");

String cookie = cookieManager.getCookie("http://xx.xxx.xxx.com");

Log.d("VOGA", "cookie ------>"+cookie);
w.getSettings().setJavaScriptEnabled(true);
w.setWebViewClient(new WebViewClient());
w.loadUrl("http://xx.xx.xxx.com");
setContentView(w);

Instead of long and boring methods, I think you can get cookies from the WebView directly (for example, after user login) by:

@Override
public void onPageFinished(WebView view, String url){
  String myCookies = CookieManager.getInstance().getCookie(url);
}

you can use following code to read cookies :

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;    
}

Also refer this Android - extracting cookies after login in webview

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