需要向需要特定 cookie 的服务器发出请求。能够使用带有 cookiecontainer 的 HTTP 客户端和处理程序来执行此操作。通过使用 Typed 客户端,无法找到设置 cookiecontainer 的方法。
使用 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;
} 
使用 HttpClientFactory:
在启动.cs
services.AddHttpClient<TypedClient>().
           ConfigurePrimaryHttpMessageHandler(() => new HttpClientHandler
           {
               CookieContainer = new CookieContainer()
            });
在控制器类
// Need to call AddCookie method here
var response =_typedclient.client.GetAsync('/').Result;
在 Addcookie 方法中,我需要将 cookie 添加到容器中。任何建议如何做到这一点。