我正在尝试获取POST成功后在响应中设置的cookie。如何从结果中获取cookie?
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();
}
答案 0 :(得分:1)
您可以使用CookieContainer.GetCookies方法:
var cookies = cookieContainer.GetCookies(new Uri("http://rtchatserver/account/login"));