How to disable caching in the .NET WebBrowser Control?

て烟熏妆下的殇ゞ 提交于 2019-11-30 03:09:43

问题


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.

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.

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).


回答1:


Add following meta tag in your pages

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



回答2:


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 here and here more info.




回答3:


use navigate(url,4) 0x4=noCache flag




回答4:


This page shows how 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

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

in order to clear cache.




回答5:


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).

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.

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 :/.

Let me know if you need more help.




回答6:


C# WebBrowser control: Clearing cache without clearing cookies




回答7:


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

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

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




回答8:


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 : )



来源:https://stackoverflow.com/questions/3421144/how-to-disable-caching-in-the-net-webbrowser-control

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!