【问题标题】:Delete cookies in webBrowser without restart无需重启即可删除 webBrowser 中的 cookie
【发布时间】:2014-02-11 11:49:05
【问题描述】:

你能说我如何删除private System.Windows.Forms.WebBrowser webBrowser1 中的所有cookie。我用过

[DllImport("wininet.dll", SetLastError = true)]
private static extern bool InternetSetOption(IntPtr hInternet, int dwOption, IntPtr lpBuffer, int lpdwBufferLength);

我想通过点击按钮来做到这一点。

private void button1_Click(object sender, EventArgs e)
{
         InternetSetOption(IntPtr.Zero, INTERNET_OPTION_END_BROWSER_SESSION, IntPtr.Zero, 0);
} 

但它不起作用。我知道 webBrowser1.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());}}}})())"); 不适合我。

【问题讨论】:

  • 这可能有效,但没有记录:stackoverflow.com/q/17369275/1768303。这也可能会有所帮助,具体取决于您的目标:stackoverflow.com/a/21254020/1768303
  • 我不太明白应该将动态document=webBroweser.Document; document.execCommand("ClearAuthenticationCache", false); 放在我的按钮中的什么位置??我在 System.Core.dll 中得到 Microsoft.CSharp.RuntimeBinder.RuntimeBinderException
  • System.Windows.Forms.HtmlDocument 不包含“execCommand”的定义
  • 您忘记了dynamic 关键字:dynamic document=webBroweser.Document;。没有hard-typed execCommand 方法,除非你使用MSHTML PIA。
  • 我只在这条评论中忘记了。

标签: c# cookies browser webbrowser-control


【解决方案1】:

正确的打法如下:

dynamic document = webBrowser1.Document;
document.ExecCommand("ClearAuthenticationCache", false, null);

干杯。

【讨论】:

  • 不适用于 .Net 4.5 和 Facebook 登录流程。
猜你喜欢
  • 2020-01-19
  • 2020-04-08
  • 2017-10-24
  • 1970-01-01
  • 2014-07-18
  • 2017-08-10
  • 1970-01-01
  • 1970-01-01
  • 2011-12-29
相关资源
最近更新 更多