.NET 中提供的WebBrowser 控件中的COOKIE无法直接设置,无奈之下只好使用API去设置COOKIE的值了。希望MS可以尽快提供在WebBrowser控件中任意设置Cookie的值。可惜的是在VS2010中也不能直接设置这个值。
代码
using System;
using System.Runtime.InteropServices;
public class CookieHelper
{
private string url;
[DllImport("wininet.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern bool InternetSetCookie(string lpszUrlName, string lbszCookieName, string lpszCookieData);
public static void SetCookie(string path,string cookieName,string cookieValue)
{
this.url = path;
// 设置Cookie的值
InternetSetCookie(url, cookieName, cookieValue);
}
}
using System.Runtime.InteropServices;
public class CookieHelper
{
private string url;
[DllImport("wininet.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern bool InternetSetCookie(string lpszUrlName, string lbszCookieName, string lpszCookieData);
public static void SetCookie(string path,string cookieName,string cookieValue)
{
this.url = path;
// 设置Cookie的值
InternetSetCookie(url, cookieName, cookieValue);
}
}
代码
// 清除所有COOKIE
using System;
using System.IO;
void clearIECache()
{
ClearFolder (new DirectoryInfo (Environment.GetFolderPath(Environment.SpecialFolder.InternetCache)));
}
void ClearFolder (DirectoryInfo folder)
{
foreach (FileInfo file in folder.GetFiles())
{
file.Delete();
}
foreach (DirectoryInfo subfolder in folder.GetDirectories())
{
ClearFolder(subfolder);
}
}
using System;
using System.IO;
void clearIECache()
{
ClearFolder (new DirectoryInfo (Environment.GetFolderPath(Environment.SpecialFolder.InternetCache)));
}
void ClearFolder (DirectoryInfo folder)
{
foreach (FileInfo file in folder.GetFiles())
{
file.Delete();
}
foreach (DirectoryInfo subfolder in folder.GetDirectories())
{
ClearFolder(subfolder);
}
}