使用HttpClient发送网络请求时,返回HttpResponseMessage,那么怎样从HttpResponseMessage中获取想要的数据呢?

这就取决于实际需求了,情况分类如下:

1.只要原始字符串

Task<string> stringObj = response.Content.ReadAsStringAsync();
Console.WriteLine("Resp string: " + stringObj .Result); //Raw string
  • 1.
  • 2.

2.要组织好的类型数据

Task<Device> typedObj = response.Content.ReadAsAsync<T>().Result;
Console.WriteLine("Resp device: " + typedObj .Result); //Device data
  • 1.
  • 2.