The issue I have with many of the answers here is that using CookieContainer
uses short-lived HttpClient
objects which is not recommended.
Instead, you can simply read the "Set-Cookie"
header from the response:
// httpClient is long-lived and comes from a IHttpClientFactory
HttpResponseMessage response = await httpClient.GetAsync(uri);
IEnumerable<string> cookies = response.Headers.SingleOrDefault(header => header.Key == "Set-Cookie").Value;
If the "Set-Cookie"
header is not present, cookies
will be null.