Method for C to clear Cookie cache in WebBrowser

  • 2021-10-11 19:23:13
  • OfStack

This article illustrates how C # clears the Cookie cache in WebBrowser. Share it for your reference, as follows:

Recently with C # write a program, with a form of WebBrowser to log in to the site, but WebBrowser has cookie cache, the second login time WebBrowser is still the first login after the state, so to clear WebBrowser cookie cache.

Find 1 piece of code available on 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 !>?");
  }
}

For more readers interested in C # related content, please check out the topics on this site: "C # Common Control Usage Tutorial", "C # Data Structure and Algorithm Tutorial", "C # Object-Oriented Programming Introduction Tutorial" and "C # Programming Thread Use Skills Summary"

I hope this article is helpful to everyone's C # programming.


Related articles: