C# HttpClient手动设置cookie 电脑版发表于:2020/5/22 14:18 C# HttpClient手动设置cookie ``` var handler = new HttpClientHandler() { UseCookies = false}; var client = new HttpClient(handler);// { BaseAddress = baseAddress }; var message = new HttpRequestMessage(HttpMethod.Get, url); message.Headers.Add("Cookie", "session_id=50F5240AD76949FE8D7B721B2C012183"); var result = await client.SendAsync(message); result.EnsureSuccessStatusCode(); ``` 当然cookie设置,其他值也可以,比如token ``` //获取cookie的token信息 string token; HttpContext.Request.Cookies.TryGetValue("token", out token); //发送请求携带token HttpClient httpClient = new HttpClient(); var message = new HttpRequestMessage(HttpMethod.Post, "http://localhost:XJ520/api/v1/Token"); message.Headers.Add("Cookie", "token=" + token); var httpResponseHeaders = httpClient.SendAsync(message).Result; ```