12

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?

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Lucas78
  • 324
  • 3
  • 12

1 Answers1

5

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;
    }
Rares Barbantan
  • 761
  • 4
  • 9
  • I tried this approach. However, I am seeing some inconsistency between what's in Set-Cookie and cookies in cookieManager. Simply speaking, I try to find a cookie and its information from raw response since cookiemanager only gives you key and value pair. However, I am seeing that cookie is showing up in cookiemanager after a certain point but never seen it in Set-Cookie field. please reference to my post https://stackoverflow.com/questions/49835438/android-webview-get-raw-cookie-information – Yao Apr 15 '18 at 22:02
  • 1
    In case anyone comes across this answer later, the documentation on the `getHeaderField()` method says if there are multiple headers with the same key only the last is returned. Use the `getHeaderFields()` method to get _all_ headers with that key. – Brian Dupuis Nov 17 '20 at 14:20