Need to make a request to server which need specific cookies. Able to do this using HTTP client and handler with cookiecontainer. By using Typed clients, not able to find a way to set cookiecontainer.
Using httpclient:
var cookieContainer = new CookieContainer();
using (var handler = new HttpClientHandler() { CookieContainer = cookieContainer })
using (HttpClient client = new HttpClient(handler))
{
//.....
// Used below method to add cookies
AddCookies(cookieContainer);
var response = client.GetAsync('/').Result;
}
Using HttpClientFactory:
In startup.cs
services.AddHttpClient<TypedClient>().
ConfigurePrimaryHttpMessageHandler(() => new HttpClientHandler
{
CookieContainer = new CookieContainer()
});
In controller class
// Need to call AddCookie method here
var response =_typedclient.client.GetAsync('/').Result;
In Addcookie method, I need to add cookies to container. Any suggestions how to do this.
Create an abstraction to provide access to an instance of
CookieContainerFor example
Add cookie container to service collection during startup and use it to configure primary HTTP message handler
Startup.ConfigureServices
And lastly, inject abstraction where needed
For example