如何在.net core中以编程方式将Cookie附加到http请求?

时间:2019-05-13 21:14:08

标签: c# asp.net-core

我可以通过以下方法将Cookie集合用作IRequestCookieCollection吗?如何将它们附加到请求?谢谢!

public static HttpClient GetHttpClient(string baseUri, IRequestCookieCollection cookies)
    {
        var cookieContainer = new CookieContainer();
        var handler = new HttpClientHandler() { CookieContainer = cookieContainer };
        var client = new HttpClient(handler) { BaseAddress = new Uri(baseUri) };

        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

        //How to attach cookies to the request?

        return client;
    }

0 个答案:

没有答案