[英]How to read cookies from HttpResponseMessage?
This is my recent code:这是我最近的代码:
HttpClient authClient = new HttpClient();
authClient.BaseAddress = new Uri("http://localhost:4999/test_db/_session");
authClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var user = new LoginUserSecretModel
{
name = userKey,
password = loginData.Password,
};
HttpResponseMessage authenticationResponse = authClient.PostAsJsonAsync("", user).Result;
The issue I have with many of the answers here is that using CookieContainer
uses short-lived HttpClient
objects which is not recommended .我在这里的许多答案中遇到的问题是,使用CookieContainer
使用的是不推荐的短期HttpClient
对象。
Instead, you can simply read the "Set-Cookie"
header from the response:相反,您可以简单地从响应中读取"Set-Cookie"
标头:
// httpClient is long-lived and comes from a IHttpClientFactory
HttpResponseMessage response = await httpClient.GetAsync(uri);
IEnumerable<string> cookies = response.Headers.SingleOrDefault(header => header.Key == "Set-Cookie")?.Value;
Try this:尝试这个:
CookieContainer cookies = new CookieContainer();
HttpClientHandler handler = new HttpClientHandler();
handler.CookieContainer = cookies;
HttpClient authClient = new HttpClient(handler);
var uri = new Uri("http://localhost:4999/test_db/_session");
authClient.BaseAddress = uri;
authClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var user = new LoginUserSecretModel
{
name = userKey,
password = loginData.Password,
};
HttpResponseMessage authenticationResponse = authClient.PostAsJsonAsync("", user).Result;
var responseCookies = cookies.GetCookies(uri).Cast<Cookie>();
This is what you need to get a list of cookies;这是获取 cookie 列表所需要的;
private async Task<List<Cookie>> GetCookies(string url, string cookieName)
{
var cookieContainer = new CookieContainer();
var uri = new Uri(url);
using (var httpClientHandler = new HttpClientHandler
{
CookieContainer = cookieContainer
})
{
using (var httpClient = new HttpClient(httpClientHandler))
{
await httpClient.GetAsync(uri);
List<Cookie> cookies = cookieContainer.GetCookies(uri).Cast<Cookie>().ToList();
return cookies;
}
}
}
and if you need only one cookie value here's how如果您只需要一个 cookie 值,方法如下
private async Task<string> GetCookieValue(string url)
{
var cookieContainer = new CookieContainer();
var uri = new Uri(url);
using (var httpClientHandler = new HttpClientHandler
{
CookieContainer = cookieContainer
})
{
using (var httpClient = new HttpClient(httpClientHandler))
{
await httpClient.GetAsync(uri);
var cookie = cookieContainer.GetCookies(uri).Cast<Cookie>().FirstOrDefault(x => x.Name == cookieName);
return cookie?.Value;
}
}
}
Building on top of Daniel's answer and this answer to another question , this would be an easy way to read the cookies from an HTTP response.基于Daniel 的回答和对另一个问题的回答,这将是一种从 HTTP 响应中读取 cookie 的简单方法。
// httpClient is long-lived and comes from a IHttpClientFactory
HttpResponseMessage response = await httpClient.GetAsync(uri);
CookieContainer cookies = new CookieContainer();
foreach (var cookieHeader in response.Headers.GetValues("Set-Cookie"))
cookies.SetCookies(uri, cookieHeader);
string cookieValue = cookies.GetCookies(uri).FirstOrDefault(c => c.Name == "MyCookie")?.Value;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.