开发者

How to disable caching in the .NET WebBrowser Control?

开发者 https://www.devze.com 2023-01-10 16:57 出处:网络
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.

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 on开发者_Python百科ly things I've found are outdated (vb6 or lower).


Add following meta tag in your pages

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


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.


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


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.


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.


C# WebBrowser control: Clearing cache without clearing cookies


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消