HttpClient.GetStringAsync 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 string in an asynchronous operation.
Overloads
| Name | Description |
|---|---|
| GetStringAsync(String) |
Send a GET request to the specified URI and return the response body as a string in an asynchronous operation. |
| GetStringAsync(Uri) |
Send a GET request to the specified URI and return the response body as a string in an asynchronous operation. |
| GetStringAsync(String, CancellationToken) |
Send a GET request to the specified URI and return the response body as a string in an asynchronous operation. |
| GetStringAsync(Uri, CancellationToken) |
Send a GET request to the specified URI and return the response body as a string in an asynchronous operation. |
Remarks
This operation doesn't block.
GetStringAsync(String)
- Source:
- HttpClient.cs
- Source:
- HttpClient.cs
- Source:
- HttpClient.cs
- Source:
- HttpClient.cs
- Source:
- HttpClient.cs
Send a GET request to the specified URI and return the response body as a string in an asynchronous operation.
public:
System::Threading::Tasks::Task ^ GetStringAsync(System::String ^ requestUri);
public System.Threading.Tasks.Task GetStringAsync(string requestUri);
public System.Threading.Tasks.Task GetStringAsync(string? requestUri);
member this.GetStringAsync : string -> System.Threading.Tasks.Task
Public Function GetStringAsync (requestUri As String) As Task(Of String)
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 Task200 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
GetStringAsync(Uri)
- Source:
- HttpClient.cs
- Source:
- HttpClient.cs
- Source:
- HttpClient.cs
- Source:
- HttpClient.cs
- Source:
- HttpClient.cs
Send a GET request to the specified URI and return the response body as a string in an asynchronous operation.
public:
System::Threading::Tasks::Task ^ GetStringAsync(Uri ^ requestUri);
public System.Threading.Tasks.Task GetStringAsync(Uri requestUri);
public System.Threading.Tasks.Task GetStringAsync(Uri? requestUri);
member this.GetStringAsync : Uri -> System.Threading.Tasks.Task
Public Function GetStringAsync (requestUri As Uri) As Task(Of String)
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 Task200 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
GetStringAsync(String, CancellationToken)
- Source:
- HttpClient.cs
- Source:
- HttpClient.cs
- Source:
- HttpClient.cs
- Source:
- HttpClient.cs
- Source:
- HttpClient.cs
Send a GET request to the specified URI and return the response body as a string in an asynchronous operation.
public:
System::Threading::Tasks::Task ^ GetStringAsync(System::String ^ requestUri, System::Threading::CancellationToken cancellationToken);
public System.Threading.Tasks.Task GetStringAsync(string? requestUri, System.Threading.CancellationToken cancellationToken);
member this.GetStringAsync : string * System.Threading.CancellationToken -> System.Threading.Tasks.Task
Public Function GetStringAsync (requestUri As String, cancellationToken As CancellationToken) As Task(Of String)
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 requestUri is null.
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 Task200 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
GetStringAsync(Uri, CancellationToken)
- Source:
- HttpClient.cs
- Source:
- HttpClient.cs
- Source:
- HttpClient.cs
- Source:
- HttpClient.cs
- Source:
- HttpClient.cs
Send a GET request to the specified URI and return the response body as a string in an asynchronous operation.
public:
System::Threading::Tasks::Task ^ GetStringAsync(Uri ^ requestUri, System::Threading::CancellationToken cancellationToken);
public System.Threading.Tasks.Task GetStringAsync(Uri? requestUri, System.Threading.CancellationToken cancellationToken);
member this.GetStringAsync : Uri * System.Threading.CancellationToken -> System.Threading.Tasks.Task
Public Function GetStringAsync (requestUri As Uri, cancellationToken As CancellationToken) As Task(Of String)
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 requestUri is null.
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 Task200 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.