在 full .net framework中,可以使用该类库中的扩展方法,将HttpContent强类型化输出。
https://www.nuget.org/packages/System.Net.Http.Formatting.Extension/
但是不支持 netstandard1.x
自己实现了个简单的
public static async Task<T> ReadAsync<T>(this HttpContent httpContent) { var mediaType = httpContent.Headers.ContentType.MediaType; if (mediaType != "application/json") { throw new FormatException(); } using (var stream = await httpContent.ReadAsStreamAsync()) using (var sr = new StreamReader(stream)) using (var jsonTestReader = new JsonTextReader(sr)) { var jsonSerializer = new JsonSerializer(); return jsonSerializer.Deserialize<T>(jsonTestReader); } }