1

How To Get Cookies enter image description here

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);
Cœur
  • 34,719
  • 24
  • 185
  • 251
Frøst
  • 5
  • 1
  • 6

2 Answers2

4

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

Community
  • 1
  • 1
Shruti
  • 8,785
  • 12
  • 54
  • 93
3

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);
}
szholdiyarov
  • 377
  • 2
  • 11