将Http头添加到HttpClient

所有:

在向Web服务发送请求之前,我需要将http头添加到HttpClient。 我如何做到这一点的个人请求(而不是在HttpClient所有未来的请求)? 我不确定这是否可能。

var client = new HttpClient(); var task = client.GetAsync("http://www.someURI.com") .ContinueWith((taskwithmsg) => { var response = taskwithmsg.Result; var jsonTask = response.Content.ReadAsAsync<JsonObject>(); jsonTask.Wait(); var jsonObject = jsonTask.Result; }); task.Wait(); 

创build一个HttpRequestMessage ,将方法设置为GET ,设置您的标题,然后使用SendAsync而不是GetAsync

 var client = new HttpClient(); var request = new HttpRequestMessage() { RequestUri = new Uri("http://www.someURI.com"), Method = HttpMethod.Get, }; request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("text/plain")); var task = client.SendAsync(request) .ContinueWith((taskwithmsg) => { var response = taskwithmsg.Result; var jsonTask = response.Content.ReadAsAsync<JsonObject>(); jsonTask.Wait(); var jsonObject = jsonTask.Result; }); task.Wait();