在HttpClient内部使用语句我需要从HttpResponseMessage的某处解包。
using (HttpClient client = new HttpClient())
{
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", authUser);
Task<HttpResponseMessage> m = client.GetAsync(url);
// HttpResponseMessage msg = ???
task.Wait();
return task.Result;
}
我的问题是:如何从此行获取HttpResponseMessage
Task<HttpResponseMessage> m = client.GetAsync(url);
答案 0 :(得分:2)
答案 1 :(得分:0)
要获取HttpResponseMessasge,应使用Task.Result属性。 这是您从Task获得的HttpResponseMessage的代码(经过一些修改):
using (HttpClient client = new HttpClient())
{
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", authUser);
Task<HttpResponseMessage> task = client.GetAsync(uri);
**HttpResponseMessage msg = task.Result;**
task.Wait();
return task.Result;
}