Discussion:
.Net Interop with InterGetCookie() in wininet.dll
(too old to reply)
CasperJ
2008-05-10 11:21:00 UTC
Permalink
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;
}
}
}
--
Best regards

Casper Jensen
CRM Extensions
www.crmextensions.com
Volodymyr M. Shcherbyna
2008-05-13 08:47:53 UTC
Permalink
Well, I do not have C# here installed, so I just converted your code into
C++ analog:

int _tmain(int argc, _TCHAR* argv[])
{
BOOL bRetVal = FALSE;
TCHAR szData[250] = {0};
DWORD dwSize = sizeof(szData);

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

if (bRetVal)
{
bRetVal = InternetGetCookie(TEXT("http://blah.test.com"), TEXT("test"),
szData, &dwSize);
}

return 0;
}

And it works. Could you please shed light what error code do you get?
--
V.
This posting is provided "AS IS" with no warranties, and confers no
rights.
Post by CasperJ
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;
}
}
}
--
Best regards
Casper Jensen
CRM Extensions
www.crmextensions.com
Loading...