none
.Net Interop with InterGetCookie() in wininet.dll RRS feed

  • Question

  • Hi

     

    I'm trying to read a cookie from Internet Explorer from a .Net Application. I've found some code (http://www.rendelmann.info/blog/PermaLink.aspx?guid=bd99bcd5-7088-4d46-801e-c0fe622dc2e5) for looking up cookies in IE.

     

    The problem is that I can't get it to work. It will only let me create cookies not read them.

     

    I know that I just could read the actual files from the disk. The problem is that cookies I want to read are "in-memory" and not actually stored on the file system.

     

    Is it at all possible with this API or should I look at something else?

     

    Thanks!

     

    //Casper

     

    Code Snippet

    using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Text;

    using System.Runtime.InteropServices;

    using System.Net;

    using System.Diagnostics;

    namespace CaptureCookie

    {

    class Program

    {

    static void Main(string[] args)

    {

    InternetSetCookie("http://blah.test.com", "test", "aaa;expires = Sat, 01-Jan-2010 00:00:00 ");

    CookieContainer c = GetCookieContainerForUrl(new Uri("http://blah.test.com"), "test");

    }

    [DllImport("wininet.dll", CharSet = CharSet.Auto, SetLastError = true)]

    public static extern bool InternetSetCookie(string lpszUrlName, string lpszCookieName, string lpszCookieData);

    [DllImport("wininet.dll", CharSet = CharSet.Auto, SetLastError = true)]

    static extern bool InternetGetCookie(string lpszUrlName, string lpszCookieName, [Out] string lpszCookieData, [MarshalAs(UnmanagedType.U4)] ref int lpdwSize);

    private static string RetrieveIECookiesForUrl(string url, string s)

    {

    string cookieHeader = "";

    int datasize = cookieHeader.Length;

    if (!InternetGetCookie(url, s, cookieHeader, ref datasize))

    {

    if (datasize < 0)

    return String.Empty;

    InternetGetCookie(url, s, cookieHeader, ref datasize);

    }

    return cookieHeader.ToString();

    }

    public static CookieContainer GetCookieContainerForUrl(Uri url, string s)

    {

    CookieContainer container = new CookieContainer();

    string cookieHeaders = RetrieveIECookiesForUrl(url.AbsoluteUri, s);

    if (cookieHeaders.Length > 0)

    {

    try { container.SetCookies(url, cookieHeaders); }

    catch (CookieException) { }

    }

    return container;

    }

    }

    }

     

     

     

    Friday, May 9, 2008 1:23 PM

Answers

  • Try the System.Windows.Forms.HtmlDocument.Cookie property.  To get an HtmlDocument instance, use the WebBrowser control's Document property in its DocumentCompleted event.  For an example, start a new console app project and add a reference to System.Windows.Forms:

    using System;
    using System.Windows.Forms;

    namespace ConsoleApplication1 {
      class Program {
        static WebBrowser ctl;
        static bool done;
        [STAThread]   // Important!
        static void Main(string[] args) {
          ctl = new WebBrowser();
          ctl.DocumentCompleted += ReadCookie;
          ctl.Navigate(@"www.google.com");
          while (!done)
            Application.DoEvents();
          Console.ReadLine();
          ctl.Dispose();
        }
        static void ReadCookie(object sender, WebBrowserDocumentCompletedEventArgs e) {
          Console.WriteLine(ctl.Document.Cookie);
          done = true;
        }
      }
    }
    Saturday, May 10, 2008 1:51 PM
    Moderator

All replies

  • Try the System.Windows.Forms.HtmlDocument.Cookie property.  To get an HtmlDocument instance, use the WebBrowser control's Document property in its DocumentCompleted event.  For an example, start a new console app project and add a reference to System.Windows.Forms:

    using System;
    using System.Windows.Forms;

    namespace ConsoleApplication1 {
      class Program {
        static WebBrowser ctl;
        static bool done;
        [STAThread]   // Important!
        static void Main(string[] args) {
          ctl = new WebBrowser();
          ctl.DocumentCompleted += ReadCookie;
          ctl.Navigate(@"www.google.com");
          while (!done)
            Application.DoEvents();
          Console.ReadLine();
          ctl.Dispose();
        }
        static void ReadCookie(object sender, WebBrowserDocumentCompletedEventArgs e) {
          Console.WriteLine(ctl.Document.Cookie);
          done = true;
        }
      }
    }
    Saturday, May 10, 2008 1:51 PM
    Moderator
  • Hi,

     

    Thank you for the reply. I'd really liked your approch but sadly it didn't worked. ctl.Document.Cookie is null. It seems like IE is keeping the sessions in-memory. If I create a new iexplore.exe instance and navigates to the exact same site then the cookie is missing again even though the cookie exists on the other iexplore.exe instance...

     

    //Casper

    Sunday, May 11, 2008 8:07 PM