HttpClient GetStreamAsync and HTTP status codes?

前端 未结 2 1041
小蘑菇
小蘑菇 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:11

    I prefer to dispose of the HttpResponseMessage via using as it is disposable. I also prefer to not rely on exception handling to deal with failed requests. Instead I prefer to check against the IsSuccessStatusCode boolean value and proceed accordingly. For example:

    using(HttpClient client = new HttpClient())
    {
        using(var response = await client.GetAsync("http://httpbin.org/get", HttpCompletionOption.ResponseHeadersRead))
        {
            if(response.IsSuccessStatusCode)
            {
                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
                }
            }
            else {
                //do your error logging and/or retry logic
            }
        }       
    }
    

    EDIT: If you're doing work with a rate limited api sending the HEAD request just isn't sometimes feasible. As such, here's a code sample using the good ol' fashion HttpWebRequest (note that there isn't a better way to deal with http errors than WebException in this case):

    var req = WebRequest.CreateHttp("http://httpbin.org/get");
    
    /*
     * execute
     */
    try
    {
        using (var resp = await req.GetResponseAsync())
        {
            using (var s = resp.GetResponseStream())
            using (var sr = new StreamReader(s))
            using (var j = new JsonTextReader(sr))
            {
                var serializer = new JsonSerializer();
                //do some deserializing http://www.newtonsoft.com/json/help/html/Performance.htm
            }
        }
    }
    catch (WebException ex)
    {
        using (HttpWebResponse response = (HttpWebResponse)ex.Response)
        {
            using (StreamReader sr = new StreamReader(response.GetResponseStream()))
            {
                string respStr = sr.ReadToEnd();
                int statusCode = (int)response.StatusCode;
    
                //do your status code logic here
            }
        }
    }
    
    0 讨论(0)
  • 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
        }
    }
    
    0 讨论(0)
提交回复
热议问题