user547794
user547794

Reputation: 14511

Grabbing cookie set after post with httpWebClient

I am trying to grab the cookie that is set in the response after a successful POST. How can I grab the cookies from the result?

var baseAddress = new Uri("http://rtchatserver");
            var cookieContainer = new CookieContainer();
            using (var postHandler = new HttpClientHandler() { CookieContainer = cookieContainer })
            using (var client = new HttpClient(postHandler) {BaseAddress = baseAddress})
            {
                var content = new FormUrlEncodedContent(new[]
                {
                    new KeyValuePair<string, string>("UserName", "bar"),
                    new KeyValuePair<string, string>("Password", "bazinga"),
                    new KeyValuePair<string, string>("__RequestVerificationToken", token),
                    new KeyValuePair<string, string>("returnUrl", "http://google.com"),
                });
                cookieContainer.Add(baseAddress, responseCookies.FirstOrDefault());
                var result = client.PostAsync("/account/login", content).Result;

                result.EnsureSuccessStatusCode();
               }

Upvotes: 0

Views: 200

Answers (1)

Den Pakizh
Den Pakizh

Reputation: 384

You can use CookieContainer.GetCookies method:

var cookies = cookieContainer.GetCookies(new Uri("http://rtchatserver/account/login"));

Upvotes: 1

Related Questions