When you make an HTTP request using the HttpClient
class in .NET and the server returns an error response (e.g. a 404 or a 500 status code), the HttpClient
will throw an HttpRequestException
. By default, this exception does not include the response body.
To get the response body for a failed request, you can catch the HttpRequestException
and then use the HttpResponseMessage
property of the exception to access the response. The HttpResponseMessage
class has a Content
property that represents the response body as an HttpContent
object.
Here's an example of how to get the response body for a failed request:
try { HttpResponseMessage response = await httpClient.GetAsync("https://example.com/path"); response.EnsureSuccessStatusCode(); } catch (HttpRequestException ex) { string responseBody = await ex.Response.Content.ReadAsStringAsync(); Console.WriteLine(responseBody); }
In this example, we're making an HTTP GET request to https://example.com/path
. If the server returns an error response, a HttpRequestException
will be thrown. We catch this exception and then use the Response
property to get the HttpResponseMessage
. We then use the ReadAsStringAsync
method of the HttpContent
object to get the response body as a string.
Note that the ReadAsStringAsync
method is an asynchronous method, so you need to use the await
keyword to wait for the method to complete before accessing the response body. Also note that you should only read the response body if the server returns an error response. If the request succeeds, the response body may be large and unnecessary to read.
"C# HttpRequestException get response body on failure"
HttpRequestException
.// Example Code: try { // Make HTTP request } catch (HttpRequestException ex) { if (ex.Response != null) { string responseBody = await ex.Response.Content.ReadAsStringAsync(); Console.WriteLine($"Response Body on Failure: {responseBody}"); } }
"HttpClient get response body on error"
HttpResponseMessage
to get the response body on a failed request using HttpClient
.// Example Code: HttpResponseMessage response = null; try { response = await httpClient.GetAsync("https://example.com/api/resource"); response.EnsureSuccessStatusCode(); } catch (HttpRequestException ex) { if (ex.Response != null) { string responseBody = await ex.Response.Content.ReadAsStringAsync(); Console.WriteLine($"Response Body on Failure: {responseBody}"); } }
"C# HttpClient handle unsuccessful response body"
HttpResponseMessage
and HttpRequestException
.// Example Code: HttpResponseMessage response = await httpClient.GetAsync("https://example.com/api/resource"); if (!response.IsSuccessStatusCode) { string responseBody = await response.Content.ReadAsStringAsync(); Console.WriteLine($"Response Body on Failure: {responseBody}"); }
"C# HttpClient send request and get response body on failure"
HttpClient
.// Example Code: HttpResponseMessage response = await httpClient.SendAsync(request); if (!response.IsSuccessStatusCode) { string responseBody = await response.Content.ReadAsStringAsync(); Console.WriteLine($"Response Body on Failure: {responseBody}"); }
"HttpClient retrieve response body from HttpResponseMessage"
HttpResponseMessage
in various scenarios.// Example Code: HttpResponseMessage response = await httpClient.GetAsync("https://example.com/api/resource"); if (!response.IsSuccessStatusCode) { string responseBody = await response.Content.ReadAsStringAsync(); Console.WriteLine($"Response Body on Failure: {responseBody}"); }
"C# HttpClient handle specific HTTP status code and get response body"
HttpClient
.// Example Code: HttpResponseMessage response = await httpClient.GetAsync("https://example.com/api/resource"); if (response.StatusCode == HttpStatusCode.NotFound) { string responseBody = await response.Content.ReadAsStringAsync(); Console.WriteLine($"Response Body on 404: {responseBody}"); }
"C# HttpClient exception handling and response body"
HttpRequestException
and extract the response body.// Example Code: try { // Make HTTP request } catch (HttpRequestException ex) { if (ex.Response != null) { string responseBody = await ex.Response.Content.ReadAsStringAsync(); Console.WriteLine($"Response Body on Failure: {responseBody}"); } }
"HttpClient check if response has content on failure"
HttpClient
.// Example Code: HttpResponseMessage response = await httpClient.GetAsync("https://example.com/api/resource"); if (!response.IsSuccessStatusCode && response.Content != null) { string responseBody = await response.Content.ReadAsStringAsync(); Console.WriteLine($"Response Body on Failure: {responseBody}"); }
"C# HttpClient get response body for specific status codes"
HttpClient
.// Example Code: HttpResponseMessage response = await httpClient.GetAsync("https://example.com/api/resource"); if (response.StatusCode == HttpStatusCode.InternalServerError) { string responseBody = await response.Content.ReadAsStringAsync(); Console.WriteLine($"Response Body on 500: {responseBody}"); }
"C# HttpClient handle timeout and get response body"
HttpClient
.// Example Code: try { // Make HTTP request } catch (TaskCanceledException ex) when (ex.InnerException is TimeoutException) { HttpResponseMessage response = (HttpResponseMessage)ex.InnerException.GetType().GetProperty("Response").GetValue(ex.InnerException); if (response != null) { string responseBody = await response.Content.ReadAsStringAsync(); Console.WriteLine($"Response Body on Timeout: {responseBody}"); } }
interop django-rest-auth ntp lucene instagram custom-pages lambda startup plesk url-validation