14

I have to load url in webview with sending some cookies. HOw to achieve this ?

I am doing following code..

CookieManager cookieManager;

CookieSyncManager.createInstance(PrivacyActivity.this);
cookieManager = CookieManager.getInstance();

cookieManager.setCookie("param", "value");
CookieSyncManager.getInstance().sync();

WebSettings webSettings = wv.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setBuiltInZoomControls(true);
wv.setWebViewClient(new WebViewClient() {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        return super.shouldOverrideUrlLoading(view, url);
    }
});
wv.loadUrl("https://example.com");

But not getting proper result. Just getting "https://example.com" as it is. Cookie does not works..

2 Answers 2

16

This solution works for me (Just add cookies to the CookieManager before loading the url and that's all):

WebView webview = (WebView) findViewById(R.id.webview);
...
CookieManager.getInstance().setCookie("http://example.com/", "key=value");
webview.loadUrl("http://example.com/");
Sign up to request clarification or add additional context in comments.

Comments

6

I solved problem as,,,

        WebView webview = (WebView) this.findViewById(R.id.wv_file);
        final WebSettings settings = webview.getSettings();
        settings.setJavaScriptEnabled(true);
        settings.setAppCacheEnabled(true);
        settings.setBuiltInZoomControls(true);
        settings.setPluginState(WebSettings.PluginState.ON);

        webview.setWebChromeClient(new WebChromeClient());

        CookieSyncManager.createInstance(ActivityName.this);
        CookieManager cookieManager = CookieManager.getInstance();
        cookieManager.removeSessionCookie();
        String cookieString = "param=value";
        cookieManager.setCookie(domain_of_url("like http://abc-site.com"), cookieString);
        CookieSyncManager.getInstance().sync();

        Map<String, String> abc = new HashMap<String, String>();
        abc.put("Cookie", cookieString);
        webview.loadUrl("http://abc-site.com/a1/namedFolder/file",
                abc);

2 Comments

what is the alternate for CookieSyncManager ? Also, do we need to add cookie manually? Can't we pass all cookies have to webview?
@Mr.India CookieManager now has a flush() method, which blocks until all cookies are sync'ed to disk.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.