【发布时间】: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 有什么提示或建议吗?
谢谢。
【问题讨论】: