1

我正在尝试清除 WPF WebBrowser 中的会话 cookie。这种方法

Public Const INTERNET_OPTION_END_BROWSER_SESSION As Integer = 42
<DllImport("wininet.dll", SetLastError:=True)>
Public Function InternetSetOption(ByVal hInternet As IntPtr, ByVal dwOption As Integer, ByVal lpBuffer As IntPtr, ByVal lpdwBufferLength As Integer) As Boolean
End Function

此处描述如何清除 System.Windows.Forms.WebBrowser 会话数据?适用于我们的 WinForms 应用程序,但不适用于 WPF。

有任何想法吗?

谢谢

4

1 回答 1

2

原来答案是确保每次加载页面时都创建 WebBrowser 对象。

由于 WebBrowser 对象不被视为控件,因此我在初始化时创建了一个 ScrollViewer,然后在加载的事件上将 WebBrowser 添加到 ScrollViewer。

它现在完美运行

对于 Faber75

Imports System.ComponentModel
Class Website_Login_Page
Private LoginBrowser As WebBrowser


Private Sub Website_Login_Page_Initialized(sender As Object, e As System.EventArgs) Handles Me.Initialized
    Try
        Website_Login_Grid.Background = New SolidColorBrush(Colors.White)

        For i As Integer = 0 To 1
            Dim vRow As New RowDefinition
            If i = 0 Then
                vRow.Height = New GridLength(35, GridUnitType.Star)
            Else
                vRow.Height = New GridLength(35)
            End If
            Website_Login_Grid.RowDefinitions.Add(vRow)
        Next

        Dim SV As New ScrollViewer
        With SV
            .VerticalScrollBarVisibility = ScrollBarVisibility.Auto
            .Name = "Website_Login_SV"
        End With
        RegisterControl(Website_Login_Grid, SV)
        'Cookies are not removed if the WebBrowser object is not created each time the page is loaded!
        ' LoginBrowser = New WebBrowser
        Grid.SetRow(SV, 0)
        Website_Login_Grid.Children.Add(SV)

        Dim DP As DockPanel = PageStatusBarDP(Website_Login_Grid)
        Grid.SetRow(DP, 1)
        Website_Login_Grid.Children.Add(DP)

    Catch ex As Exception
        EmailError(ex)
    End Try
End Sub





Private Sub Website_Login_Page_Loaded(sender As Object, e As System.Windows.RoutedEventArgs) Handles Me.Loaded
    Try

        If LoginBrowser Is Nothing Then
            LoginBrowser = New WebBrowser
        End If
        Dim SV As ScrollViewer = Website_Login_Grid.FindName("Website_Login_SV")
        SV.Content = LoginBrowser

        AddHandler LoginBrowser.Navigating, AddressOf WebBrowser_Loading
        AddHandler LoginBrowser.LoadCompleted, AddressOf WebBrowser_Loaded
        AddHandler LoginBrowser.Navigated, AddressOf WebBrowser_Navigated
        If InternetSetOption(IntPtr.Zero, INTERNET_OPTION_END_BROWSER_SESSION, IntPtr.Zero, 0) = False Then
            MessageBox.Show("Returned False")
        End If
        With LoginBrowser
            .Navigate(New Uri("https://website.com"))
                        End With

    Catch ex As Exception
        EmailError(ex)
    End Try

End Sub


Private Sub Website_Login_Page_Unloaded(sender As Object, e As System.Windows.RoutedEventArgs) Handles Me.Unloaded
    Try
        If Not LoginBrowser Is Nothing Then

            RemoveHandler LoginBrowser.Navigating, AddressOf WebBrowser_Loading
            RemoveHandler LoginBrowser.LoadCompleted, AddressOf WebBrowser_Loaded
            RemoveHandler LoginBrowser.Navigated, AddressOf WebBrowser_Navigated
            LoginBrowser = Nothing




        End If
        GC.GetTotalMemory(True)

    Catch ex As Exception
        EmailError(ex)
    End Try
End Sub

Private Sub WebBrowser_Loading(ByVal sender As Object, ByVal e As NavigatingCancelEventArgs)
    PageStatusBarLoading(Website_Login_Grid)
    PageStatusBarRightChangeText(Website_Login_Grid, "Loading... Please wait...")

End Sub

Private Sub WebBrowser_Loaded(ByVal sender As Object, ByVal e As NavigationEventArgs)
    Dim vDoc As Object = LoginBrowser.Document
    Dim vTitle As String = vDoc.Title
    PageStatusBarLoaded(Website_Login_Grid, vTitle)
    PageStatusBarRightChangeText(Website_Login_Grid, "Loaded")
End Sub

Private Sub WebBrowser_Navigated(ByVal sender As Object, ByVal e As NavigationEventArgs)
    Dim vDoc As Object = LoginBrowser.Document
    Dim vTitle As String = vDoc.Title
    PageStatusBarLoaded(Website_Login_Grid, vTitle)
    PageStatusBarRightChangeText(Website_Login_Grid, "Loaded")
End Sub

结束类

于 2013-11-14T15:15:58.320 回答