title: ASP.NET Core 2 get cookies from HttpResponseMessage
body: <p>currently I am working on a Project written with ASP.NET Core 2 and I am trying to get a JSON file from a third party website. The problem is that this website requires a few cookies in order to retrieve data from it. I implemented a typed HttpClient in my Startup.cs file like this:</p>
  
  <pre><code>    services.AddHttpClient&lt;IMyClient, MyClient&gt;().ConfigurePrimaryHttpMessageHandler(() =&gt;
      {
          return new HttpClientHandler()
          {
              UseCookies = true,
              UseDefaultCredentials = true,
              CookieContainer = new CookieContainer()
          };
      });
  </code></pre>
  
  <p>Is there any way of accessing the CookieContainer to use the CookieContainer.GetCookies() method, so that I can copy various cookies like the Session Cookie and some verification tokens from the HttpResponseMessage?</p>
  
  <p>Sorry if I am making something wrong, this is my first post.</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>Got it to work by adding a HttpMessageHandler in the Request Pipeline</p>
  
  <pre><code>services.AddHttpClient&lt;IMyHttpClient, MyHttpClient&gt;()
                      .AddHttpMessageHandler&lt;MyMessageHandler&gt;();
  </code></pre>
  
  <p>and edit the http header information in the HttpMessageHandler</p>
  
  <pre><code>public class MyMessageHandler : DelegatingHandler
  {
      private readonly CookieContainer _cookies;
  
      public MyMessageHandler()
      {
          _cookies = new CookieContainer();
      }
  
      protected override async Task&lt;HttpResponseMessage&gt; SendAsync(HttpRequestMessage message, CancellationToken cancellationToken)
      {
          try
          {
              if (_cookies.Count == 0 &amp;&amp; message.RequestUri.Host.Contains("example.com"))
              {
                  // Simulate the Request
                  var getCookieMesssage = new HttpRequestMessage()
                  {
                      RequestUri = new Uri("http://www.example.com/"),
                      Content = message.Content,
                      Method = HttpMethod.Get,
                  };
  
                  // Simulate User Agent
                  foreach(var agent in message.Headers.UserAgent)
                  {
                      getCookieMesssage.Headers.UserAgent.Add(agent);
                  }
  
                  var testResponse = await base.SendAsync(getCookieMesssage, cancellationToken);
  
                  if (testResponse.Headers.TryGetValues("Set-Cookie", out var newCookies))
                  {
                      foreach (var item in SetCookieHeaderValue.ParseList(newCookies.ToList()))
                      {
                          var uri = new Uri(message.RequestUri, item.Path.Value);
  
                          // Add parsed cookies to CookieContainer
                          _cookies.Add(uri, new Cookie(item.Name.Value, item.Value.Value, item.Path.Value));
                      }
                  }
                  // Add parsed cookies to the header of the HttpRequestMessage
                  message.Headers.Add("Cookie", _cookies.GetCookieHeader(message.RequestUri));
              }
              // get response
              var response = await base.SendAsync(message, cancellationToken).ConfigureAwait(false);
  
              return response;
          }catch(Exception ex)
          {
              throw ex;
          }
  
      }
  
  }
  </code></pre>
  
  <p>This approach gets all cookies from the Set-Cookie header and adds it to the Cookie header of the request.</p>