HttpClient GetStreamAsync and HTTP status codes?

前端 未结 2 1046
小蘑菇
小蘑菇 2021-02-02 13:09

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 a

2条回答
  •  面向向阳花
    2021-02-02 13:35

    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
        }
    }
    

提交回复
热议问题