c# - 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 中的请求确认了这一点。据我所知,这应该可以正常工作。
解决方案
看起来您需要将处理程序绑定到客户端,可能是这样的:
using (var client = new HttpClient(clientHandler))
{
var Response = client.SendAsync(Request);
}
推荐阅读
- c# - 在 c# 上将自动编号列添加到 ListView
- arm - 为什么树莓派即使有ARM处理器也非常慢
- vb.net - 使用 CompileAssemblyFromSource 时未找到 Sub Main
- sql - 使用 JOIN 添加较小的表
- json - 如何使用 jq 将 JSON 字符串格式化为“key=value”格式?
- java - 在 Spring MVC 中将 JSON 数据传递给 POST
- python - 使用 ProcessPoolExcecutor 进行多处理会产生意想不到的结果
- crash - 在 Windows 10 上运行“新查询”会导致 SQL Server Management Studio 2019 崩溃
- c# - 在链表中如果我传递对象数据然后搜索代码不起作用
- django - 从 django settings.py 访问 javascript 中的 API 密钥