CasperJ
2008-05-10 11:21:00 UTC
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;
}
}
}
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;
}
}
}
--
Best regards
Casper Jensen
CRM Extensions
www.crmextensions.com
Best regards
Casper Jensen
CRM Extensions
www.crmextensions.com