HttpClient.GetByteArrayAsync Method
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Send a GET request to the specified URI and return the response body as a byte array in an asynchronous operation.
Overloads
GetByteArrayAsync(String) |
Sends a GET request to the specified URI and return the response body as a byte array in an asynchronous operation. |
GetByteArrayAsync(Uri) |
Send a GET request to the specified URI and return the response body as a byte array in an asynchronous operation. |
GetByteArrayAsync(String, CancellationToken) |
Sends a GET request to the specified URI and return the response body as a byte array in an asynchronous operation. |
GetByteArrayAsync(Uri, CancellationToken) |
Send a GET request to the specified URI and return the response body as a byte array in an asynchronous operation. |
Remarks
The operation will not block.
GetByteArrayAsync(String)
- Source:
- HttpClient.cs
- Source:
- HttpClient.cs
- Source:
- HttpClient.cs
Sends a GET request to the specified URI and return the response body as a byte array in an asynchronous operation.
public:
System::Threading::Tasks::Task<cli::array <System::Byte> ^> ^ GetByteArrayAsync(System::String ^ requestUri);
public System.Threading.Tasks.Task<byte[]> GetByteArrayAsync(string requestUri);
public System.Threading.Tasks.Task<byte[]> GetByteArrayAsync(string? requestUri);
member this.GetByteArrayAsync : string -> System.Threading.Tasks.Task<byte[]>
Public Function GetByteArrayAsync (requestUri As String) As Task(Of Byte())
Parameters
- requestUri
- String
The URI the request is sent to.
Returns
The task object representing the asynchronous operation.
Exceptions
The requestUri
is not an absolute URI and BaseAddress isn't set.
The request failed due to an issue getting a valid HTTP response, such as network connectivity failure, DNS failure, server certificate validation error, or invalid server response. On .NET 8 and later versions, the reason is indicated by HttpRequestError
-or-
The response status code was outside of the range of 200-299 (which indicate success according to the standard).
-or-
.NET Framework only: The request timed out.
.NET Core and .NET 5 and later only: The request failed due to timeout.
The provided request URI is not valid relative or absolute URI.
Remarks
This operation doesn't block. The returned Task<TResult> object will complete after the whole response body is read.
Internally, this operation uses HttpResponseMessage.EnsureSuccessStatusCode to ensure the response status code is in the success range (200-299) and throws an HttpRequestException if it is not.
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.
Note
In case of timeout, different exceptions are thrown on different .NET implementations.
- HttpRequestException is thrown on all applicable .NET Framework versions.
- OperationCanceledException without any inner exception is thrown on all applicable .NET Core versions.
- OperationCanceledException that nests a TimeoutException is thrown on .NET 5 and later versions.
Applies to
GetByteArrayAsync(Uri)
- Source:
- HttpClient.cs
- Source:
- HttpClient.cs
- Source:
- HttpClient.cs
Send a GET request to the specified URI and return the response body as a byte array in an asynchronous operation.
public:
System::Threading::Tasks::Task<cli::array <System::Byte> ^> ^ GetByteArrayAsync(Uri ^ requestUri);
public System.Threading.Tasks.Task<byte[]> GetByteArrayAsync(Uri requestUri);
public System.Threading.Tasks.Task<byte[]> GetByteArrayAsync(Uri? requestUri);
member this.GetByteArrayAsync : Uri -> System.Threading.Tasks.Task<byte[]>
Public Function GetByteArrayAsync (requestUri As Uri) As Task(Of Byte())
Parameters
- requestUri
- Uri
The URI the request is sent to.
Returns
The task object representing the asynchronous operation.
Exceptions
The requestUri
is not an absolute URI and BaseAddress isn't set.
The request failed due to an issue getting a valid HTTP response, such as network connectivity failure, DNS failure, server certificate validation error, or invalid server response. On .NET 8 and later versions, the reason is indicated by HttpRequestError.
-or-
The response status code was outside of the range of 200-299 (which indicate success according to the standard).
-or-
.NET Framework only: The request timed out.
.NET Core and .NET 5 and later only: The request failed due to timeout.
Remarks
This operation doesn't block. The returned Task<TResult> object will complete after the whole response body is read.
Internally, this operation uses HttpResponseMessage.EnsureSuccessStatusCode to ensure the response status code is in the success range (200-299) and throws an HttpRequestException if it is not.
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.
Note
In case of a timeout:
- .NET Framework throws a HttpRequestException.
- .NET Core throws a OperationCanceledException without any inner exception.
- .NET 5 and later versions throw a OperationCanceledException that nests a TimeoutException.
Applies to
GetByteArrayAsync(String, CancellationToken)
- Source:
- HttpClient.cs
- Source:
- HttpClient.cs
- Source:
- HttpClient.cs
Sends a GET request to the specified URI and return the response body as a byte array in an asynchronous operation.
public:
System::Threading::Tasks::Task<cli::array <System::Byte> ^> ^ GetByteArrayAsync(System::String ^ requestUri, System::Threading::CancellationToken cancellationToken);
public System.Threading.Tasks.Task<byte[]> GetByteArrayAsync(string? requestUri, System.Threading.CancellationToken cancellationToken);
member this.GetByteArrayAsync : string * System.Threading.CancellationToken -> System.Threading.Tasks.Task<byte[]>
Public Function GetByteArrayAsync (requestUri As String, cancellationToken As CancellationToken) As Task(Of Byte())
Parameters
- requestUri
- String
The URI the request is sent to.
- cancellationToken
- CancellationToken
The cancellation token to cancel the operation.
Returns
The task object representing the asynchronous operation.
Exceptions
The request failed due to an issue getting a valid HTTP response, such as network connectivity failure, DNS failure, server certificate validation error, or invalid server response. On .NET 8 and later versions, the reason is indicated by HttpRequestError
-or-
The response status code was outside of the range of 200-299 (which indicate success according to the standard).
The provided request URI is not valid relative or absolute URI.
The cancellation token was canceled. This exception is stored into the returned task.
-or-
.NET Core and .NET 5 and later only: The request failed due to timeout.
Remarks
This operation doesn't block. The returned Task<TResult> object will complete after the whole response body is read.
Internally, this operation uses HttpResponseMessage.EnsureSuccessStatusCode to ensure the response status code is in the success range (200-299) and throws an HttpRequestException if it is not.
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.
Note
In case of a timeout:
- .NET Framework throws a HttpRequestException.
- .NET Core throws a OperationCanceledException without any inner exception.
- .NET 5 and later versions throw a OperationCanceledException that nests a TimeoutException.
Applies to
GetByteArrayAsync(Uri, CancellationToken)
- Source:
- HttpClient.cs
- Source:
- HttpClient.cs
- Source:
- HttpClient.cs
Send a GET request to the specified URI and return the response body as a byte array in an asynchronous operation.
public:
System::Threading::Tasks::Task<cli::array <System::Byte> ^> ^ GetByteArrayAsync(Uri ^ requestUri, System::Threading::CancellationToken cancellationToken);
public System.Threading.Tasks.Task<byte[]> GetByteArrayAsync(Uri? requestUri, System.Threading.CancellationToken cancellationToken);
member this.GetByteArrayAsync : Uri * System.Threading.CancellationToken -> System.Threading.Tasks.Task<byte[]>
Public Function GetByteArrayAsync (requestUri As Uri, cancellationToken As CancellationToken) As Task(Of Byte())
Parameters
- requestUri
- Uri
The URI the request is sent to.
- cancellationToken
- CancellationToken
The cancellation token to cancel the operation.
Returns
The task object representing the asynchronous operation.
Exceptions
The request failed due to an issue getting a valid HTTP response, such as network connectivity failure, DNS failure, server certificate validation error, or invalid server response. On .NET 8 and later versions, the reason is indicated by HttpRequestError
-or-
The response status code was outside of the range of 200-299 (which indicate success according to the standard).
The cancellation token was canceled. This exception is stored into the returned task.
-or-
.NET Core and .NET 5 and later only: The request failed due to timeout.
Remarks
This operation doesn't block. The returned Task<TResult> object will complete after the whole response body is read.
Internally, this operation uses HttpResponseMessage.EnsureSuccessStatusCode to ensure the response status code is in the success range (200-299) and throws an HttpRequestException if it is not.
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.
Note
In case of a timeout:
- .NET Framework throws a HttpRequestException.
- .NET Core throws a OperationCanceledException without any inner exception.
- .NET 5 and later versions throw a OperationCanceledException that nests a TimeoutException.