Inject Cookies into a WebView

android webview cookies not working
android webview share cookies
android webview set-cookie httponly
android webview clear cookies
android webview save cookies
cookies in android example
set-cookie in android application
javafx webview set cookies

I am calling a service to get Cookies as a JSONArray that looks like this


then parsing these cookies like this

Cookie cookie = new Cookie.Builder()

then injecting the cookies like this

    CookieManager cookieManager = CookieManager.getInstance();
        cookieManager.setAcceptThirdPartyCookies(webView, true);
    } else {

    if (cookies != null) {
        for (Cookie cookie : cookies) {
            String cookieString = + "=" + cookie.value() + "; domain=" + cookie.domain();
            cookieManager.setCookie(cookie.domain(), cookieString);

but when i load into the WebView cookies are not working, am i missing anything?

Please help

You can use the below code to set cookies.

web_view = findViewById(;
    CookieSyncManager cookieSyncManager = CookieSyncManager.createInstance(web_view.getContext());
    CookieManager cookieManager = CookieManager.getInstance();

    String cookie = cookieManager.getCookie(URL);

    Log.e(TAG, ""+cookie);
    web_view.setWebViewClient(new WebViewClient());


2019-09-16 15:20:21.412 31408-31408/com.sam.webviewtest E/MainActivity: uniqueId=10028
2019-09-16 15:20:22.204 31408-31408/com.sam.webviewtest E/SpannableStringBuilder: SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
2019-09-16 15:20:22.205 31408-31408/com.sam.webviewtest E/SpannableStringBuilder: SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length

For testing purpose, you can use below url

in this php file, it only output all the cookies

<pre><?php print_r( $_COOKIE ); ?></pre>

I've used the same url in my above example,

CookieManager API, In this article we will learn how to capture requests and get cookies when a web page is loaded into a WebView in Android. Where is the best place to inject cookies for subsequent requests? E.g. The initial page load is covered in the answer above, but what if there are links on the page that also lead to the same domain, and also need the same cookies injected into the request? didStartProvisionalNavigation? – Mason G. Zhwiti May 4 '15 at 16:34

This is what I worked with, it works perfectly fine:

    CookieManager.getInstance().setCookie(treeUrl, cookieString);            
    CookieManager.getInstance().setAcceptThirdPartyCookies(webView, true);

If you have to inject a JSON to cookies, just use yourJson.toString()

Capture Requests and Get Cookies of Web Page in a WebView in , To set cookies before Lollipop, use the CookieSyncManager and it's synchronous methods: CookieSyncManager. createInstance(webView. CookieManager cookieManager = CookieManager. getInstance(); cookieManager. Map<String, String> headers = new HashMap<>(); headers. put("name", "value"); web. In particular session cookies. This is due to a missing proper sync between the underlying WKHTTPCookieStore and the WebView . While this issue could probably only get fixed by Apple in the first place, there is a simple workaround available to get it working: Once a dummy cookie is placed into the WKHTTPCookieStore manually, the syncronization

Please try this one:

private fun setCookie(){
val cookieManager = CookieManager.getInstance()

val domain = ""

webView.webViewClient = WebViewClient()
webViewTest.settings.javaScriptEnabled = true



Controlling cookies, headers, and user agent in the Android WebView, We have a flow in our app where, if the webview attempts to navigate to a I'm creating web view after cookies injection and with sharing the same Pool. On iOS​  The WebView component includes the evalJS() method that you can use to inject JavaScript into a WebView. A few things to keep in mind: A few things to keep in mind: You should call evalJS() from the webview's load event so you're sure the page is done loading before you attempt to inject your code.

iOS 11.3: WKWebView cookie synchro…, How do I synchronize cookies in WK WebView? ◇Configuration. 【ios】 ios version: iOS9 Xcode:8.3.3 Swift3. 【Server】 WebServer:CentOS framework:​cakePHP 2.9  You can use InvokeScriptAsync with the JavaScript eval function to inject content into the web page. Here, the text of a XAML text box (nameTextBox.Text) is written to a div in an HTML page hosted in webView1.

How to synchronize cookies in WKWe…, I'm trying to inject a cookie in a web view. I used HTTPCookieStorage and saved my cookie there, but when I load the web view I want it always display the login  In Swift, it is possible to inject a JavaScript code into a WKWebView and execute it from your app. The below code example demonstrates how to: Load HTML file from a local file, Load JavaScript code from a local file, Create WKUserScript object, Inject JavaScript into an HTML document.

How to inject cookie in Web View : swift, or XSRF attack specific to android WebView is investigated. In. XSRF attack, the trusts of Event sniffing, hijacking, JavaScript injection and attacks through frame CookieManager class in Android manages cookies used by an application's  WebView with injected JS script. so the obvious thing we need to do is to allow webView to use JavaScript. since injecting code to an existing page is risky and not immune to changes on

  • Any critics on my answer?
  • Is the cookie.domain() the same for multiple values in the array? If so, it is being replaced like a Hashmap. See the docs for more info.