Using EnsureSuccessStatusCode and HttpRequestException Exception Handling

What is the usage pattern for HttpResponseMessage.EnsureSuccessStatusCode() ? It has the Content of the message and throws an HttpRequestException , but I don’t see how to programmatically handle it other than a general Exception . For example, it does not include HttpStatusCode , which would be convenient.

Is there a way to get more information? Can someone show an appropriate usage pattern of both EnsureSuccessStatusCode() and an HttpRequestException?

+50
Jan 13 '14 at 17:34
source share
3 answers

The EnsureSuccessStatusCode use of EnsureSuccessStatusCode is to briefly confirm the success of the request when you do not want to handle the failure cases in any particular way. This is especially useful if you want to quickly prototype a client.

When you decide that you want to handle failure cases in a specific way, do not do the following.

 var response = await client.GetAsync(...); try { response.EnsureSuccessStatusCode(); // Handle success } catch (HttpRequestException) { // Handle failure } 

This creates an exception to catch it right away, which makes no sense. For this purpose, the IsSuccessStatusCode property for the HttpResponseMessage exists. Instead, do the following.

 var response = await client.GetAsync(...); if (response.IsSuccessStatusCode) { // Handle success } else { // Handle failure } 
+65
Jan 16 '15 at 22:30
source

I do not like EnsureSuccessStatusCode as it does not return anything serious. This is why I created my own extension:

 public static class HttpResponseMessageExtensions { public static async Task EnsureSuccessStatusCodeAsync(this HttpResponseMessage response) { if (response.IsSuccessStatusCode) { return; } var content = await response.Content.ReadAsStringAsync(); if (response.Content != null) response.Content.Dispose(); throw new SimpleHttpResponseException(response.StatusCode, content); } } public class SimpleHttpResponseException : Exception { public HttpStatusCode StatusCode { get; private set; } public SimpleHttpResponseException(HttpStatusCode statusCode, string content) : base(content) { StatusCode = statusCode; } } 

source code for Microsoft EnsureSuccessStatusCode can be found here

Synchronous version based on SO Link :

 public static void EnsureSuccessStatusCode(this HttpResponseMessage response) { if (response.IsSuccessStatusCode) { return; } var content = response.Content.ReadAsStringAsync().GetAwaiter().GetResult(); if (response.Content != null) response.Content.Dispose(); throw new SimpleHttpResponseException(response.StatusCode, content); } 

What I don't like about IsSuccessStatusCode is that it is not "beautifully" reusable. For example, you can use a library, for example polly , to repeat the request in case of a network problem. In this case, you need your code to raise the exception so that a political or some other library can handle it ...

+53
Jan 16 '15 at 14:43
source

I know that this is not the best way to do this, but I use it like this:

 try { ... } catch (HttpRequestException exception) { if (exception.Message.Contains("401 (Unauthorized)")) { statusCode = HttpStatusCode.Unauthorized; } else if (exception.Message.Contains("403 (Forbidden)")) { statusCode = HttpStatusCode.Forbidden; } } 

Please let me know if you have a better solution.

Regards, BJ

+3
Feb 27 '14 at 18:28
source



All Articles