8

有没有办法读取/写入 WebBrowser 控件使用的 cookie?

我正在做这样的事情......

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

6 回答 6

7

关于控件和 cookie 的一个可能令人困惑的事情WebBrowser是,乍一看,您的应用程序通常会获得一个单独的 cookie 存储例如,如果您登录一个存储持久性 cookie 以识别您身份的站点,那么您是否似乎是从托管该控件的应用程序内部登录该站点将与您是否似乎通过 Internet Explorer 登录无关.

事实上,您甚至可以使用不同的身份登录。

然而,虽然很自然地得出这样的结论,即每个托管应用程序WebBrowser都有自己的 cookie,但事实上这不是真的。WebBrowser只有两组 cookie:一组用于“低完整性”模式(这是 IE 默认运行的) ,另一组是您在托管如果你运行 IE 提升,你会得到。

于 2012-06-28T14:54:51.470 回答
6

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

于 2009-12-29T19:46:31.180 回答
3

是的,你是对的,InternetGetCookieEx 是检索 HttpOnly cookie 的唯一方法,它是从 WebBrowser 控件中获取 cookie 的首选方法。

我在这里发布了一个完整的例子

于 2011-04-12T10:34:45.160 回答
3

您可以使用Application.GetCookieApplication.SetCookie方法。

尽管 Application 或多或少与 WPF 相关,但您可以在任何桌面 .NET 代码中使用这些方法。事实上,它们是InternetGetCookieExInternetSetCookieEx Windows API 的包装器。

于 2014-06-17T15:49:59.527 回答
0

几天前我遇到了同样的问题。除了前面答案的示例之外,这里是 WebBrowser 控件的 Win32 包装器。此实现的优点是它提供了比默认 WebBrowser 控件更多的选项。

不幸的是,如果它不是 WPF 原生的,那么如果您打算在 WPF 中使用它,则必须创建一个包装器。

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

于 2011-06-22T21:17:09.930 回答
0
 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
            );
        }
于 2020-08-06T18:59:05.093 回答