Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - How to pass cookie to load url with webview?

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..

like image 421
Mind Android Avatar asked Mar 25 '14 14:03

Mind Android


1 Answers

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/");
like image 200
Kata Lune Avatar answered Oct 17 '22 09:10

Kata Lune