Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Distinguish between HttpClient request failure types

I need to write code to handle the 2 distinct types of failures that can arise after issuing an HTTP request using an HttpClient object that comes with .Net Framework 4.5:

  1. Connectivity failures, when the server can not be reached due to connection problems - e.g. no connection available, timeout expiration, incorrect network settings etc.

  2. Although technically not a failure, the case when the request successfully reaches the server but the server responds with an HTTP error status code (e.g. 404, 400, 409, 500 etc.)

It is very important that one failure type is not treated as the other. Which is the correct and reliable way to do this? The EnsureSuccessStatusCode() method seems to help with at least the second case, but the documentation is disappointingly scarce.

like image 805
Gabriel S. Avatar asked Jun 26 '13 16:06

Gabriel S.


People also ask

What is GetAsync in Web API?

GetAsync(String) Send a GET request to the specified Uri as an asynchronous operation. GetAsync(Uri) Send a GET request to the specified Uri as an asynchronous operation.

What is an HttpClient in C#?

The HttpClient class instance acts as a session to send HTTP requests. An HttpClient instance is a collection of settings applied to all requests executed by that instance. In addition, every HttpClient instance uses its own connection pool, isolating its requests from requests executed by other HttpClient instances.

Should I reuse HttpClient?

HttpClient is intended to be instantiated once and reused throughout the life of an application. The following conditions can result in SocketException errors: * Creating a new HttpClient instance per request. * Server under heavy load. Creating a new HttpClient instance per request can exhaust the available sockets.


1 Answers

After some manual testing, the good news is that there seems to be a reliable method to separate the 2 types of errors, but the bad news is that it's not consistently the same on all platforms - at least not on Windows Phone 8. On .Net Framework 4.5 Full and on Windows Store App types of project, the following code does the trick:

        try
        {
            var response = await httpClient.PostAsync(uri, content);

            if (!response.IsSuccessStatusCode)
            {
                // handle the second type of error (404, 400, etc.)
            }
        }
        catch (HttpRequestException ex)
        {
            // handle the first type of error (no connectivity, etc)
        }

On Windows Phone 8, unfortunately, both types are handled under the if condition: IsSuccessStatusCode is set to false and no HttpRequestException (or any other exception, for that matter) is raised. For WP8 I'm using the portable class library implementation of HttpClient (http://blogs.msdn.com/b/bclteam/archive/2013/02/18/portable-httpclient-for-net-framework-and-windows-phone.aspx), I asked them if this inconsistency can be fixed, hopefully it can and will soon be.

like image 115
Gabriel S. Avatar answered Oct 04 '22 23:10

Gabriel S.