Checkout our demo site to practice selenium https://magento.softwaretestingboard.com/

0 like 0 dislike
250 views
in Programming by
retagged by
I am using c#.net to send POST request and receive response. I've setup CookingContainer in my request. But when I try to receive cookies in response, its null. How do I set it up?

1 Answer

0 like 0 dislike
by The go-to Tester (181 points)
selected by
 
Best answer

First: We need to initiate cookie collector with Request.

httpRequest.CookieContainer = new CookieContainer();

Second: In case cookie collector is corrupted, we need to manually fix it.

CookieCollection cookiesCollections = response.Cookies;
            if(cookiesCollections == null)
            {
                {
                    for (int i = 0; i < response.Headers.Count; i++)
                    {
                        string name = response.Headers.GetKey(i);
                        if (name != "Set-Cookie")
                            continue;
                        string value = response.Headers.Get(i);
                        foreach (var singleCookie in value.Split(','))
                        {
                            Match match = Regex.Match(singleCookie, "(.+?)=(.+?);");
                            if (match.Captures.Count == 0)
                                continue;
                            response.Cookies.Add(
                                new Cookie(
                                    match.Groups[1].ToString(),
                                    match.Groups[2].ToString(),
                                    "/",
                                    httpRequest.Host.Split(':')[0]));
                        }
                    }
                }


This site is for software testing professionals, where you can ask all your questions and get answers from 1300+ masters of the profession. Click here to submit yours now!

1.4k questions

1.6k answers

866 comments

1.9k users

...