Microsoft Web Browser Control Cookies
-
Is there any way to access and delete cookies obtained by the Microsoft Web Browser Control?
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 theOutAttribute
as I've done above. AString
(C# aliasstring
) 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]
-
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 theOutAttribute
as I've done above. AString
(C# aliasstring
) 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]
-
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
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]