How to enable cookies in WPF WebBrowser Control

随声附和 提交于 2019-12-13 09:11:01

问题


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.

[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.


回答1:


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.

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.

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



来源:https://stackoverflow.com/questions/48959810/how-to-enable-cookies-in-wpf-webbrowser-control

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