4

I want to clear ALL the cookies received in a CookieContainer without the need to initialize a new CookieContainer, HttpClientHandler and HttpClient. Is there a way? I've checked MSDN but it seems I can only use GetCookies(Uri) to get all the cookies associated with a particular Uri.

var cc = new CookieContainer();

var handler = new HttpClientHandler
    {
        CookieContainer = cc
    };

var client = new HttpClient(handler);
derekhh
  • 5,272
  • 11
  • 40
  • 60
  • It appears you need to use reflection. What is the issue with creating a new HttpClient? http://stackoverflow.com/questions/15983166/how-can-i-get-all-cookies-of-a-cookiecontainer – dana Oct 08 '15 at 20:47
  • @dana: I just feel then it might be a waste of resources since I'm using multiple tasks. – derekhh Oct 08 '15 at 20:49
  • 2
    Personally, I wouldn't worry about creating a few more instances of a class if the alternative is to use reflection to break into private members. This is especially true since you are already making network requests over HTTP. – dana Oct 09 '15 at 01:00

1 Answers1

6

The only solution I know is to expire all cookies:

        cc.GetCookies(new Uri(...))
            .Cast<Cookie>()
            .ToList()
            .ForEach(c => c.Expired = true);
Paolo Mazzoni
  • 73
  • 1
  • 7