通用Windows 10 WebView-如何清除/禁用缓存? [英] Universal Windows 10 WebView - how to clear/disable cache?

查看:307
本文介绍了通用Windows 10 WebView-如何清除/禁用缓存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在清除UWP应用中的WebView缓存时遇到很多麻烦。

I am having a lot of trouble clearing the WebView cache in my UWP app.

如果我编辑从HTML页面链接的JS文件的内容,除非我重新安装应用程序,否则无法将更改添加到我的应用程序中。

If I edit the content of a JS file linked from my HTML page, I can't get the change into my app unless I re-install the app.

静态 WebView.ClearTemporaryWebDataAsync()方法似乎无效。

我还尝试向请求中添加标头以禁用缓存:

I have also tried adding headers to the request to disable caching:

private void reloadPage()
{
    string url = getUrl();
    HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, new Uri(url));
    request.Headers.Add("Cache-Control", "no-cache, no-store, must-revalidate");
    request.Headers.Add("Pragma", "no-cache");
    myWebView.NavigateWithHttpRequestMessage(request);
}

我也在平底锅上尝试了以下操作(我不确定是否这会影响WebView的缓存行为),但仍然没有乐趣:

I also tried the following, on a punt (I'm not sure if this affects the WebView's caching behaviour), but still no joy:

private void onWebviewLoaded(object sender, RoutedEventArgs e)
{
    Windows.Web.Http.Filters.HttpBaseProtocolFilter myFilter = new Windows.Web.Http.Filters.HttpBaseProtocolFilter();
    myFilter.CacheControl.WriteBehavior = Windows.Web.Http.Filters.HttpCacheWriteBehavior.NoCache;
    myFilter.CacheControl.ReadBehavior = Windows.Web.Http.Filters.HttpCacheReadBehavior.Default;

    WebView.ClearTemporaryWebDataAsync().AsTask().Wait();
    reloadPage();
}

任何帮助将不胜感激!

编辑(15/12/15):
我发现在请求中添加标头(如上面的第一个代码示例)确实有效,但前提是自安装以来在应用程序的整个生命周期中都已执行此操作。这是有道理的,因为它只是说不缓存此特定请求-它仍可以使用旧的缓存版本。

EDIT (14/12/15): I have found that adding headers to the request (as in the first code example above) does work, but only if this has been in place for the lifetime of the app, since install. Which makes sense, as it's just saying not to cache this particular request - it could still use an old cached version.

这在目前看来很麻烦,但是它将能够更好地利用缓存(例如在应用会话期间),然后再清除缓存(例如在下次启动时)。

This works as a cludge for now, but it would be much nicer to be able to make use of caching (e.g. for the duration of an app session), then later clear the cache (e.g. on next startup).

编辑(16/07/16):上面的方法似乎不可行。在Webview中,缓存行为似乎很不稳定...

EDIT (14/07/16): The above approach doesn't seem to bear out. Caching behaviour seems to be erratic in the webview...

在干净安装该应用程序后,我可以看到对CSS / JS文件的更改,其中绝对没有要清除的代码/禁用缓存。然后在某些看似随意的点上,文件似乎已被缓存,我无法从缓存中清除它们。

On a clean install of the app, I can see changes to CSS/JS files with absolutely NO code to clear/disable cache. Then at some seemingly arbitrary point, files seem to be cached, and I cannot clear them from the cache.

推荐答案

对我来说:

await WebView.ClearTemporaryWebDataAsync();
        Windows.Web.Http.Filters.HttpBaseProtocolFilter myFilter = new Windows.Web.Http.Filters.HttpBaseProtocolFilter();
        var cookieManager = myFilter.CookieManager;

        HttpCookieCollection myCookieJar = cookieManager.GetCookies(new Uri("http://www.msftncsi.com/ncsi.txt"));
        foreach (HttpCookie cookie in myCookieJar)
        {
            cookieManager.DeleteCookie(cookie);
        }

这篇关于通用Windows 10 WebView-如何清除/禁用缓存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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