HttpRequestException
Namespace: System.Net.Http
Assembly: System.Net.Http.dll
Assembly: System.Net.Http.dll
Represents errors that occur during an HTTP request.
Inheritance
ObjectExceptionHttpRequestException
Syntax
public class HttpRequestException : Exception
Constructors
-
public HttpRequestException() - Initializes a new instance of the
HttpRequestExceptionclass. -
public HttpRequestException(string? message) - Initializes a new instance of the
HttpRequestExceptionclass with a specified error message. -
public HttpRequestException(string? message, Exception? innerException) - Initializes a new instance of the
HttpRequestExceptionclass with a specified error message and a reference to the inner exception that is the cause of the current exception.
Properties
| Name | Description |
|---|---|
StatusCode |
Gets the HTTP status code of the response, if available. Returns null if the status code is not available. |
Data |
Gets a collection of key/value pairs that provide additional user-defined information about the exception. (Inherited from Exception) |
HResult |
Gets or sets HRESULT, which is a coded numerical value that is assigned to a specific exception. (Inherited from Exception) |
InnerException |
Gets a reference to the innermost exception that caused the current exception and retains a reference to the last exception thrown by the value instantiation, if any. (Inherited from Exception) |
Message |
Gets a message that describes the current exception. (Inherited from Exception) |
Source |
Gets or sets the name of the application or the object that causes the error. (Inherited from Exception) |
StackTrace |
Gets a string representation of the immediate frames of the call stack. (Inherited from Exception) |
TargetSite |
Gets the method that throws the current exception. (Inherited from Exception) |
Remarks
HttpRequestException is thrown by HttpClient methods when an error occurs during an HTTP request. This can include network errors, DNS resolution failures, or invalid HTTP responses.
The StatusCode property can be checked to determine if the exception was raised due to an HTTP status code that indicates an error (e.g., 4xx or 5xx status codes).
It is recommended to catch HttpRequestException to gracefully handle potential network or HTTP-related issues in your applications.
Exceptions
| Exception Type | Condition |
|---|---|
System.OperationCanceledException |
This exception is thrown if the request is canceled, for example, by using a CancellationToken. |
System.Net.Http.HttpRequestException |
Thrown when the HTTP request fails for various reasons, including network issues or server errors. |
Examples
C#
VB.NET
using System;
using System.Net.Http;
using System.Threading.Tasks;
public class Example
{
public static async Task MakeRequestAsync()
{
using (HttpClient client = new HttpClient())
{
try
{
HttpResponseMessage response = await client.GetAsync("http://example.com/nonexistent");
response.EnsureSuccessStatusCode(); // Throws if status code is 4xx or 5xx
Console.WriteLine("Request succeeded!");
}
catch (HttpRequestException e)
{
Console.WriteLine("HttpRequestException Caught.");
Console.WriteLine("Message :" + e.Message);
if (e.StatusCode.HasValue)
{
Console.WriteLine("Status code: " + e.StatusCode);
}
}
catch (OperationCanceledException e)
{
Console.WriteLine("OperationCanceledException Caught.");
Console.WriteLine("Message :" + e.Message);
}
}
}
}
Imports System
Imports System.Net.Http
Imports System.Threading.Tasks
Public Class Example
Public Shared Async Function MakeRequestAsync() As Task
Using client As New HttpClient()
Try
Dim response As HttpResponseMessage = Await client.GetAsync("http://example.com/nonexistent")
response.EnsureSuccessStatusCode() ' Throws if status code is 4xx or 5xx
Console.WriteLine("Request succeeded!")
Catch e As HttpRequestException
Console.WriteLine("HttpRequestException Caught.")
Console.WriteLine("Message :" & e.Message)
If e.StatusCode.HasValue Then
Console.WriteLine("Status code: " & e.StatusCode)
End If
Catch e As OperationCanceledException
Console.WriteLine("OperationCanceledException Caught.")
Console.WriteLine("Message :" & e.Message)
End Try
End Using
End Function
End Class