如何在WPF WebBrowser控件中启用cookie

时间:2021-07-09 20:44:29

I need to enable cookies in my WPF application WebBrowser control even if it is disabled in the IE settings. After going through many questions this is what i tried, but this does not work.

我需要在我的WPF应用程序WebBrowser控件中启用cookie,即使它在IE设置中被禁用。经过许多问题后,这就是我尝试过的,但这不起作用。

[DllImport("wininet.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern bool InternetSetOption(IntPtr hInternet, int dwOption, ref int flag, int dwBufferLength);

    static bool EnableCookies(int settingCode, int option)
    {
        if (!InternetSetOption(IntPtr.Zero, settingCode, ref option, sizeof(int)))
        {
            var ex = Marshal.GetExceptionForHR(Marshal.GetHRForLastWin32Error());
            //throw ex;
            return false;
        }
        return true;
    }

    protected override void OnStartup(StartupEventArgs e)
    {
        base.OnStartup(e);
        EnableCookies(81, 1);
    }

If this is not possible, I wish to at least be able to get the setting value to show the user an error message that cookies are not enabled.

如果无法做到这一点,我希望至少能够获得设置值,以向用户显示未启用cookie的错误消息。

1 个解决方案

#1


0  

webbrowser control uses wininet for networking, specifically use the internetSetCookie(Ex) and internetGetCookie(Ex) functions for Cookie management. There isn't a wininet wrapper in .Net, but you can p-invoke.

webbrowser控件使用wininet进行网络连接,特别是使用internetSetCookie(Ex)和internetGetCookie(Ex)函数进行Cookie管理。 .Net中没有wininet包装器,但是你可以p调用。

The WPF webbrowser does not expose all the features of the winforms webbrowser. The way to get around this is to host the winforms webbrowser in a WindowsFormsHost.

WPF webbrowser没有公开winforms webbrowser的所有功能。解决这个问题的方法是在WindowsFormsHost中托管winforms webbrowser。

xmlns:my="clr-namespace:System.Windows.Forms.Integration;assembly=WindowsFormsIntegration"
xmlns:wb="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
Title="WpfWebBrowser" Height="300" Width="300">
<Grid>
    <my:WindowsFormsHost>
        <wb:WebBrowser x:Name="webBrowser"></wb:WebBrowser>
    </my:WindowsFormsHost>
</Grid>

see description here get-cookie-string-from-wpfs-webbrowser

请参阅此处的描述get-cookie-string-from-wpfs-webbrowser

#1


0  

webbrowser control uses wininet for networking, specifically use the internetSetCookie(Ex) and internetGetCookie(Ex) functions for Cookie management. There isn't a wininet wrapper in .Net, but you can p-invoke.

webbrowser控件使用wininet进行网络连接,特别是使用internetSetCookie(Ex)和internetGetCookie(Ex)函数进行Cookie管理。 .Net中没有wininet包装器,但是你可以p调用。

The WPF webbrowser does not expose all the features of the winforms webbrowser. The way to get around this is to host the winforms webbrowser in a WindowsFormsHost.

WPF webbrowser没有公开winforms webbrowser的所有功能。解决这个问题的方法是在WindowsFormsHost中托管winforms webbrowser。

xmlns:my="clr-namespace:System.Windows.Forms.Integration;assembly=WindowsFormsIntegration"
xmlns:wb="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
Title="WpfWebBrowser" Height="300" Width="300">
<Grid>
    <my:WindowsFormsHost>
        <wb:WebBrowser x:Name="webBrowser"></wb:WebBrowser>
    </my:WindowsFormsHost>
</Grid>

see description here get-cookie-string-from-wpfs-webbrowser

请参阅此处的描述get-cookie-string-from-wpfs-webbrowser