本文介绍了如何在 WPF WebBrowser 控件中启用 cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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);
    }

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

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.

推荐答案

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

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.

WPF webbrowser 没有公开 winforms webbrowser 的所有功能.解决这个问题的方法是在 WindowsFormsHost 中托管 winforms 网络浏览器.

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>

请参阅此处的说明 get-cookie-string-from-wpfs-webbrowser

这篇关于如何在 WPF WebBrowser 控件中启用 cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 23:54
查看更多