2

I am trying to set cookie using CookieManager on Oreo (https://developer.android.com/reference/android/webkit/CookieManager#setCookie(java.lang.String,%20java.lang.String,%20android.webkit.ValueCallback%3Cjava.lang.Boolean%3E) and above for a Webview, which loads from a local server running on my laptop (let's say localhost:8080) using 10.0.2.2:8080

As we all know, For an android emulator, localhost address is referred by 10.0.2.2

As far as I recall One was able to set cookies for 10.0.2.2

Also Is there a way to debug why a cookie setting failed? I am checking cookies are set or not via chrome inspector (chrome://inspect)

Here is the use case (react-native but it uses regular CookieManager under the hood) :

import CookieManager from '@react-native-community/cookies';

// Set cookies from a response header
// This allows you to put the full string provided by a server's Set-Cookie 
// response header directly into the cookie store.
CookieManager.setFromResponse(
  'http://10.0.2.2:8080', 
  'user_session=abcdefg; path=/; expires=Thu, 1 Jan 2030 00:00:00 -0000; secure; HttpOnly')
    .then((res) => {
      // `res` will be true or false depending on success.
      console.log('CookieManager.setFromResponse =>', res);
    });
jay shah
  • 903
  • 6
  • 21
  • I am also flushing the cookies if you are wondering. Also cookie setting is working for regular domains, as well as cookie setting is working on 4.4 I checked. – jay shah Mar 22 '20 at 13:41
  • Does setting a cookie work for you if you use IP of a regular domain? – Enselic Mar 27 '20 at 08:15

1 Answers1

0

Is the cookie being set for a domain eg 'localhost' or 10.0.2.2? i.e. 'domain=localhost'.

If so try removing the domain portion completely to see if that resolves the issue of the cookie not being set.

Other than that can you show the code of how you are attempting to set the cookie with CookieManager?

EDIT:

I believe that post android version 2.2 it is not possible in a webview to set a cookie, or to use localstorage, when using the localhost. This is due to a security restriction to prevent any other apps accessing the data.

See these related questions/answers:

Android webview cookie returns null

How to enable cookie in webview for local js file?

Fraser
  • 15,275
  • 8
  • 53
  • 104
  • I am setting cookie for 10.0.2.2 as in my url itself is like `http://10.0.2.2:8080/mywebpage` – jay shah Mar 29 '20 at 18:20
  • @jayshah - so, are you trying to set the domain to 10.0.2.2? Can you post the actual code you are using to set the cookie? Also - see my edit... – Fraser Mar 29 '20 at 21:16