Managing cookies in a WPF WebBrowser control?

后端 未结 6 689
终归单人心
终归单人心 2020-12-18 03:02

Is there a way to read/write the cookies that a WebBrowser control uses?

I am doing something like this...

string resultHtml;
HttpWebRequest requ         


        
6条回答
  •  离开以前
    2020-12-18 03:38

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

提交回复
热议问题