【问题标题】:Clear a CookieContainer清除 CookieContainer
【发布时间】:2016-09-23 19:06:22
【问题描述】:

我想清除我的 CookieContainer 中的所有 cookie。如果我这样做:

CookieContainer cookieContainer = new CookieContainer();
var handler = new HttpClientHandler { CookieContainer = cookieContainer };
var httpClient = new HttpClient(handler);
// make some requests
cookieContainer = new CookieContainer();

httpClient 会有新的(空的)cookieContainer,还是我必须重新创建我的 httpClient?

【问题讨论】:

  • 是的。 cookie 将被清除。
  • @ThePerplexedOne 谢谢!

标签: c# httpclient


【解决方案1】:

请注意,您正在更新本地引用,而不是 HttpClientHandler 中对 CookieContainer 的引用。您可以在下面的答案中看到使 CookieContainer 中的 cookie 过期的唯一方法,将 Expires 属性设置为过去的 DateTime。

How to remove cookies under 1 domain in CookieContainer

如果您不关心性能,创建一个新的 HttpClient 也可以修复它。

【讨论】:

    猜你喜欢
    • 2016-01-06
    • 2010-11-06
    • 2012-08-15
    • 1970-01-01
    • 2014-06-16
    • 1970-01-01
    • 2019-04-24
    • 2014-06-14
    • 1970-01-01
    相关资源
    最近更新 更多