如何从 HttpClientHandler.CookieContainer 获取 cookie
代码如下:
public static async Task<string> DownloadPageWithCookiesAsync(string url)
{
HttpClientHandler handler = new HttpClientHandler();
handler.UseDefaultCredentials = true;
handler.AllowAutoRedirect = true;
handler.UseCookies = true;
handler.CookieContainer = new CookieContainer();
HttpClient client = new HttpClient(handler);
HttpResponseMessage response = await client.GetAsync(url);
response.EnsureSuccessStatusCode();
string responseBody = response.Content.ReadAsString();
return responseBody;
}
client.GetAsync(url);
运行后,handler.CookieContainer
包含 7 个 cookie。我如何访问它们?
Here's the code:
public static async Task<string> DownloadPageWithCookiesAsync(string url)
{
HttpClientHandler handler = new HttpClientHandler();
handler.UseDefaultCredentials = true;
handler.AllowAutoRedirect = true;
handler.UseCookies = true;
handler.CookieContainer = new CookieContainer();
HttpClient client = new HttpClient(handler);
HttpResponseMessage response = await client.GetAsync(url);
response.EnsureSuccessStatusCode();
string responseBody = response.Content.ReadAsString();
return responseBody;
}
after the client.GetAsync(url);
runs, the handler.CookieContainer
contains 7 cookies. How can I access them?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用 CookieContainer 的 GetCookies 方法,指定您想要 cookie 的 URI。它返回一个您可以枚举的 CookieCollection。
Use the CookieContainer's GetCookies method, specifying the URI you want cookies for. It returns a CookieCollection you can enumerate.
尝试:
我相信,您还可以使用 foreach 循环迭代 CookieCollection。
Try:
You can also iterate the CookieCollection with a foreach loop, I believe.