c# - 如何从 HttpClientHandler.CookieContainer 获取 cookie

标签 c# .net windows-runtime

代码如下:

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。我如何访问它们?

最佳答案

使用 CookieContainer 的 GetCookies 方法,指定您想要 cookie 的 URI。它返回一个您可以枚举的 CookieCollection。

关于c# - 如何从 HttpClientHandler.CookieContainer 获取 cookie,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8315251/

相关文章:

c# - Windows 运行时组件中不可能继承?

c# - IApplicationActivationManager::ActivateApplication 在 C# 中?

c# - 是否有用于 .net 的序列化程序将输出 c# 代码?

c# - 将空值传递给重载方法,其中 Object 和 String 作为 C# 中的参数

c# - 当我尝试通过 WinRT 对象从 C++ 调用回调到 C# 时如何解决此 E_POINTER (NullPointerException)?

c# - 是否可以在 C# 中编写将在 Windows 8 Metro 环境中运行的 Ping 类?

c# - 异步多线程异常处理?

c# - 如何使用 MVVM 绑定(bind)多个数据网格源?

c# - 如何通过 asp.net-webform 项目模板创建 asp.net-mvc3 项目(不要安装 asp.net-mvc3-dlls 和 asp.net-mvc3-vs-tool)?

.net - 从命令行调用 MSBuild 时转义 DisabledWarnings 的正确方法是什么?