HttpResponse Class
Namespace: System.Net.Http
Represents a Hypertext Transfer Protocol (HTTP) response message received from an HTTP server.
Syntax
public sealed class HttpResponse : IDisposable
Remarks
The HttpResponse class provides a convenient way to access the properties of an HTTP response, such as the status code, headers, and content. It is typically obtained as the result of an HTTP request made using classes like HttpClient.
The HttpResponse object implements IDisposable, and it's important to dispose of it when you are finished with it to release any unmanaged resources it may hold.
Constructors
There are no public constructors for the HttpResponse class. Instances are created by the HttpClient class.
Properties
-
Contentpublic virtual HttpContent? Content { get; protected set; }Gets the HTTP content of the response.
Type:
HttpContent?Access: public, virtual, protected set
-
Headerspublic HttpResponseHeaders Headers { get; }Gets the collection of HTTP response headers.
Type:
HttpResponseHeadersAccess: public
-
RequestMessagepublic HttpRequestMessage? RequestMessage { get; set; }Gets or sets the HTTP request message that led to this response message.
Type:
HttpRequestMessage?Access: public
-
StatusCodepublic HttpStatusCode StatusCode { get; set; }Gets or sets the status code of the HTTP response.
Type:
HttpStatusCodeAccess: public
-
Versionpublic Version Version { get; set; }Gets or sets the HTTP message version.
Type:
VersionAccess: public
Methods
-
Disposepublic void Dispose()Releases the unmanaged resources used by the
HttpResponseand optionally releases the managed resources.Return Type:
voidAccess: public
-
Disposeprotected virtual void Dispose(bool disposing)Releases the unmanaged resources used by the
HttpResponseand optionally releases the managed resources.Parameters:
disposing:bool.trueto release both managed and unmanaged resources;falseto release only unmanaged resources.
Return Type:
voidAccess: protected, virtual
-
EnsureSuccessStatusCodepublic void EnsureSuccessStatusCode()Throws an exception if the HTTP response status code indicates an error.
Return Type:
voidAccess: public
Throws:
HttpRequestExceptionif the status code is an error code.
Example
using System;
using System.Net.Http;
using System.Threading.Tasks;
public class Example
{
public static async Task GetHttpResponseAsync(string url)
{
using (HttpClient client = new HttpClient())
{
try
{
HttpResponseMessage response = await client.GetAsync(url);
response.EnsureSuccessStatusCode(); // Throws if status code is not 2xx
Console.WriteLine($"Status Code: {response.StatusCode}");
Console.WriteLine($"Content-Type: {response.Content.Headers.ContentType?.MediaType}");
string responseBody = await response.Content.ReadAsStringAsync();
Console.WriteLine($"Response Body Length: {responseBody.Length}");
}
catch (HttpRequestException e)
{
Console.WriteLine($"Request error: {e.Message}");
}
}
}
public static async Task Main(string[] args)
{
await GetHttpResponseAsync("https://www.example.com");
}
}