Edit

Share via


HttpResponseMessage.EnsureSuccessStatusCode Method

Definition

Throws an exception if the IsSuccessStatusCode property for the HTTP response is false.

public:
 System::Net::Http::HttpResponseMessage ^ EnsureSuccessStatusCode();
public System.Net.Http.HttpResponseMessage EnsureSuccessStatusCode();
member this.EnsureSuccessStatusCode : unit -> System.Net.Http.HttpResponseMessage
Public Function EnsureSuccessStatusCode () As HttpResponseMessage

Returns

The HTTP response message if the call is successful.

Exceptions

The HTTP response is unsuccessful.

Remarks

The EnsureSuccessStatusCode method throws an HttpRequestException if StatusCode is outside of the range 200-299 (the range of status codes indicating success according to the standard). In some scenarios, you might need more control over which status codes are considered to be successful; for example, an API might be expected to always return 200 OK. In such cases, we recommend to manually check if StatusCode matches the expected value. In .NET Framework and .NET Core 2.2 and earlier versions, if the Content is not null, this method also calls Dispose to free managed and unmanaged resources. Starting with .NET Core 3.0, the content is not disposed.

Applies to