【问题标题】:Not able to set cookie for 10.0.2.2 on android emulator无法在 android 模拟器上为 10.0.2.2 设置 cookie
【发布时间】:2020-03-30 14:09:48
【问题描述】:

我正在尝试在 Oreo 上使用 CookieManager 设置 cookie(https://developer.android.com/reference/android/webkit/CookieManager#setCookie(java.lang.String,%20java.lang.String,%20android.webkit.ValueCallback%3Cjava.lang.Boolean%3E) 及以上用于 Webview, 它使用10.0.2.2:8080从我的笔记本电脑上运行的本地服务器加载(比如说localhost:8080

众所周知,对于安卓模拟器来说,localhost地址是由10.0.2.2引用的

据我所知,One 可以为 10.0.2.2 设置 cookie

还有没有办法调试cookie设置失败的原因? 我正在通过 chrome 检查器(chrome://inspect)检查 cookie 是否设置

这是用例(react-native,但它在后台使用常规 CookieManager):

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);
    });

【问题讨论】:

  • 如果你想知道的话,我也在刷新 cookie。 cookie 设置也适用于常规域,以及 cookie 设置适用于我检查的 4.4。
  • 如果您使用常规域的 IP,设置 cookie 对您有用吗?

标签: android react-native cookies android-emulator android-webview


【解决方案1】:

是否为域设置了 cookie,例如“localhost”或 10.0.2.2?即“域=本地主机”。

如果是这样,请尝试完全删除域部分,看看是否可以解决未设置 cookie 的问题。

除此之外,您能否展示您如何尝试使用 CookieManager 设置 cookie 的代码?

编辑:

我相信发布 android 版本 2.2 后,在使用 localhost 时,无法在 webview 中设置 cookie 或使用 localstorage。这是由于安全限制,以防止任何其他应用访问数据。

查看这些相关问题/答案:

Android webview cookie returns null

How to enable cookie in webview for local js file?

【讨论】:

  • 我正在为 10.0.2.2 设置 cookie,因为我的 url 本身就像 http://10.0.2.2:8080/mywebpage
  • @jayshah - 那么,您是否尝试将域设置为 10.0.2.2?你能发布你用来设置cookie的实际代码吗?另外 - 查看我的编辑...
猜你喜欢
  • 1970-01-01
  • 2018-08-21
  • 1970-01-01
  • 2014-08-29
  • 2022-01-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多