C#清除WebBrowser的Cookie缓存

标签: , , ,

最近用C#写一个程序,用一个窗体中的WebBrowser来登陆网站,但是WebBrowser有cookie缓存,第二次登陆的时候WebBrowser仍然是第一次登陆后的状态,所以要清除WebBrowser的cookie缓存。

stackoverflow上找到一段可用的代码:


[DllImport("wininet.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto, SetLastError = true)]
public static extern bool InternetSetOption(int hInternet, int dwOption, IntPtr lpBuffer, int dwBufferLength);

private unsafe void SuppressWininetBehavior()
{
    /* SOURCE: http://msdn.microsoft.com/en-us/library/windows/desktop/aa385328%28v=vs.85%29.aspx
    * INTERNET_OPTION_SUPPRESS_BEHAVIOR (81):
    *      A general purpose option that is used to suppress behaviors on a process-wide basis. 
    *      The lpBuffer parameter of the function must be a pointer to a DWORD containing the specific behavior to suppress. 
    *      This option cannot be queried with InternetQueryOption. 
    *      
    * INTERNET_SUPPRESS_COOKIE_PERSIST (3):
    *      Suppresses the persistence of cookies, even if the server has specified them as persistent.
    *      Version:  Requires Internet Explorer 8.0 or later.
    */


    int option = (int)3/* INTERNET_SUPPRESS_COOKIE_PERSIST*/;
    int* optionPtr = &option;

    bool success = InternetSetOption(0, 81/*INTERNET_OPTION_SUPPRESS_BEHAVIOR*/, new IntPtr(optionPtr), sizeof(int));
    if (!success)
    {
        MessageBox.Show("Something went wrong !>?");
    }
}

随机文章:

  1. windows live writer代码高亮插件
  2. VBS中的GUI界面(EditPlus注册机)
  3. WMI入门教程:第一部分
  4. 那些长的像UTF-8的GBK编码
  5. WMI中的Win32_PingStatus类

2 条评论 发表在“C#清除WebBrowser的Cookie缓存”上

  1. Bob Gu说道:

    这里似乎使用阻塞线程的方式来出清空cookie的方法更好。

留下回复