【问题标题】:Set-Cookie header in Ajax response doesn't work with android webViewAjax 响应中的 Set-Cookie 标头不适用于 android webView
【发布时间】:2017-11-16 21:00:41
【问题描述】:

我已经阅读答案、问题、论坛和博客 2 天了,但无法解决我的问题。

我有一个 AngularJS webApp 在带有 WebView 的原生 (SDK) Android 应用中工作。

一切似乎都正常,但是当我的应用程序调用以“Set-Cookie”标头响应的服务时,这些 cookie 没有在 web 视图中设置,当然也不会在下一次调用中发送,所以我的应用程序爆炸了。

该应用程序在浏览器和科尔多瓦中完美运行,“document.cookie”返回一个空字符串,所以我知道这是它失败的地方。

我的 webview 是这样初始化的:

public class Main extends AppCompatActivity {
...

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    WebView webview = (WebView) findViewById(R.id.webview);
    webview.setWebViewClient(new mWebViewClient());

    webview.getSettings().setAppCacheEnabled(true);
    webview.getSettings().setDomStorageEnabled(true);
    webview.getSettings().setSavePassword(true);
    webview.getSettings().setAllowFileAccessFromFileURLs(true);
    webview.getSettings().setAllowContentAccess(true);
    webview.getSettings().setJavaScriptEnabled(true);
    webview.getSettings().setUserAgentString(...);
    webview.loadUrl("file:///android_asset/www/index.html");

    javascriptAPI = new JavascriptAPI(this);
    webview.addJavascriptInterface(javascriptAPI, "jsAPI");
    ...
  }
  ...
}

我也尝试过使用这里和那里的代码块,例如:

        CookieSyncManager.createInstance(this);
        CookieSyncManager.getInstance().run();
        CookieManager.getInstance().setAcceptCookie(true);

但它也不起作用。

我的应用程序正在调用一些使用重定向的服务,因此从 JS 获取 cookie 并设置它们不是一个选项,我需要 webview 像浏览器一样工作。

这是我的第一个 SDK Android 应用程序,我完全不确定哪些信息可能与回答问题相关(清单、jsAPI 等),所以不要犹豫,我会发布任何其他需要的信息

已解决

谢谢@Mark,这修正了我的代码:

(在 Activity onCreate 内)

...
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
    CookieSyncManager.createInstance(this);
}
 ^^^^^^^
  THIS


WebView webview = (WebView) findViewById(R.id.webview);

CookieSyncManager.createInstance(this);
CookieSyncManager.getInstance().run();
CookieManager.getInstance().setAcceptCookie(true);
CookieManager.getInstance().setAcceptFileSchemeCookies(true); <- THIS
CookieManager.getInstance().setAcceptThirdPartyCookies(webview, true); <- THIS

webview.setWebViewClient(new mWebViewClient());
webview.getSettings().setAppCacheEnabled(true);
...

【问题讨论】:

  • 这应该有你的答案 - stackoverflow.com/questions/33998688/…
  • 我已经看到了,但我看到的是它拦截了请求,我必须拦截响应才能设置 cookie:/ ALSO,因为我的应用程序只执行 Ajax 查询'shouldOverride...' 方法什么都不做,iirc
  • 我认为您可能错过了其中的部分内容,所以我会为您发布答案。

标签: java android http webview sdk


【解决方案1】:

您需要担心三种 acceptCookie 方法,并且需要考虑跨多个 Android 版本的不同行为。

1) 我注意到您的初始网址是 file:// 网址。您可能需要致电CookieManager.getInstance().setAcceptFileSchemeCookies(true);

2) 在 Android Lollipop 及更高版本中,您可能需要致电 CookieManager.getInstance().setAcceptThirdPartyCookies(webView, true);

3) 对于 Android 版本

首先,通过将 CookieSyncManager 添加到您的应用程序类的 onCreate() 或扩展这个来确保在应用程序启动期间初始化 CookieSyncManager。

public class CookieApplication extends Application {
   @Override
   public void onCreate() {
      super.onCreate();
      if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
         CookieSyncManager.createInstance(this);
      }
   }
}

其次,您需要为 pre-lollipop 设备设置同步,最后启用 cookie。这是一个您可以扩展的示例活动,它为棒棒糖之前的设备设置所有同步,并提供一个enablesCookies(webView),它应该打开所有 cookie 类型,无论 Android 版本如何(请注意,调用这些时需要考虑安全性方法!)。注意所有的 CookieManager.getInstance().set*Cookie*() 方法必须在 webView 初始化之后调用。

public class AppCompatCookieActivity extends AppCompatActivity {

   @Override
   public void onResume() {
      super.onResume();
      // start cookie syncing for pre-lollipop devices
      if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
         CookieSyncManager.getInstance().startSync();
      }
   }

   @Override
   public void onPause() {
      // stop cookie syncing for pre-lollipop devices
      if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
         CookieSyncManager.getInstance().sync();
      }
      super.onPause();
   }

   public final void enableCookies(WebView webView) {
      // enable javascript (has security implications!)
      webView.getSettings().setJavaScriptEnabled(true);

      // enable cookies.
      CookieManager.getInstance().setAcceptCookie(true);

      // enable fileScheme cookies (has security implications!)
      // it is recommended to comment this out should you find it not necessary
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR1) {
         CookieManager.getInstance().setAcceptFileSchemeCookies(true);
      }

      // enable third party cookies
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
          CookieManager.getInstance().setAcceptThirdPartyCookies(webView, true);
      }
   }
}

【讨论】:

    猜你喜欢
    • 2017-08-25
    • 2016-05-22
    • 2019-08-01
    • 2015-08-15
    • 2020-02-02
    • 1970-01-01
    • 2021-06-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多