当前位置: 移动技术网 > IT编程>开发语言>c# > C#清除WebBrowser中Cookie缓存的方法

C#清除WebBrowser中Cookie缓存的方法

2019年07月18日  | 移动技术网IT编程  | 我要评论
本文实例讲述了c#清除webbrowser中cookie缓存的方法。分享给大家供大家参考,具体如下: 最近用c#写一个程序,用一个窗体中的webbrowser来登陆网站,

本文实例讲述了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 !>?");
  }
}

更多关于c#相关内容感兴趣的读者可查看本站专题:《c#常见控件用法教程》、《c#数据结构与算法教程》、《c#面向对象程序设计入门教程》及《c#程序设计之线程使用技巧总结

希望本文所述对大家c#程序设计有所帮助。

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网