Android webview reads 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 the webview cookie. Is it possible?

+6
source share
1 answer

It was pretty late, but it could help someone

you can get a cookie 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

Note:

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

+22
source

Source: https://habr.com/ru/post/949467/


All Articles