获取标头“ setcookie”并将其转换为Cookie对象

时间:2018-09-05 02:34:25

标签: c# cookies http-headers

如何将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

1 个答案:

答案 0 :(得分:0)

您需要这样声明您的HttpClient

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

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