HttpWebResponse.Cookies empty despite Set-Cookie Header (no-redirect)

前端 未结 7 1625
刺人心
刺人心 2020-12-28 14:00

I\'m struggling to figure out what is wrong here. I\'m sending login information, I can see the Set-Cookie in the Header with the correct value, but the Cookies collection

7条回答
  •  佛祖请我去吃肉
    2020-12-28 14:06

    It seems like Set-Cookie header sent by the website is malformed (Not in the typical format it should have been).

    In such case you need to Parse cookie manually and it it to the CookieContainer.

    for (int i = 0; i < b.Headers.Count; i++)
    {
        string name = b.Headers.GetKey(i);
        string value = b.Headers.Get(i);
        if (name == "Set-Cookie")
        {
            Match match = Regex.Match(value, "(.+?)=(.+?);");
            if (match.Captures.Count > 0)
            {
                reqCookies.Add(new Cookie(match.Groups[1].Value, match.Groups[2].Value, "/", "example.com"));
            }
        }
    }
    

提交回复
热议问题