Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
376 views
in Technique[技术] by (71.8m points)

How to enable cookies in WPF WebBrowser Control

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...