如何从 windows.form 中删除 Cookies? [英] How to delete Cookies from windows.form?

查看:19
本文介绍了如何从 windows.form 中删除 Cookies?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 C# 编写的 windows.form 应用程序上的 Webbrowser 控件.我想编写一个方法,用于在访问某个站点后从 Webbrowers 控件中删除 cookie.不幸的是,我不知道如何确切地做到这一点,也没有在互联网上找到很多帮助.

I am working with the Webbrowser control on a windows.form application written in C#. I would like to write a method for deleting the cookies from the Webbrowers control after it visits a certain site. Unfortunately, I don't know how to do that exactly and haven't found a lot of help on the internet.

如果有人有实际这样做的经验,而不仅仅是假设,因为它可能比看起来更棘手,我不知道.

If anyone has experience actually doing this, not just hypothetical because it might be trickier than it seems, I don't know.

int count = webBrowser2.Document.Cookie.Length;
webBrowser2.Document.Cookie.Remove(0,count);

我只是假设上面的代码会起作用,但我想不会.任何人都可以对整个 cookie 事情有所了解吗?

I would just assume something like the above code would work but I guess it won't. Can anyone shed some light on this whole cookie thing?

推荐答案

如果您启用了 JavaScript,您只需使用此代码片段来清除 Web 浏览器当前所在站点的 cookie.

If you have JavaScript enabled you can just use this code snippet to clear to clear the cookies for the site the webbrowser is currently on.

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());}}}})())")

它源自这个书签,用于清除 cookie.

It's derived from this bookmarklet for clearing cookies.

这篇关于如何从 windows.form 中删除 Cookies?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆