How to Force WebBrowser Control to use New Session

2020-07-18 04:14发布

In my application, user will open multiple tabs by clicking on menus. Each tab is dynamically created and containing webbrowser control to load URL.

Each URL point to same server and some of URL does not have access so , gives Resource not have access error received.

Now, problem is, example- If user directly click on Menu3 and related tab loaded with webbrowser URL and follow to next, URL contain other popup link then it works and able to popup the URL.

Now, user click on Menu5 where not have access so, get this error Resource not have access (denied from server). Its fine. NOw, again URL reach toMenu3and try to open sub link to popup dialog then it gives403 forbidden error- decline access`. It works initially but, later it just giving this error.

As it looks, I need to clear the WebBrowser Control cache or forcely start with new session.

Can any one please guide me how to force WebBrowser to start new session or remove earlier caches ?

2条回答
淡お忘
2楼-- · 2020-07-18 05:03

The cache of the WebBrowser control is the same of Internet Explorer. You have various options:

1) Completely clear that cache (will also clear Internet Explorer!):

https://stackoverflow.com/a/24401521/2633161

2) Use some tags in the server response:

<META HTTP-EQUIV="CACHE-CONTROL" CONTENT="NO-CACHE">

3) Use a random query string to force the refresh:

WebBrowser1.Navigate('http://www.example.com/?refresh=' & Guid.NewGuid().ToString())

4) Force refresh of the page (this will load the page 2 times!):

WebBrowser1.Navigate('http://www.example.com/')
WebBrowser1.Refresh(WebBrowserRefreshOption.Completely)
查看更多
▲ chillily
3楼-- · 2020-07-18 05:17

There is a better alternative. It's using the WinINet.DLL and calling SetInternetOptions

[DllImport("wininet.dll", SetLastError = true)]
    private static extern bool InternetSetOption(IntPtr hInternet, int dwOption, IntPtr lpBuffer, int lpdwBufferLength);

    private const int INTERNET_OPTION_END_BROWSER_SESSION = 42;

InternetSetOption(IntPtr.Zero, INTERNET_OPTION_END_BROWSER_SESSION, IntPtr.Zero, 0);

This will end the browser's session cache. After you call this method the webbrowser control will forget whatever sessions had in memory

查看更多
登录 后发表回答