【问题标题】:How to enable cookies in WPF WebBrowser Control如何在 WPF WebBrowser 控件中启用 cookie
【发布时间】:2018-02-24 05:46:30
【问题描述】:

我需要在我的 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);
    }

如果这不可能,我希望至少能够获得设置值,以向用户显示未启用 cookie 的错误消息。

【问题讨论】:

    标签: wpf webbrowser-control


    【解决方案1】:

    webbrowser控件使用wininet进行联网,具体使用internetSetCookie(Ex)和internetGetCookie(Ex)函数进行Cookie管理。 .Net 中没有 wininet 包装器,但您可以 p-invoke。

    WPF 网络浏览器并未公开 winforms 网络浏览器的所有功能。解决此问题的方法是将 winforms 网络浏览器托管在 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>
    

    在这里查看描述get-cookie-string-from-wpfs-webbrowser

    【讨论】:

    • 正如您在发布的代码中看到的那样,我正在使用 wininet 函数来启用 cookie 设置,但它不起作用。
    猜你喜欢
    • 2010-12-30
    • 2011-05-15
    • 2013-11-26
    • 2011-02-19
    • 2010-11-20
    • 2011-10-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多