Class HttpClientExtensions
Provides extension methods for the HttpClient class.
Inherited Members
Namespace: mk.helpers
Assembly: mk.helpers.dll
Syntax
public static class HttpClientExtensions
Methods
DownloadDataAsync(HttpClient, string, Stream, Action<long>, CancellationToken)
Asynchronously downloads data from the specified URL and writes it to the provided destination stream.
Declaration
public static Task DownloadDataAsync(this HttpClient client, string requestUrl, Stream destination, Action<long> downloadProgress = null, CancellationToken cancellationToken = default)
Parameters
| Type | Name | Description |
|---|---|---|
| HttpClient | client | The HttpClient instance. |
| string | requestUrl | The URL to download from. |
| Stream | destination | The stream to write the downloaded data to. |
| Action<long> | downloadProgress | Optional action to report download progress. |
| CancellationToken | cancellationToken | Optional cancellation token. |
Returns
| Type | Description |
|---|---|
| Task |
GetFileSizeAsync(HttpClient, string)
Retrieves the file size of a resource using a HEAD request with HttpClient.
Declaration
public static Task<long?> GetFileSizeAsync(this HttpClient httpClient, string url)
Parameters
| Type | Name | Description |
|---|---|---|
| HttpClient | httpClient | The HttpClient instance. |
| string | url | The URL of the resource. |
Returns
| Type | Description |
|---|---|
| Task<long?> | The size of the file in bytes. |
GetHeadersAsync(HttpClient, string)
Retrieves all headers of a resource using a HEAD request with HttpClient.
Declaration
public static Task<Dictionary<string, string>> GetHeadersAsync(this HttpClient httpClient, string url)
Parameters
| Type | Name | Description |
|---|---|---|
| HttpClient | httpClient | The HttpClient instance. |
| string | url | The URL of the resource. |
Returns
| Type | Description |
|---|---|
| Task<Dictionary<string, string>> | A dictionary containing the header key/value pairs. |
PostAsJsonAsync<T>(HttpClient, string, T)
Sends an HTTP POST request with JSON data as the content.
Declaration
public static Task<HttpResponseMessage> PostAsJsonAsync<T>(this HttpClient httpClient, string url, T data)
Parameters
| Type | Name | Description |
|---|---|---|
| HttpClient | httpClient | The HttpClient instance. |
| string | url | The URL to send the request to. |
| T | data | The data to be serialized and sent as JSON. |
Returns
| Type | Description |
|---|---|
| Task<HttpResponseMessage> | A task representing the asynchronous operation. |
Type Parameters
| Name | Description |
|---|---|
| T | The type of data to be serialized as JSON. |
PutAsJsonAsync<T>(HttpClient, string, T)
Sends an HTTP PUT request with JSON data as the content.
Declaration
public static Task<HttpResponseMessage> PutAsJsonAsync<T>(this HttpClient httpClient, string url, T data)
Parameters
| Type | Name | Description |
|---|---|---|
| HttpClient | httpClient | The HttpClient instance. |
| string | url | The URL to send the request to. |
| T | data | The data to be serialized and sent as JSON. |
Returns
| Type | Description |
|---|---|
| Task<HttpResponseMessage> | A task representing the asynchronous operation. |
Type Parameters
| Name | Description |
|---|---|
| T | The type of data to be serialized as JSON. |
ReadAsJsonAsync<T>(HttpContent)
Asynchronously reads the HTTP content as JSON and deserializes it to the specified type.
Declaration
public static Task<T> ReadAsJsonAsync<T>(this HttpContent content)
Parameters
| Type | Name | Description |
|---|---|---|
| HttpContent | content | The HttpContent to read from. |
Returns
| Type | Description |
|---|---|
| Task<T> | A task representing the asynchronous operation and containing the deserialized object. |
Type Parameters
| Name | Description |
|---|---|
| T | The type to deserialize the JSON content to. |