


在网上找了很多相关代码,都不好用,经过不懈努力,终于找到如下代码,经测试完美清理cookie和session并且是静默清理,不会有任何提示,
代码如下:
调用:
//清除Session所需要调用的函数
[DllImport("wininet.dll", SetLastError = true)]
private static extern bool InternetSetOption(IntPtr hInternet, int dwOption, IntPtr lpBuffer, int lpdwBufferLength);
//清空session
public void ResetSession()
{
//Session的选项ID为42
InternetSetOption(IntPtr.Zero, 42, IntPtr.Zero, 0);
}
//清空cookie
public void ResetCookie()
{
if (Browser.Document != null)
{
Browser.Document.Cookie.Remove(0, Browser.Document.Cookie.Count() - 1);
}
string[] theCookies = System.IO.Directory.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.Cookies));
foreach (string currentFile in theCookies)
{
try
{
System.IO.File.Delete(currentFile);
}
catch (Exception ex)
{
}
}
}
如上代码几乎不用修改的完美解决了我的困惑,本文章转载于https://blog.csdn.net/mydudu2005/article/details/84545404 感谢提供。
private void No_Spider_btn_Click(object sender, EventArgs e)
{
ResetSession();//清理session
ResetCookie(); //清理cookie
Browser.Navigate("http://www.zfcdn.xyz/?Spiderv40");
}



文章评论 本文章有个评论