首页 > 解决方案 > HttpClient SendAsync 未发送 cookie

问题描述

var Url = "https://myhost.com/api";
var UrlEncodedContent = GetContent();
var CookieContainer = new CookieContainer();
using(var clientHandler = new HttpClientHandler() { CookieContainer = CookieContainer })
{
    Uri Endpoint = new Uri("https://myhost.com");
    CookieContainer.Add(Endpoint, new Cookie("mycookie", "abc"));

    var Request = new HttpRequestMessage(HttpMethod.Post, Url) { Content = UrlEncodedContent };

    var Response = MyHttpClient.SendAsync(Request); 
} 

客户端没有发送 cookie。我通过查看 Fiddler 中的请求确认了这一点。据我所知,这应该可以正常工作。

标签: c#.netcookies.net-corehttpclient

解决方案


看起来您需要将处理程序绑定到客户端,可能是这样的:

using (var client = new HttpClient(clientHandler))
{
    var Response = client.SendAsync(Request);
}

推荐阅读