HttpClient GetStreamAsync和HTTP状态代码? [英] HttpClient GetStreamAsync and HTTP status codes?
本文介绍了HttpClient GetStreamAsync和HTTP状态代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我希望使用 json.net性能提示文档推荐的流,但是如果没有
I wish to use streams as recommended by the json.net performance tips documentation, however I'm unable to find how to get a hold of the http status codes without the typical awaiting the HttpResponse.
也许有一种不读取数据就先获取状态码的方法吗?所以仍然利用流吗?
Is there perhaps a way of getting the status code first without reading the data? So still taking advantage of streams?
推荐答案
我尚未进行测试以确保其性能,但这似乎很有希望:
I haven't tested to ensure it's performance, however this seems promising:
using(HttpClient client = new HttpClient())
{
var response = await client.GetAsync("http://httpbin.org/get", HttpCompletionOption.ResponseHeadersRead);
response.EnsureSuccessStatusCode();
using (var stream = await response.Content.ReadAsStreamAsync())
using (var streamReader = new StreamReader(stream))
using (var jsonReader = new JsonTextReader(streamReader))
{
var serializer = new JsonSerializer();
//do some deserializing http://www.newtonsoft.com/json/help/html/Performance.htm
}
}
这篇关于HttpClient GetStreamAsync和HTTP状态代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文