Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get cookies from HttpClientHandler.CookieContainer

Here's the code:

public static async Task<string> DownloadPageWithCookiesAsync(string url)
{
    HttpClientHandler handler = new HttpClientHandler();
    handler.UseDefaultCredentials = true;
    handler.AllowAutoRedirect = true;
    handler.UseCookies = true;
    handler.CookieContainer = new CookieContainer();
    HttpClient client = new HttpClient(handler);
    HttpResponseMessage response = await client.GetAsync(url);
    response.EnsureSuccessStatusCode();

    string responseBody = response.Content.ReadAsString();
    return responseBody;
}

after the client.GetAsync(url); runs, the handler.CookieContainer contains 7 cookies. How can I access them?

like image 857
Alireza Noori Avatar asked Nov 29 '11 17:11

Alireza Noori


1 Answers

Use the CookieContainer's GetCookies method, specifying the URI you want cookies for. It returns a CookieCollection you can enumerate.

like image 107
Peter Oehlert Avatar answered Nov 15 '22 00:11

Peter Oehlert