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

前端 未结 7 1631
刺人心
刺人心 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:19

    The Cookies property will be null unless the CookieContainer is set on the HttpWebRequest. So the proper way to do this is to set the CookieContainer member before retrieving the response:

    var request = (HttpWebRequest)HttpWebRequest.Create(..);
    request.CookieContainer = new CookieContainer();
    
    var response = request.GetResponse();
    // ..response.Cookies will now contain the cookies sent back by the server.
    

    You don't need to manually parse Set-Cookie.

    See the documentation for more information.

提交回复
热议问题