如何删除WinForm WebBrowser Cookie

.Net技术 码拜 10年前 (2015-03-06) 2966次浏览 0个评论

C# WinForm 删除 WebBrowser Cookie,网上很多都是类似

int count = webBrowser2.Document.Cookie.Length;
webBrowser2.Document.Cookie.Remove(0,count);
测试后并未清除成功。如下是stackoverflow中的问答,测试有效:

How to delete WebBrowser Cookie from windows.form

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);

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?

 

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


CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明如何删除WinForm WebBrowser Cookie
喜欢 (0)
[1034331897@qq.com]
分享 (0)

文章评论已关闭!