Make WebBrowser control to delete its cache

喜你入骨 提交于 2019-12-10 18:36:10

问题


so i am making a test application which login to a website and if given credentials are valid then separates them. when my webbrowser get successful login and program starts to repeat to process in order to check remaining credentials the logged in account gets cached. i tried

Me.WebBrowser1.Refresh(WebBrowserRefreshOption.Completely)

and couple of other methods like nevigating to about:blank but no success even when i restart the program webbrowser1 opens to cached logged in account. i want to make web browser at rully reset(startup) state after getting good login.


回答1:


You can disable cache (including cookies) before navigating to the site.

To do so, you can use InternetSetOption API function and set the value of INTERNET_OPTION_SUPPRESS_BEHAVIOR(81) option to INTERNET_SUPPRESS_COOKIE_PERSIST(3) value.

Example

I tried the following example, which disables cache and while I've logged in outlook.com, but it works like starting a new session:

Imports System.Runtime.InteropServices
Public Class Form1
    <DllImport("wininet.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
    Public Shared Function InternetSetOption(hInternet As IntPtr, dwOption As Integer, _
        lpBuffer As IntPtr, dwBufferLength As Integer) As Boolean
    End Function

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim ptr = Marshal.AllocHGlobal(4)
        Marshal.WriteInt32(ptr, 3)
        InternetSetOption(IntPtr.Zero, 81, ptr, 4)
        Marshal.Release(ptr)
        WebBrowser1.Navigate("https://outlook.com")
    End Sub
End Class

To find more information about these flags, take a look at Windows Internet Option Flags.

Note: You can find a C# version of this answer, here in my other post.



来源:https://stackoverflow.com/questions/51113676/make-webbrowser-control-to-delete-its-cache

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