How can I get all Cookies of a CookieContainer?

前端 未结 4 1403
挽巷
挽巷 2020-11-30 09:41

I want to export a CookieContainer to JSON using Newtonsoft.Json but unfortunately CookieContainer hasn\'t an enumerator or stuff, so I can\'t cycle through it ...

<

4条回答
  •  没有蜡笔的小新
    2020-11-30 10:20

    Use CookieContainer.GetCookies Method

    CookieCollection cookies = cookieContainer.GetCookies(new Uri(url));
    

    where url is URL of your site.

    In my case, I was not able to use reflection, as suggested in other answers. However, I did know URL of my site to query. I think it is even logical that container does not return all cookies blindly but returns them per URL because cookies always belong to a particular URL and cannot be used outside of context of the domain associated with them.

提交回复
热议问题