我正在从 .net 框架到 .net 核心的迁移过程中,发现 HttpWebRequest/HttpWebResponse 出现了一些奇怪的情况:任务:需要将 HttpWebRequest 发送到带有值为“fail”的 cookie A 的某个地址并从响应中获取 cookie A(但期望cookie A 在响应标头中的值为“Ok”)。如果您尝试按名称从响应中获取 cookie(例如 response.Cookies[“A”]),对于 .net 框架,您将获得“Ok”,对于 .net 核心,您将获得“fail”。
这是示例(1p_JAR 包含在请求和响应中):
CookieContainer cookieContainer = new CookieContainer();
Uri uri = new Uri("https://google.com");
cookieContainer.SetCookies(uri, "k1=1111,k2=2222,k3=3333,1P_JAR=zzz");
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
request.CookieContainer = cookieContainer;
request.Method = "GET";
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Console.WriteLine("Cookies from response: ");
foreach (Cookie cookie in response.Cookies)
{
Console.WriteLine(" " + cookie.Name + ": " + cookie.Value);
}
Console.WriteLine("Get cookie by name: " + response.Cookies["1P_JAR"]);
.net 框架的结果:
Cookies from response:
1P_JAR: 2018-10-26-13
NID: bla-bla-bla
Get cookie by name: 1P_JAR=2018-10-26-13
.net 核心的结果:
Cookies from response:
k1: 1111
k2: 2222
k3: 3333
1P_JAR: zzz
1P_JAR: 2018-10-26-13
NID: bla-bla-bla
Get cookie by name: 1P_JAR=zzz
如何在 .net 核心中重复 HttpWebRequest/HttpWebResponse 的 .net 框架实现的行为?