0

I triying to get cookies from a URL. I test with anothers URLs and works fine: I can get the cookies, but with my url doesn't return the cookie. The code:

                using (var client = new HttpClient())
                {

                    var response = client.GetAsync("http://192.168.5.230:5050/users/session").Result;

                    if (response.IsSuccessStatusCode)
                    {
                        var responseContent = response.Content;

                        // by calling .Result you are synchronously reading the result
                        string responseString = responseContent.ReadAsStringAsync().Result;

                        MessageBox.Show(responseString);
                        IEnumerable<string> cookieHeader;
                        response.Headers.TryGetValues("set-cookie", out cookieHeader);


                        MessageBox.Show(cookieHeader.Last().ToString()); // THIS LINE RETURN NULL

                    }
                }

This line returns null:

MessageBox.Show(cookieHeader.Last().ToString());

I get the url by curl: screenshot here and returns a cookie.

I thought that the problem was the capital case of the "set-cookie" but in the code is exactly equal to the http response.

What can be the problem?

Sam Axe
  • 33,313
  • 9
  • 55
  • 89
mark
  • 59
  • 1
  • 6
  • 1
    Does this answer your question? [Can't get any cookies with C# HttpClient](https://stackoverflow.com/questions/53906224/cant-get-any-cookies-with-c-sharp-httpclient) – Slava Knyazev Jun 18 '20 at 19:35
  • @SlavaKnyazev: his problem is more severe than that, the whole code does not make any sense, and in short cookies are not part of a URL neither could they be acquired from a URL. Cookies are properties attached to stateful requests. – Transcendent Jun 18 '20 at 19:47
  • @Transcendent What are you talking about? The code is clearly sending an http request, where cookies are returned as a header (as they should be). – Slava Knyazev Jun 18 '20 at 20:10
  • @SlavaKnyazev that solve my problem. Sorry for dont test that answer. – mark Jun 18 '20 at 20:10
  • @SlavaKnyazev: This is code is a copy paste, and the OP copycat without even trying to understand what it does. I have no time to post an answer, but if I had I would explain the whole thing in detail so that he would first understand what in particular he has to do. For examples, basics of HTTP requests, headers, and other properties in addition to why a request must carry a cookie at all or if it is allowed and then how to tweak the cookies where it is applicable. With another direct answer in another post, it will be another copycat. – Transcendent Jun 18 '20 at 20:13

0 Answers0