清除 CookieContainer

soo*_*qua 2 c# httpclient

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

CookieContainer cookieContainer = new CookieContainer();
var handler = new HttpClientHandler { CookieContainer = cookieContainer };
var httpClient = new HttpClient(handler);
// make some requests
cookieContainer = new CookieContainer();
Run Code Online (Sandbox Code Playgroud)

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

jor*_*nor 5

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

如何在 CookieContainer 中删除 1 个域下的 cookie

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