HttpClient does not save cookies

I use the new HttpClient to handle my web surfing project needs; However, although it is correctly installed, HttpClient does not save cookies in the Cookie container, and it is always EMPTY.

Code

private CookieContainer _cookieContainer = new CookieContainer(); private HttpClient HttpClient { get; set; } private HttpClientHandler HttpClientHandler { get; set; } public Initialize() { HttpClientHandler = new HttpClientHandler { AllowAutoRedirect = true, UseCookies = true, CookieContainer = _cookieContainer }; HttpClient = new HttpClient(HttpClientHandler); } public CookieContainer Cookies { get { return _cookieContainer; } set { _cookieContainer = value; } } public void TEST() { //This is always empty, although I am sure that the site is saving login cookies var cookies = Cookies; } 
+6
source share
1 answer

Strange ... Have you tried directly using the CookieContainer HttpClientHandler?

Code:

 public Initialize() { HttpClientHandler = new HttpClientHandler { AllowAutoRedirect = true, UseCookies = true, CookieContainer = new CookieContainer() }; HttpClient = new HttpClient(HttpClientHandler); } public CookieContainer Cookies { get { return HttpClientHandler.CookieContainer; } set { HttpClientHandler.CookieContainer = value; } } 
+8
source

Source: https://habr.com/ru/post/950770/


All Articles