开发者

Managing cookies in a WPF WebBrowser control?

开发者 https://www.devze.com 2022-12-14 21:11 出处:网络
Is there a way to read/write the c开发者_如何学JAVAookies that a WebBrowser control uses? I am doing something like this...

Is there a way to read/write the c开发者_如何学JAVAookies that a WebBrowser control uses?

I am doing something like this...

string resultHtml;
HttpWebRequest request = CreateMyHttpWebRequest(); // fills http headers and stuff
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
using (StreamReader sr = new StreamReader(response.GetResponseStream()))
{
    resultHtml = sr.ReadToEnd();
}
WebBrowser browser = new WebBrowser();
browser.CookieContainer = request.CookieContainer; // i wish i could do this :(
browser.NavigateToString(resultHtml);  


One of the potentially confusing things about the WebBrowser control and cookies is that at a first glance, it often looks like your app gets a separate cookie store. For example, if you log into a site that stores a persistent cookie to identify you, then whether you appear to be logged in for that site from inside an app hosting the control will be independent of whether you seem to be logged in via Internet Explorer.

In fact, you can even be logged in with different identities.

However, although it might be natural to draw the conclusion that each app hosting the WebBrowser therefore gets its own cookies, in fact that's not true. There are merely two sets of cookies: the ones used in 'low integrity' mode (which is what IE runs in by default), and the other set, which is what you'll get in a normal app that hosts the WebBrowser and also what you'll get if you run IE elevated.


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


Yes you are right, InternetGetCookieEx is the only way to retrieve HttpOnly cookies and it is the preferred way to grab cookie from WebBrowser control.

I posted a complete example here


You can use Application.GetCookie and Application.SetCookie methods.

Although Application is more or less related to WPF, you can use these methods in any desktop .NET code. In fact, they are wrappers on InternetGetCookieEx and InternetSetCookieEx Windows APIs.


I faced the same issue few days ago. Besides the examples of the previous answers, here is a Win32 wrapper for the WebBrowser control. The advantage of this implementation is that it exposes more options that the default WebBrowser control.

Unfortunately if It's not WPF native, so you will have to create a wrapper if you're planning to use it in WPF.

http://code.google.com/p/csexwb2/


 Here is sample from [link][1]
 >   public static class WinInetHelper
        {
            public static bool SupressCookiePersist()
            {
                // 3 = INTERNET_SUPPRESS_COOKIE_PERSIST
                // 81 = INTERNET_OPTION_SUPPRESS_BEHAVIOR
                return SetOption(81, 3);
            }
    
            public static bool EndBrowserSession()
            {
                // 42 = INTERNET_OPTION_END_BROWSER_SESSION
                return SetOption(42, null);
            }
            static bool SetOption(int settingCode, int? option)
            {
                IntPtr optionPtr = IntPtr.Zero;
                int size = 0;
                if (option.HasValue)
                {
                    size = sizeof(int);
                    optionPtr = Marshal.AllocCoTaskMem(size);
                    Marshal.WriteInt32(optionPtr, option.Value);
                }
    
                bool success = InternetSetOption(0, settingCode, optionPtr, size);
    
                if (optionPtr != IntPtr.Zero) Marshal.Release(optionPtr);
                return success;
            }
    
            [System.Runtime.InteropServices.DllImport("wininet.dll", CharSet = CharSet.Auto, SetLastError = true)]
            private static extern bool InternetSetOption(
            int hInternet,
            int dwOption,
            IntPtr lpBuffer,
            int dwBufferLength
            );
        }
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号