清除webBrowser 缓存和Cookie的解决方案
2016-03-13 20:07
281 查看
通过测试webBrowser与IE缓存和Cookie都存放在Local Settings\Temporary Internet Files,我们可以直接调用IE API进行清除
解决方案1:
其中ClearMyTracksByProcess 可进行选择设置 :
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 (全部删除)
RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 255
缺点:在Webbrowser 在不关闭的情况下无法清除cookie
解决方案2:
缺点:需要程序取得管理员权限
解决方案3:
利用 webBrowser 执行清楚cookie的js代码
缺点:在某些运行环境中会报“无法下载”的错,至于原因目前还没找到
解决方案1:
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); //清除IE临时文件 ShellExecute(IntPtr.Zero, "open", "rundll32.exe", "InetCpl.cpl,ClearMyTracksByProcess 255", "", ShowCommands.SW_HIDE)
其中ClearMyTracksByProcess 可进行选择设置 :
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 (全部删除)
RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 255
缺点:在Webbrowser 在不关闭的情况下无法清除cookie
解决方案2:
//清除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 (c_web.Document != null ) { c_web.Document.Cookie.Remove (0 , c_web.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) { } } }
缺点:需要程序取得管理员权限
解决方案3:
利用 webBrowser 执行清楚cookie的js代码
this._webBrowser.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());}}}})())");
缺点:在某些运行环境中会报“无法下载”的错,至于原因目前还没找到
相关文章推荐
- +号的使用
- Struts2国际化介绍及详细说明
- 线段树(维护最大值):HDU Billboard
- 使用JDBC进行数据库的事务操作(1)
- CodeForces 596A--Wilbur and Swimming Pool
- 阿里面试问题
- jenkins+git+android+gradle持续集成问题总结
- nodejs使用mysql例子
- 堆排序的实现(c++)
- Network Security_异或解密
- python(5)-os模块
- 上传--下载HDFS文件并指定文件物理块的大小
- html中js实现左边框控制右边框的显示
- JSP
- 相关资料
- 异或运算及其应用
- 正则表达式及grep
- 相同的和
- 手机的九宫格图形解锁算法(判断是否合法)
- kali2.0如何安装中文输入法