下面是我测试下来的 6 种清除 webbrowser 中 cookie 的 6 种方法:
- // 方法一: 调用 wininet.dll 清除 cookie (推荐)
- SuppressWininetBehavior();
- // 方法二: 删除用户登录后的信息, 这里相当于浏览器的注销功能, 使用的是 IE 自带的功能 (推荐)
- htmlDocument document = wb.Document;
- document.ExecCommand("ClearAuthenticationCache", false, null);
- // 方法三: 删除本机 cookie 此方法会弹出 IE 清除 cookie 的弹出框
- //Temporary Internet Files (Internet 临时文件)
- //RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 8
- //Cookies
- //RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 2
- //History (历史记录)
- //RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 1
- //Form. Data (表单数据)
- //RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 16
- //Passwords (密码)
- //RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 32
- //Delete All (全部删除)
- //ShellExecute(IntPtr.Zero, "open", "rundll32.exe", "InetCpl.cpl,ClearMyTracksByProcess 2", "", ShowCommands.SW_HIDE);
- ShellExecute(IntPtr.Zero, "open", "rundll32.exe", "InetCpl.cpl,ClearMyTracksByProcess 255", "", ShowCommands.SW_HIDE);
- // 方法四: 使用 webbrowser 自带的清 coookie 的方法 (不推荐, 清不掉 session, 实测无效)
- wb.Document.Cookie.Remove(0, (wb.Document.Cookie.Count() - 1));
- // 方法五: 使用 JS 清除 cookie (不推荐, 清不掉 session)
- wb.Navigate("javascript:void((function(){var a,b,c,e,f;f=0;a=document.cookie.split('; ');for(e=0;e<a.length&&a[e];e++){f++;for(b='.'+location.host;b;b=b.replace(/^(?:%5C.|[^%5C.]+)/,'')){for(c=location.pathname;c;c=c.replace(/.$/,'')){document.cookie=(a[e]+'; domain='+b+'; path='+c+'; expires='+new Date((new Date()).getTime()-1e11).toGMTString());}}}})())");
- //var a,b,c,e,f;
- //f=0;
- //a=document.cookie.split(';');
- //b='.'+'baidu.com';
- ////b='.'+'www.baidu.com';
- //for(e=0;e<a.length;e++){
- // //b='.'+location.host;
- // b=b.replace(/^(?:%5C.|[^%5C.]+)/,'');
- // c=location.pathname;
- // c=c.replace(/.$/,'');
- // ck = a[e]+'; domain='+b+'; path='+c+'; expires='+new Date((new Date()).getTime()-1e11).toGMTString();
- // console.log(ck);
- // document.cookie=ck;
- //}
- // 方法六: 使用 InternetSetCookie 给 cookie 赋 null 值 (不推荐)
- // 也可以给此 Cookie 赋空值: InternetSetCookie
- //InternetSetCookie("http://.qq.com/", NULL, "uin=; PATH=/; DOMAIN=qq.com");
方法一:
- [System.Runtime.InteropServices.DllImport("wininet.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto, SetLastError = true)]
- public static extern bool InternetSetOption(int hInternet, int dwOption, IntPtr lpBuffer, int dwBufferLength);
- /// <summary>
- /// 使用 InternetSetOption 操作 wininet.dll 清除 webbrowser 里的 cookie
- /// </summary>
- private static unsafe void SuppressWininetBehavior()
- {
- /* SOURCE: http://msdn.microsoft.com/en-us/library/windows/desktop/aa385328%28v=vs.85%29.aspx
- * INTERNET_OPTION_SUPPRESS_BEHAVIOR (81):
- * A general purpose option that is used to suppress behaviors on a process-wide basis.
- * The lpBuffer parameter of the function must be a pointer to a DWORD containing the specific behavior to suppress.
- * This option cannot be queried with InternetQueryOption.
- *
- * INTERNET_SUPPRESS_COOKIE_PERSIST (3):
- * Suppresses the persistence of cookies, even if the server has specified them as persistent.
- * Version: Requires Internet Explorer 8.0 or later.
- */
- int option = (int)3/* INTERNET_SUPPRESS_COOKIE_PERSIST*/;
- int* optionPtr = &option;
- bool success = InternetSetOption(0, 81/*INTERNET_OPTION_SUPPRESS_BEHAVIOR*/, new IntPtr(optionPtr), sizeof(int));
- if (!success)
- {
- MessageBox.Show("Something went wrong ! Clear Cookie Failed!");
- }
- }
方法二:
就只有这一句就好了:
- // 方法二: 删除用户登录后的信息, 这里相当于浏览器的注销功能, 使用的是 IE 自带的功能 (推荐)
- HtmlDocument document = wb.Document;
- document.ExecCommand("ClearAuthenticationCache", false, null);
方法三:
- // 方法三: 删除本机 cookie 此方法会弹出 IE 清除 cookie 的弹出框
- //Temporary Internet Files (Internet 临时文件)
- //RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 8
- //Cookies
- //RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 2
- //History (历史记录)
- //RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 1
- //Form. Data (表单数据)
- //RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 16
- //Passwords (密码)
- //RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 32
- //Delete All (全部删除)
- //ShellExecute(IntPtr.Zero, "open", "rundll32.exe", "InetCpl.cpl,ClearMyTracksByProcess 2", "", ShowCommands.SW_HIDE);
- ShellExecute(IntPtr.Zero, "open", "rundll32.exe", "InetCpl.cpl,ClearMyTracksByProcess 255", "", ShowCommands.SW_HIDE);
ShellExecute 方法:
- public enum ShowCommands : int
- {
- SW_HIDE = 0,
- SW_SHOWNORMAL = 1,
- SW_NORMAL = 1,
- SW_SHOWMINIMIZED = 2,
- SW_SHOWMAXIMIZED = 3,
- SW_MAXIMIZE = 3,
- SW_SHOWNOACTIVATE = 4,
- SW_SHOW = 5,
- SW_MINIMIZE = 6,
- SW_SHOWMINNOACTIVE = 7,
- SW_SHOWNA = 8,
- SW_RESTORE = 9,
- SW_SHOWDEFAULT = 10,
- SW_FORCEMINIMIZE = 11,
- SW_MAX = 11
- }
- [DllImport("shell32.dll")]
- static extern IntPtr ShellExecute(IntPtr hwnd, string lpOperation, string lpFile, string lpParameters, string lpDirectory, ShowCommands nShowCmd);
方法四:
- // 方法四: 使用 webbrowser 自带的清 coookie 的方法 (不推荐, 清不掉 session, 实测无效)
- wb.Document.Cookie.Remove(0, (wb.Document.Cookie.Count() - 1));
方法五:
- // 方法五: 使用 JS 清除 cookie (不推荐, 清不掉 session)
- wb.Navigate("javascript:void((function(){var a,b,c,e,f;f=0;a=document.cookie.split('; ');for(e=0;e<a.length&&a[e];e++){f++;for(b='.'+location.host;b;b=b.replace(/^(?:%5C.|[^%5C.]+)/,'')){for(c=location.pathname;c;c=c.replace(/.$/,'')){document.cookie=(a[e]+'; domain='+b+'; path='+c+'; expires='+new Date((new Date()).getTime()-1e11).toGMTString());}}}})())");
- //var a,b,c,e,f;
- //f=0;
- //a=document.cookie.split(';');
- //b='.'+'baidu.com';
- ////b='.'+'www.baidu.com';
- //for(e=0;e<a.length;e++){
- // //b='.'+location.host;
- // b=b.replace(/^(?:%5C.|[^%5C.]+)/,'');
- // c=location.pathname;
- // c=c.replace(/.$/,'');
- // ck = a[e]+'; domain='+b+'; path='+c+'; expires='+new Date((new Date()).getTime()-1e11).toGMTString();
- // console.log(ck);
- // document.cookie=ck;
- //}
将 wb.Navigate("JavaScript:void((function(){...} 里的内容换成下面注释掉的代码, 写好你要清 cookier 的 domain 然后就可以清了, 但清不掉 session, 这个是从外国网站上看来的, 实际无效!
方法六:
- // 方法六: 使用 InternetSetCookie 给 cookie 赋 null 值 (不推荐)
- // 也可以给此 Cookie 赋空值: InternetSetCookie
- //InternetSetCookie("http://.qq.com/", NULL, "uin=; PATH=/; DOMAIN=qq.com");
关于 InternetSetCookie 这个方法自己网上搜索一下.
来源: http://www.bubuko.com/infodetail-3037871.html