【问题标题】:WebView is not using Cookies for loading imagesWebView 不使用 Cookies 来加载图像
【发布时间】:2015-03-05 06:13:09
【问题描述】:

当我希望我的 WebView 加载需要 Cookie 的图像时遇到问题。

我已在“CookieManager”上设置了我的 cookie

final android.webkit.CookieManager instance = android.webkit.CookieManager.getInstance();
instance.setAcceptCookie(true);
instance.setCookie(".example.fr", mCookies, new ValueCallback<Boolean>() {
    @Override
    public void onReceiveValue(final Boolean value) {
        loadWebView();
    }
});

然后,WebView 会加载自定义 HTML 字符串,因为应用正在生成正确的 HTML。

private void loadWebView() {
    // this string is an example of a generated HTML 
    String htmlContent = 
        "<!DOCTYPE html>" +
        "<html><head>" + 
        "<link rel=\"stylesheet\" href=\"css/style.css\" type=\"text/css\" media=\"screen, projection\"/></head>" +
        "<body><img src=\"www.example.fr/img.jpg\"/></body></html>";
    mWebView.loadDataWithBaseUrl("file:///android_asset/", htmlContent, "text/html", "UTF-8", null);
}

我尝试使用 Charles Proxy 代理网络调用,我注意到对 www.example.fr/img.jpg 的请求在标头中没有设置 cookie。但是,当我使用 Chrome 调试检查 WebView 时,我可以看到 Cookie 正确地位于资源选项卡下。

好像没有用于图片下载。

关于使用 Cookie 进行资源下载的 WebView 有什么提示或建议吗?

谢谢。

【问题讨论】:

    标签: android cookies webview


    【解决方案1】:

    我遇到了同样的问题,它与以下更改有关: https://developer.android.com/about/versions/android-5.0-changes.html#BehaviorWebView

    要修复它,您需要设置:

    CookieManager.getInstance().setAcceptThirdPartyCookies(mWebView,true);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-02
      • 2012-08-18
      • 2023-03-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多