简体   繁体   English

Android WebView不缓存HTTPS内容

[英]Android WebView not caching HTTPS content

I have a single-page webapp which is displayed in an Android WebView (Android Jelly Bean). 我有一个单页webapp,它显示在Android WebView(Android Jelly Bean)中。 It's a decent size so I want to utilise caching as much as possible. 这是一个不错的大小,所以我想尽可能多地利用缓存。

If the app is served over HTTP, the cache fills to about 8 meg and on an app re-start, the web content is loaded almost instantly from the cache (no network traffic is observed). 如果应用程序通过HTTP提供,则缓存填充到大约8兆,并且在应用程序重新启动时,Web内容几乎立即从缓存加载(未观察到网络流量)。

However, if the app is served over HTTPS, only a fraction of the cache is used (a few hundred K) and, on an app-restart, all the the web assets (JS / CSS etc) are requested from the server again. 但是,如果应用程序是通过HTTPS提供的,则只使用一小部分缓存(几百K),并且在应用程序重新启动时,将再次从服务器请求所有Web资产(JS / CSS等)。 It's difficult to see exactly what files are being cached as I do not have a rooted device and the files look to have been renamed (eg webviewCacheChromium/data_0 etc). 由于我没有root设备且文件看起来已被重命名(例如webviewCacheChromium / data_0等),因此很难确切地看到正在缓存的文件。

I tried setting the 'Cache-Control: max-age' response header (as I believe this can force https caching in older browsers) but it made no difference. 我尝试设置'Cache-Control:max-age'响应标头(因为我相信这可以强制在旧浏览器中使用https缓存),但它没有任何区别。

I can't find anything to suggest that a WebView will not cache https content by default. 我找不到任何暗示WebView默认不会缓存https内容的内容。 I believe most modern browsers behave this way - I would expect the Android WebView to do the same. 我相信大多数现代浏览器都是这样的 - 我希望Android WebView能够做到这一点。

Am I missing something here? 我在这里错过了什么吗? Does a WebView need to be told to cache https content? 是否需要告知WebView缓存https内容?

Thanks in advance, 提前致谢,

Neil. 尼尔。

Sorted the problem. 排序问题。

Just in case anyone stumbles across the same problem in future.... 万一以后任何人都会遇到同样的问题....

It turns that the app was not being cached correctly due to an SSL error (which unfortunately was being swallowed elsewhere in the app). 由于SSL错误(不幸的是,它被应用程序中的其他地方吞没)导致应用程序未正确缓存。 Basically, if you don't have your SSL certificates properly set-up, this is what you will see. 基本上,如果您没有正确设置SSL证书,您将看到这一点。

To check this extend WebViewClient.onReceivedSslError() and log any errors. 要检查此扩展WebViewClient.onReceivedSslError()并记录任何错误。

We had a couple of different errors as our SSL certs weren't properly set-up - once these were fixed, the app was cached as expected. 我们有两个不同的错误,因为我们的SSL证书没有正确设置 - 一旦修复,应用程序就会按预期缓存。

Hopefully this will save someone the wasted hours that I went through :) 希望这可以节省一些人经历的浪费时间:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM