vb.net 如何在 .NET WebBrowser 控件中禁用缓存?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3421144/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 14:59:18  来源:igfitidea点击:

How to disable caching in the .NET WebBrowser Control?

vb.netcachingbrowserwebbrowser-controldisable-caching

提问by fMinkel

I have been googling for hours and trying to figure this out, and I just can't. I have 1 webbrowser control on a form, webbrowser1.

我已经搜索了几个小时并试图解决这个问题,但我做不到。我在表单 webbrowser1 上有 1 个 webbrowser 控件。

Once I load a page, say google.com, if I use webbrowser1.refresh()or webbrowser1.navigate("google.com"), it's not reloading the page, it has it cached so it's just reloading the cache. This is terribly apparent especially on pages like forums or craigslist.

一旦我加载了一个页面,比如 google.com,如果我使用webbrowser1.refresh()webbrowser1.navigate("google.com"),它不会重新加载页面,它已经缓存了它,所以它只是重新加载缓存。这在论坛或 craigslist 等页面上尤为明显。

I need to clear the cache between each refresh (not ideal) or disable caching all together, any ideas? The only things I've found are outdated (vb6 or lower).

我需要在每次刷新之间清除缓存(不理想)或一起禁用缓存,有什么想法吗?我发现的唯一东西是过时的(vb6 或更低版本)。

回答by Adrian Faciu

You could try to call webbrowser1.Refresh(WebBrowserRefreshOption.Completely). It should refresh the page and show the latest version, something like ctrl+F5 in IE. See hereand heremore info.

你可以试试打电话webbrowser1.Refresh(WebBrowserRefreshOption.Completely)。它应该刷新页面并显示最新版本,类似于 IE 中的 ctrl+F5。请参阅此处此处了解更多信息。

回答by Ravi Patel

Add following meta tag in your pages

在您的页面中添加以下元标记

<meta http-equiv="cache-control" content="no-cache">

回答by Marco

use navigate(url,4) 0x4=noCacheflag

使用navigate(url,4) 0x4=noCache标志

回答by Erx_VB.NExT.Coder

In the .navigate method, you pass the number 2 (the no history flag) but this will only kill history for that navigation, won't kill history for when you follow links. If you want to kill history for links that are clicked on, then you can intercept navigation during beforenavigate event, cancel the navigation by setting cancel = true and then using the URL provided by the event, re doing the navigation using .navigate with the flag set to 2 again (the no history flag).

在 .navigate 方法中,您传递数字 2(无历史标志),但这只会杀死该导航的历史记录,不会杀死您点击链接时的历史记录。如果你想杀死被点击的链接的历史记录,那么你可以在 beforenavigate 事件期间拦截导航,通过设置 cancel = true 取消导航,然后使用事件提供的 URL,使用带有标志的 .navigate 重新进行导航再次设置为 2(无历史标志)。

As far as other cache items like cookies, the flags don't work (though they may have implemented this in current versions)... Soto kill all cache items you actually need to programmatically do this outside of the web browser control by querying user cache using other Apis and deleting it, perhaps during document complete event or when browsing is done.

至于其他缓存项目,如 cookie,这些标志不起作用(尽管它们可能已经在当前版本中实现了这一点)...... Soto 杀死所有您实际需要的缓存项目,通过查询用户在 Web 浏览器控制之外以编程方式执行此操作使用其他 API 缓存并删除它,可能是在文档完成事件期间或浏览完成时。

Also be aware that if you kill history using web browser control, the web browser controls .goback method will not work (as it uses the same history unfortunately, and doesn't keep another history list in memory)... So when doing a goback, it will act like there was nothing to go back to :/.

另请注意,如果您使用 Web 浏览器控件杀死历史记录,则 Web 浏览器控件 .goback 方法将不起作用(不幸的是,它使用相同的历史记录,并且不会在内存中保留另一个历史记录列表)......所以在执行goback,它会表现得好像没有什么可以回到:/。

Let me know if you need more help.

如果您需要更多帮助,请告诉我。

回答by Ishthiyaque

This page showshow to clear some temp file, read it. I also have this issue, but when .refresh() is not useful to me as it doesnt trigger documentcomplete event. So when I wish to reload, I simply use .navigate()and before navigation I call the

此页面显示如何清除一些临时文件,阅读它。我也有这个问题,但是当 .refresh() 对我没有用时,因为它不会触发 documentcomplete 事件。因此,当我想重新加载时,我只需使用.navigate()并在导航之前调用

System.Diagnostics.Process.Start("rundll32.exe","InetCpl.cpl,ClearMyTracksByProcess 8")

in order to clear cache.

为了清除缓存。

回答by Reinaldo

You could try adding a random number or guid in the url as a parameter. Such as:

您可以尝试在 url 中添加一个随机数或 guid 作为参数。如:

var url = "http://google.com";
webBrowser.Navigate(url + "?refreshToken=" + Guid.NewGuid().ToString());

It's not elegant, but it works. Hope it helps.

这并不优雅,但它有效。希望能帮助到你。

回答by Robert Christ

You can't disable it. You can either refresh the page, clear the cache before each request, or use something other than the web browser control. If you want to clear the cache before each request, there is a LOT of bad information online. I would recommend you to my answer here: https://stackoverflow.com/a/22074463/1607218. Beware, the code posted in the other answers on that page is NOT reliable, and is massively buggy, but hopefully my answer will lead you in the right direction : )

你不能禁用它。您可以刷新页面,在每次请求之前清除缓存,或者使用 Web 浏览器控件以外的其他内容。如果你想在每次请求之前清除缓存,网上有很多不好的信息。我会在这里向您推荐我的答案:https: //stackoverflow.com/a/22074463/1607218。请注意,该页面上其他答案中发布的代码不可靠,并且存在大量错误,但希望我的回答会引导您走向正确的方向:)