user1041858
user1041858

Reputation: 957

WebView.loadUrl(url, headers) not working in android

I am setting the cookie in headers and call WebView.loadUrl() with this header but it(Cookie in header) will not work on any android device except 4.4. I have test it on android versions 4.2, 4.3, 4.4, 5.0 and 5.1.

webView = (WebView) findViewById(R.id.web_view);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setDomStorageEnabled(true);
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setDisplayZoomControls(false);


HashMap <String, String> extraHeaders = new HashMap<String, String>();
extraHeaders.put("Cookie", "{cookie value}");


webView.setWebViewClient(new WebViewClient() {
    public boolean shouldOverrideUrlLoading(WebView view, String url){
            view.loadUrl(url, extraHeaders);
                return false;
        }
});

webView.loadUrl(url, extraHeaders);

Upvotes: 14

Views: 10787

Answers (7)

SoH
SoH

Reputation: 2190

If you are using Android Lollipop i.e. SDK 21, then:

CookieManager.getInstance().setAcceptCookie(true);

won't work. You need to use:

CookieManager.getInstance().setAcceptThirdPartyCookies(true);

I ran into same issue and the above line worked as a charm.

Upvotes: 1

kuljeet singh
kuljeet singh

Reputation: 2852

This is just a quick post about adding cookies to a web view. If you’ve ever tried to do this the way most people have said that it should be done, you’ve failed miserably and found this post. :)

The way it’s supposed to work is you set the cookie on the CookieManager and then tell the CookieSyncManager to sync.

CookieManager.getInstance().setCookie(domain, value);
CookieSyncManager.getInstance().sync();

I’ve never got this to work as described. With or without async tasks waiting for the threads to catch up.

Instead, I just add the cookie in the header of all the loadUrl calls.

Map<String, String> headers = new HashMap<String, String>();
headers.put("Cookie", "cookieName=cookieValue;domain=domain.com;path=/;Expires=Thu, 2 Aug 2021 20:47:11 UTC;");
webView.loadUrl("myurl.com", headers );

Caveat: I only need to initially load the appropriate cookie for the request, if you want to cover nested calls from inside the browser, you need to override shouldOverrideUrlLoading.

webView.setWebViewClient(new WebViewClient() {
     @Override
     public boolean shouldOverrideUrlLoading(WebView view, String url) {
         view.loadUrl(url, headers);
         return false;
     }
});

If you need to inject the cookie for all requests(including images, js, etc), you’re going to need to override shouldInterceptRequest,

Upvotes: 6

user1041858
user1041858

Reputation: 957

I got the solution for the issue by creating the custom cookie manager:

public class WebkitCookieManagerProxy extends CookieManager {
    private android.webkit.CookieManager webkitCookieManager;
    public WebkitCookieManagerProxy() {
        this(null, null);
    }
    public WebkitCookieManagerProxy(CookieStore store, CookiePolicy cookiePolicy) {
        super(null, cookiePolicy);
        this.webkitCookieManager = android.webkit.CookieManager.getInstance();
    }
    @Override
    public void put(URI uri, Map<String, List<String>> responseHeaders) throws IOException {
        // make sure our args are valid
        if ((uri == null) || (responseHeaders == null)) return;
        // save our url once
        String url = uri.toString();
        // go over the headers
        for (String headerKey : responseHeaders.keySet()) {
            // ignore headers which aren't cookie related
            if ((headerKey == null) || !(headerKey.equalsIgnoreCase("Set-Cookie2") || headerKey.equalsIgnoreCase("Set-Cookie"))) continue;
            // process each of the headers
            for (String headerValue : responseHeaders.get(headerKey)) {
                this.webkitCookieManager.setCookie(url, headerValue);
            }
        }
    }
    @Override
    public Map<String, List<String>> get(URI uri, Map<String, List<String>> requestHeaders) throws IOException {
        // make sure our args are valid
        if ((uri == null) || (requestHeaders == null)) throw new IllegalArgumentException("Argument is null");
        // save our url once
        String url = uri.toString();
        // prepare our response
        Map<String, List<String>> res = new java.util.HashMap<String, List<String>>();
        // get the cookie
        String cookie = this.webkitCookieManager.getCookie(url);
        if (cookie != null) res.put("Cookie", Arrays.asList(cookie));
        return res;
    }
    @Override
    public CookieStore getCookieStore() {
        // we don't want anyone to work with this cookie store directly
        throw new UnsupportedOperationException();
    }
}

And initialize the custom cookie manager in Application class or when application starts as:

android.webkit.CookieSyncManager.createInstance(this);
android.webkit.CookieManager.getInstance().setAcceptCookie(true);
WebkitCookieManagerProxy coreCookieManager = new WebkitCookieManagerProxy(null, java.net.CookiePolicy.ACCEPT_ALL);
java.net.CookieHandler.setDefault(coreCookieManager);

Upvotes: 2

rahul.ramanujam
rahul.ramanujam

Reputation: 5628

To set cookie use the following method

CookieManager.getInstance().setCookie(BuildConfig.BASE_SERVER_ENDPOINT,COOKIE_VAL);

Make sure the base endpoint matches the base url of the links opened in the webview.

To remove cookie

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                CookieManager.getInstance().removeAllCookies(null);
            } else {
                CookieManager.getInstance().removeAllCookie();
            }

Upvotes: 6

SureshCS50
SureshCS50

Reputation: 3768

if you are using Android Lollipop, then

CookieManager.getInstance().setAcceptCookie(true);

won't work. You need to use

CookieManager.getInstance().setAcceptThirdPartyCookies(true);

Upvotes: 6

Cristiana Chavez
Cristiana Chavez

Reputation: 11529

Have you tried this

   CookieManager.getInstance().setAcceptCookie(true);

Upvotes: 6

Maxime
Maxime

Reputation: 1392

It's beceause of Cookie Policy, to fix it, you should add this :

public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    super.init();

    // Allow third party cookies for Android Lollipop
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        WebView webView = (WebView)super.appView;
        CookieManager cookieManager = CookieManager.getInstance();
        cookieManager.setAcceptThirdPartyCookies(webView,true);
    }

    super.loadUrl(Config.getStartUrl());
}

Upvotes: 5

Related Questions