HttpClient不保存Cookie

Err*_*rin 12 c# dotnet-httpclient

我正在使用新的HttpClient来处理我项目的网上冲浪需求; 但是,尽管设置正确,但HttpClient不会将cookie保存到Cookie容器中,并且它始终为EMPTY.

private CookieContainer _cookieContainer = new CookieContainer();
private HttpClient HttpClient { get; set; }
private HttpClientHandler HttpClientHandler { get; set; }

public Initialize()
{
    HttpClientHandler = new HttpClientHandler
                            {
                                AllowAutoRedirect = true,
                                UseCookies = true,
                                CookieContainer = _cookieContainer
                            };
    HttpClient = new HttpClient(HttpClientHandler);
}

public CookieContainer Cookies
{
    get { return _cookieContainer; }
    set { _cookieContainer = value; }
}

public void TEST()
{
    //This is always empty, although I am sure that the site is saving login cookies
    var cookies = Cookies;
}
Run Code Online (Sandbox Code Playgroud)

Rom*_*key 16

很奇怪......你试过直接使用HttpClientHandler的CookieContainer吗?

代码:

public Initialize()
{
    HttpClientHandler = new HttpClientHandler
                            {
                                AllowAutoRedirect = true,
                                UseCookies = true,
                                CookieContainer = new CookieContainer()
                            };
    HttpClient = new HttpClient(HttpClientHandler);
}

public CookieContainer Cookies
{
    get { return HttpClientHandler.CookieContainer; }
    set { HttpClientHandler.CookieContainer = value; }
}
Run Code Online (Sandbox Code Playgroud)