Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Microsoft Web Browser Control Cookies

Microsoft Web Browser Control Cookies

Scheduled Pinned Locked Moved C#
question
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    kayhustle
    wrote on last edited by
    #1

    Is there any way to access and delete cookies obtained by the Microsoft Web Browser Control?

    H 1 Reply Last reply
    0
    • K kayhustle

      Is there any way to access and delete cookies obtained by the Microsoft Web Browser Control?

      H Offline
      H Offline
      Heath Stewart
      wrote on last edited by
      #2

      You need to P/Invoke InternetGetCookie:

      [DllImport("wininet.dll", CharSet=CharSet.Auto, SetLastError=true)]
      static extern bool InternetGetCookie(string url, string name, [Out] string data,
      ref int size);

      Do not declare the third param using out, be sure to use the OutAttribute as I've done above. A String (C# alias string) is already a reference type, so you're already passing the address of a string variable. Double referencing will crash the CLR. To call it, consider a wrapper method since P/Invoke methods should not generally be exposed as public members (depending on if you're writing a library or just using this in an app):

      public string GetCookie(string url, string name)
      {
      int size = 0;
      string data = null;
      // Get the size for 'data' first.
      if (!InternetGetCookie(url, name, null, ref size))
      {
      if (Marshal.GetLastWin32Error() == 122) // ERROR_INSUFFICIENT_BUFFER
      {
      data = new string('\0', size);
      if (InternetGetCookie(url, name, data, ref size))
      return data.Trim();
      }
      }
      return null;
      }

      You'll still need to parse the cookie string, though, but that should be simple. This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Sustained Engineering Microsoft [My Articles]

      K 1 Reply Last reply
      0
      • H Heath Stewart

        You need to P/Invoke InternetGetCookie:

        [DllImport("wininet.dll", CharSet=CharSet.Auto, SetLastError=true)]
        static extern bool InternetGetCookie(string url, string name, [Out] string data,
        ref int size);

        Do not declare the third param using out, be sure to use the OutAttribute as I've done above. A String (C# alias string) is already a reference type, so you're already passing the address of a string variable. Double referencing will crash the CLR. To call it, consider a wrapper method since P/Invoke methods should not generally be exposed as public members (depending on if you're writing a library or just using this in an app):

        public string GetCookie(string url, string name)
        {
        int size = 0;
        string data = null;
        // Get the size for 'data' first.
        if (!InternetGetCookie(url, name, null, ref size))
        {
        if (Marshal.GetLastWin32Error() == 122) // ERROR_INSUFFICIENT_BUFFER
        {
        data = new string('\0', size);
        if (InternetGetCookie(url, name, data, ref size))
        return data.Trim();
        }
        }
        return null;
        }

        You'll still need to parse the cookie string, though, but that should be simple. This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Sustained Engineering Microsoft [My Articles]

        K Offline
        K Offline
        kayhustle
        wrote on last edited by
        #3

        Once I got the cookie, do you know what I would have to do to delete it? Also, let's say I wanted to return all cookies for a specific website. I understand I pass in the homepage of the website as "url", but what do I pass in for "name"? Thank you

        H 1 Reply Last reply
        0
        • K kayhustle

          Once I got the cookie, do you know what I would have to do to delete it? Also, let's say I wanted to return all cookies for a specific website. I understand I pass in the homepage of the website as "url", but what do I pass in for "name"? Thank you

          H Offline
          H Offline
          Heath Stewart
          wrote on last edited by
          #4

          I recommend you read about the WinInet Functions[^] on MSDN Online. If you want to know more about InternetGetCookie, you should read[^] about it on MSDN Online as well, which is under the WinInet Functions that I posted a link to above. This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Sustained Engineering Microsoft [My Articles]

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups