C#响应后如何获取Request Header的Cookie

How to get Cookie of Request Header after reponse in C#

我想要获取 cookies "Request Header" 而不是链接“https://www.udemy.com/join/login-popup/”的 "Reponse Header" 我使用过 WebRequest 和 HttpClient 但是当我没有在其中看到 Cookie 调试时 请帮助我用 C# 编写它我发现试图找到所有可能的方法,但我仍然没有找到。

System.Net.Http.HttpClient为例,我们可以得到如下cookie:

var myClientHandler = new HttpClientHandler();
myClientHandler.CookieContainer = new CookieContainer();

var client = new HttpClient(myClientHandler);

var response = await client.GetAsync("https://www.udemy.com/join/login-popup/");

var cookieCollection = myClientHandler.CookieContainer.GetCookies(new Uri("https://www.udemy.com/join/login-popup/"));

foreach (var cookie in cookieCollection.Cast<Cookie>())
{
    Debug.WriteLine(cookie);
}

HttpClient 保存服务器发送的 cookie,并自动将它们添加到同一应用程序容器中对该 URI 的后续请求。所以如果我们不手动修改cookies,"Request Header"中的cookies应该和之前reponse中返回的一样。