Reputation: 14511
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
Reputation: 384
You can use CookieContainer.GetCookies method:
var cookies = cookieContainer.GetCookies(new Uri("http://rtchatserver/account/login"));
Upvotes: 1