CookieManager.getCookie()始终返回null(版本< = API 28(Pie)) [英] CookieManager.getCookie() always returns null (Version <= API 28 (Pie))

查看:97
本文介绍了CookieManager.getCookie()始终返回null(版本< = API 28(Pie))的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在基于Java的本机android应用程序中使用标准的 android.webkit.Webview .用户在Webview内登录到Webapp.成功登录后,Web应用程序将存储带有访问令牌的cookie.我要访问此Cookie.

I use a standard android.webkit.Webview in a Java-Based native android app. Inside the Webview the User logs into a webapp. After successful login the webapp stores a cookie with the access token. I want to access this cookie.

cookie具有以下特征:

The cookie has the following characteristics:

  • HTTPS网址结构,例如: https://api.example.com
  • httpOnly 标志
  • 安全标志

在Android API级别29或API级别30上时,调用 CookieManager.getCookie("https://api.example.com")的工作原理与预期相同,并返回包含以下值的字符串:Cookie.

When on Android API Level 29 or API Level 30, calling CookieManager.getCookie("https://api.example.com") works like expected and returns a String containing the Value of the Cookie.

在Android API级别28或更低版本上,始终调用相同的方法 返回 null .尽管如此,调用 CookieManager.hasCookies()会返回 true ,并且 Webview 中的webapp可以正常运行的事实表明cookie 必须存在于某个地方.

When on Android API Level 28 or lower, calling the same method always returns null. Nevertheless calling the CookieManager.hasCookies() returns true and the fact that the webapp in Webview is working as expected shows that the cookie must exist somewhere.

我需要保留cookie以处理外部"邮件.Webview中未执行的下载和其他API调用.

I need to persist the cookie for handling "external" Downloads and other API Calls not executed within the Webview.

为什么在API 28下无法正常工作,我该如何解决?

已经尝试过的东西:

  • 在来自 WebChromeClient WebViewClient 的不同事件挂钩上调用方法 CookieManager.getCookie("https://api.example.com")代码>(例如, onPageStarted onPageFinished onProgressChanged shouldInterceptRequest shouldOverrideUrlLoading )
  • 在不同位置使用 CookieManager.flush()将cookie从内存刷新到持久性存储.
  • 通过拦截流量来提取cookie(使用现有的钩子,这太骇人听闻了)
  • Calling the method CookieManager.getCookie("https://api.example.com") on different event hooks from WebChromeClient, WebViewClient (e.g. onPageStarted, onPageFinished, onProgressChanged, shouldInterceptRequest, shouldOverrideUrlLoading)
  • Flushing the cookies from memory to persistent storage using CookieManager.flush() in different places.
  • Extracting the cookie by intercepting traffic (this is too hacky using existing hooks)

假设:

  • 线程?,28以下的API支持不完整?

推荐答案

在使用低于28的旧API版本时,标准的WebView实施低于72,或者标准的Chrome Webview.

When using older API Versions below 28, the standard WebView Implementation is below 72, or the standard Chrome Webview.

在版本72中,在使用SameSite Attibute解析Cookie时存在一个错误.副作用是,CookieManager不会将带有属性的Cookies移交.因此,需要在设备上放置更新的Webview实现(例如,通过Android WebView的PlayStore更新),否则getCookie(url)将始终返回null.

In Version 72 there is a Bug in parsing Cookies with the SameSite Attibute. As a sideeffect, the CookieManager does not hand over Cookies with the Attribute. Therefore a newer Webview Implementation needs to be placed on the device (e.g. via PlayStore Update of Android WebView), otherwise getCookie(url) will always return null.

这会影响使用默认Google图片的模拟器.因此,开发人员可能很难找到这一点.:)

This affects Emulators with the default Google Images. Therefore a dev may have a hard time finding this. :)

更多信息可在此处找到: https://bugs.chrome.org/p/chromium/issues/detail?id=780491

More Info can be found here: https://bugs.chromium.org/p/chromium/issues/detail?id=780491

这篇关于CookieManager.getCookie()始终返回null(版本< = API 28(Pie))的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆