Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get cookies from webview with path and expiration date

I currently have a webview which get cookies in the onPageFinished

mWebview = (WebView) this.findViewById(R.id.myWebView);

    mWebview.setWebViewClient(new WebViewClient() {
        @Override
        public void onPageFinished(WebView view, String url) {
            String cookies = CookieManager.getInstance().getCookie(url);
            Log.d("Cookie", cookies);
        }
    });

    mWebview.loadUrl("http://www.google.com");

CookieManager.getCookie() only returns name and value pairs of the cookie.

Now I would like to get more information about that cookie such as the path and the expiration date ect...

Any idea of how can I extract all the "raw data" of the cookies?

like image 671
Lucas78 Avatar asked Dec 05 '16 18:12

Lucas78


People also ask

Does WebView save cookies?

WebViews automatically persist cookies into a android. webkit. CookieManager. Need to store cookies from a WebView into a java.

How do I check my cookies expiry date?

If you are using Chrome you can goto the "Resources" tab and find the item "Cookies" in the left sidebar. From there select the domain you are checking the set cookie for and it will give you a list of cookies associated with that domain, along with their expiration date.

Do WebViews share cookies?

Thankfully, UWP and iOS share their cookie containers automatically between the WebView and native http client, however Android does not.


1 Answers

You need to override the WebView's resource loading in order to have access the the response headers (the Cookies are sent as http headers). Depending on the version of Android you are supporting you need to override the following two methods of the WebViewClient:

mWebview.setWebViewClient(new WebViewClient() {

            @Override
            public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
                if (request != null && request.getUrl() != null && request.getMethod().equalsIgnoreCase("get")) {
                    String scheme = request.getUrl().getScheme().trim();
                    if (scheme.equalsIgnoreCase("http") || scheme.equalsIgnoreCase("https")) {
                        return executeRequest(request.getUrl().toString());
                    }
                }
                return null;
            }

            @Override
            public WebResourceResponse shouldInterceptRequest(WebView view, String url) {
                if (url != null) {
                    return executeRequest(url);
                }
                return null;
            }
        });

You can then retrieve the contents of the url yourself and give that to the WebView (by creating a new WebResourceResponse) or return null and let the WebView handle it (take into consideration that this make another call to the network!)

private WebResourceResponse executeRequest(String url) {
        try {
            URLConnection connection = new URL(url).openConnection();
            String cookie  = connection.getHeaderField("Set-Cookie");
            if(cookie != null) {
                Log.d("Cookie", cookie);
            }
            return null;
            //return new WebResourceResponse(connection.getContentType(), connection.getHeaderField("encoding"), connection.getInputStream());
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
like image 68
Rares Barbantan Avatar answered Oct 24 '22 06:10

Rares Barbantan