【问题标题】:Taking the header 'setcookie' and turning it into a Cookie object获取标头“setcookie”并将其转换为 Cookie 对象
【发布时间】:2018-09-05 02:34:24
【问题描述】:

如何将一组 cookie 转换为 Cookie 对象?

var client = new HttpClient(handler);
var content = await client.GetAsync("http://example.com");
var cookies = content.Headers.GetValues("Set-Cookie");

我试过了,但是没用

var cliente = new RestClient("http://example.com");
cliente.CookieContainer = cookiesC; // CookieContainer cookiesC = new CookieContainer();
var parsedCookies = string.Join(";", cookies);
cookiesC.SetCookies(new Uri("http://example.com"), parsedCookies);
var res = cliente.Execute(new RestRequest("/", Method.GET));

如果我使用 httpClient 只是为了捕获 cookie 并在继续使用 restsharp 后不久。我正在寻找如何将 cookie 从 httpClient 传输到 RestSharp

【问题讨论】:

  • 你用的是哪个? HttpClient 还是 RestClient?您想要解决哪个问题?
  • 如果我使用 httpClient 只是为了捕获 cookie,然后很快就转到 RestClient。我正在寻找如何将 cookie 从 httpClient 传输到 RestClient

标签: c# cookies http-headers


【解决方案1】:

你需要像这样声明你的HttpClient

CookieContainer cookiesC= new CookieContainer();
HttpClientHandler handler = new HttpClientHandler();
handler.CookieContainer = cookiesC;
HttpClient client = new HttpClient(handler);

这将导致HttpClient 使用cookiesC 容器,然后您可以(看似)将其与RestClient 一起使用。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-11-28
  • 2019-05-25
  • 1970-01-01
  • 2021-07-23
  • 2023-04-06
  • 2016-04-22
  • 1970-01-01
相关资源
最近更新 更多