HttpClient GetStreamAsync 和 HTTP 状态代码? [英] HttpClient GetStreamAsync and HTTP status codes?

查看:31
本文介绍了HttpClient GetStreamAsync 和 HTTP 状态代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望按照 json.net 性能提示文档的推荐使用流,但是我无法找到如何在没有 典型的等待 HttpResponse.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆