HttpClient.PostAsync Method

Definition

Send a POST request to the specified URI as an asynchronous operation.

Overloads

PostAsync(String, HttpContent, CancellationToken)

Send a POST request with a cancellation token as an asynchronous operation.

PostAsync(Uri, HttpContent, CancellationToken)

Send a POST request with a cancellation token as an asynchronous operation.

PostAsync(String, HttpContent)

Send a POST request to the specified URI as an asynchronous operation.

PostAsync(Uri, HttpContent)

Send a POST request to the specified URI as an asynchronous operation.

Remarks

This operation doesn't block.

PostAsync(String, HttpContent, CancellationToken)

Source:
HttpClient.cs
Source:
HttpClient.cs
Source:
HttpClient.cs

Send a POST request with a cancellation token as an asynchronous operation.

C#
public System.Threading.Tasks.Task<System.Net.Http.HttpResponseMessage> PostAsync(string requestUri, System.Net.Http.HttpContent content, System.Threading.CancellationToken cancellationToken);
C#
public System.Threading.Tasks.Task<System.Net.Http.HttpResponseMessage> PostAsync(string? requestUri, System.Net.Http.HttpContent? content, System.Threading.CancellationToken cancellationToken);
C#
public System.Threading.Tasks.Task<System.Net.Http.HttpResponseMessage> PostAsync(string? requestUri, System.Net.Http.HttpContent content, System.Threading.CancellationToken cancellationToken);

Parameters

requestUri
String

The URI the request is sent to.

content
HttpContent

The HTTP request content sent to the server.

cancellationToken
CancellationToken

A cancellation token that can be used by other objects or threads to receive notice of cancellation.

Returns

The task object representing the asynchronous operation.

Exceptions

The requestUri must be an absolute URI or BaseAddress must be set.

The request failed due to an underlying issue such as network connectivity, DNS failure, server certificate validation or timeout.

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 (including content) is read.

Märkus

In case of timeout, different exceptions are thrown on different .NET implementations.

Applies to

.NET 10 ja muud versioonid
Toode Versioonid
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

PostAsync(Uri, HttpContent, CancellationToken)

Source:
HttpClient.cs
Source:
HttpClient.cs
Source:
HttpClient.cs

Send a POST request with a cancellation token as an asynchronous operation.

C#
public System.Threading.Tasks.Task<System.Net.Http.HttpResponseMessage> PostAsync(Uri requestUri, System.Net.Http.HttpContent content, System.Threading.CancellationToken cancellationToken);
C#
public System.Threading.Tasks.Task<System.Net.Http.HttpResponseMessage> PostAsync(Uri? requestUri, System.Net.Http.HttpContent? content, System.Threading.CancellationToken cancellationToken);
C#
public System.Threading.Tasks.Task<System.Net.Http.HttpResponseMessage> PostAsync(Uri? requestUri, System.Net.Http.HttpContent content, System.Threading.CancellationToken cancellationToken);

Parameters

requestUri
Uri

The URI the request is sent to.

content
HttpContent

The HTTP request content sent to the server.

cancellationToken
CancellationToken

A cancellation token that can be used by other objects or threads to receive notice of cancellation.

Returns

The task object representing the asynchronous operation.

Exceptions

The requestUri must be an absolute URI or BaseAddress must be set.

The request failed due to an underlying issue such as network connectivity, DNS failure, server certificate validation or timeout.

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 (including content) is read.

Märkus

In case of timeout, different exceptions are thrown on different .NET implementations.

Applies to

.NET 10 ja muud versioonid
Toode Versioonid
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

PostAsync(String, HttpContent)

Source:
HttpClient.cs
Source:
HttpClient.cs
Source:
HttpClient.cs

Send a POST request to the specified URI as an asynchronous operation.

C#
public System.Threading.Tasks.Task<System.Net.Http.HttpResponseMessage> PostAsync(string requestUri, System.Net.Http.HttpContent content);
C#
public System.Threading.Tasks.Task<System.Net.Http.HttpResponseMessage> PostAsync(string? requestUri, System.Net.Http.HttpContent? content);
C#
public System.Threading.Tasks.Task<System.Net.Http.HttpResponseMessage> PostAsync(string? requestUri, System.Net.Http.HttpContent content);

Parameters

requestUri
String

The URI the request is sent to.

content
HttpContent

The HTTP request content sent to the server.

Returns

The task object representing the asynchronous operation.

Exceptions

The requestUri must be an absolute URI or BaseAddress must be set.

The request failed due to an underlying issue such as network connectivity, DNS failure, server certificate validation or timeout.

.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 (including content) is read.

Märkus

In case of timeout, different exceptions are thrown on different .NET implementations.

Applies to

.NET 10 ja muud versioonid
Toode Versioonid
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

PostAsync(Uri, HttpContent)

Source:
HttpClient.cs
Source:
HttpClient.cs
Source:
HttpClient.cs

Send a POST request to the specified URI as an asynchronous operation.

C#
public System.Threading.Tasks.Task<System.Net.Http.HttpResponseMessage> PostAsync(Uri requestUri, System.Net.Http.HttpContent content);
C#
public System.Threading.Tasks.Task<System.Net.Http.HttpResponseMessage> PostAsync(Uri? requestUri, System.Net.Http.HttpContent? content);
C#
public System.Threading.Tasks.Task<System.Net.Http.HttpResponseMessage> PostAsync(Uri? requestUri, System.Net.Http.HttpContent content);

Parameters

requestUri
Uri

The URI the request is sent to.

content
HttpContent

The HTTP request content sent to the server.

Returns

The task object representing the asynchronous operation.

Exceptions

The requestUri must be an absolute URI or BaseAddress must be set.

The request failed due to an underlying issue such as network connectivity, DNS failure, server certificate validation or timeout.

.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 (including content) is read.

Märkus

In case of timeout, different exceptions are thrown on different .NET implementations.

Applies to

.NET 10 ja muud versioonid
Toode Versioonid
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0