HttpClient.PostAsync 方法

定義

以非同步作業的方式,將 POST 要求傳送至指定的 URI。Send a POST request to the specified Uri as an asynchronous operation.

多載

PostAsync(String, HttpContent, CancellationToken)

以非同步作業的方式,傳送 POST 要求和取消語彙基元。Send a POST request with a cancellation token as an asynchronous operation.

PostAsync(Uri, HttpContent, CancellationToken)

以非同步作業的方式,傳送 POST 要求和取消語彙基元。Send a POST request with a cancellation token as an asynchronous operation.

PostAsync(String, HttpContent)

以非同步作業的方式,將 POST 要求傳送至指定的 URI。Send a POST request to the specified Uri as an asynchronous operation.

PostAsync(Uri, HttpContent)

以非同步作業的方式,將 POST 要求傳送至指定的 URI。Send a POST request to the specified Uri as an asynchronous operation.

備註

這項作業不會封鎖。This operation will not block.

PostAsync(String, HttpContent, CancellationToken)

以非同步作業的方式,傳送 POST 要求和取消語彙基元。Send a POST request with a cancellation token as an asynchronous operation.

public:
 System::Threading::Tasks::Task<System::Net::Http::HttpResponseMessage ^> ^ PostAsync(System::String ^ requestUri, System::Net::Http::HttpContent ^ content, System::Threading::CancellationToken cancellationToken);
public System.Threading.Tasks.Task<System.Net.Http.HttpResponseMessage> PostAsync (string requestUri, System.Net.Http.HttpContent content, System.Threading.CancellationToken cancellationToken);
public System.Threading.Tasks.Task<System.Net.Http.HttpResponseMessage> PostAsync (string? requestUri, System.Net.Http.HttpContent content, System.Threading.CancellationToken cancellationToken);
member this.PostAsync : string * System.Net.Http.HttpContent * System.Threading.CancellationToken -> System.Threading.Tasks.Task<System.Net.Http.HttpResponseMessage>
Public Function PostAsync (requestUri As String, content As HttpContent, cancellationToken As CancellationToken) As Task(Of HttpResponseMessage)

參數

requestUri
String

傳送要求的目標 URI。The Uri the request is sent to.

content
HttpContent

傳送至伺服器的 HTTP 要求內容。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.

傳回

Task<HttpResponseMessage>

工作物件,表示非同步作業。The task object representing the asynchronous operation.

例外狀況

requestUri 必須是絕對 URI,否則必須設定 BaseAddressThe requestUri must be an absolute URI or BaseAddress must be set.

要求已因基本問題 (例如網路連線、DNS 失敗、伺服器憑證驗證或逾時) 而失敗。The request failed due to an underlying issue such as network connectivity, DNS failure, server certificate validation or timeout.

僅限 .NET Core 與 .NET 5.0 和更新版本:要求因逾時而失敗。.NET Core and .NET 5.0 and later only: The request failed due to timeout.

備註

這項作業不會封鎖。This operation will not block. 傳回的 Task<TResult> 物件會在讀取完整的回應 (包括內容) 之後完成。The returned Task<TResult> object will complete after the whole response (including content) is read.

注意

如果有超時,則會在不同的 .NET 執行上擲回不同的例外狀況。In case of timeout, different exceptions are thrown on different .NET implementations.

適用於

PostAsync(Uri, HttpContent, CancellationToken)

以非同步作業的方式,傳送 POST 要求和取消語彙基元。Send a POST request with a cancellation token as an asynchronous operation.

public:
 System::Threading::Tasks::Task<System::Net::Http::HttpResponseMessage ^> ^ PostAsync(Uri ^ requestUri, System::Net::Http::HttpContent ^ content, System::Threading::CancellationToken cancellationToken);
public System.Threading.Tasks.Task<System.Net.Http.HttpResponseMessage> PostAsync (Uri requestUri, System.Net.Http.HttpContent content, System.Threading.CancellationToken cancellationToken);
public System.Threading.Tasks.Task<System.Net.Http.HttpResponseMessage> PostAsync (Uri? requestUri, System.Net.Http.HttpContent content, System.Threading.CancellationToken cancellationToken);
member this.PostAsync : Uri * System.Net.Http.HttpContent * System.Threading.CancellationToken -> System.Threading.Tasks.Task<System.Net.Http.HttpResponseMessage>
Public Function PostAsync (requestUri As Uri, content As HttpContent, cancellationToken As CancellationToken) As Task(Of HttpResponseMessage)

參數

requestUri
Uri

傳送要求的目標 URI。The Uri the request is sent to.

content
HttpContent

傳送至伺服器的 HTTP 要求內容。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.

傳回

Task<HttpResponseMessage>

工作物件,表示非同步作業。The task object representing the asynchronous operation.

例外狀況

requestUri 必須是絕對 URI,否則必須設定 BaseAddressThe requestUri must be an absolute URI or BaseAddress must be set.

要求已因基本問題 (例如網路連線、DNS 失敗、伺服器憑證驗證或逾時) 而失敗。The request failed due to an underlying issue such as network connectivity, DNS failure, server certificate validation or timeout.

僅限 .NET Core 與 .NET 5.0 和更新版本:要求因逾時而失敗。.NET Core and .NET 5.0 and later only: The request failed due to timeout.

備註

這項作業不會封鎖。This operation will not block. 傳回的 Task<TResult> 物件會在讀取完整的回應 (包括內容) 之後完成。The returned Task<TResult> object will complete after the whole response (including content) is read.

注意

如果有超時,則會在不同的 .NET 執行上擲回不同的例外狀況。In case of timeout, different exceptions are thrown on different .NET implementations.

適用於

PostAsync(String, HttpContent)

以非同步作業的方式,將 POST 要求傳送至指定的 URI。Send a POST request to the specified Uri as an asynchronous operation.

public:
 System::Threading::Tasks::Task<System::Net::Http::HttpResponseMessage ^> ^ PostAsync(System::String ^ requestUri, System::Net::Http::HttpContent ^ content);
public System.Threading.Tasks.Task<System.Net.Http.HttpResponseMessage> PostAsync (string requestUri, System.Net.Http.HttpContent content);
public System.Threading.Tasks.Task<System.Net.Http.HttpResponseMessage> PostAsync (string? requestUri, System.Net.Http.HttpContent content);
member this.PostAsync : string * System.Net.Http.HttpContent -> System.Threading.Tasks.Task<System.Net.Http.HttpResponseMessage>
Public Function PostAsync (requestUri As String, content As HttpContent) As Task(Of HttpResponseMessage)

參數

requestUri
String

傳送要求的目標 URI。The Uri the request is sent to.

content
HttpContent

傳送至伺服器的 HTTP 要求內容。The HTTP request content sent to the server.

傳回

Task<HttpResponseMessage>

工作物件,表示非同步作業。The task object representing the asynchronous operation.

例外狀況

requestUri 必須是絕對 URI,否則必須設定 BaseAddressThe requestUri must be an absolute URI or BaseAddress must be set.

要求已因基本問題 (例如網路連線、DNS 失敗、伺服器憑證驗證或逾時) 而失敗。The request failed due to an underlying issue such as network connectivity, DNS failure, server certificate validation or timeout.

僅限 .NET Core 與 .NET 5.0 和更新版本:要求因逾時而失敗。.NET Core and .NET 5.0 and later only: The request failed due to timeout.

備註

這項作業不會封鎖。This operation will not block. 傳回的 Task<TResult> 物件會在讀取完整的回應 (包括內容) 之後完成。The returned Task<TResult> object will complete after the whole response (including content) is read.

注意

如果有超時,則會在不同的 .NET 執行上擲回不同的例外狀況。In case of timeout, different exceptions are thrown on different .NET implementations.

適用於

PostAsync(Uri, HttpContent)

以非同步作業的方式,將 POST 要求傳送至指定的 URI。Send a POST request to the specified Uri as an asynchronous operation.

public:
 System::Threading::Tasks::Task<System::Net::Http::HttpResponseMessage ^> ^ PostAsync(Uri ^ requestUri, System::Net::Http::HttpContent ^ content);
public System.Threading.Tasks.Task<System.Net.Http.HttpResponseMessage> PostAsync (Uri requestUri, System.Net.Http.HttpContent content);
public System.Threading.Tasks.Task<System.Net.Http.HttpResponseMessage> PostAsync (Uri? requestUri, System.Net.Http.HttpContent content);
member this.PostAsync : Uri * System.Net.Http.HttpContent -> System.Threading.Tasks.Task<System.Net.Http.HttpResponseMessage>
Public Function PostAsync (requestUri As Uri, content As HttpContent) As Task(Of HttpResponseMessage)

參數

requestUri
Uri

傳送要求的目標 URI。The Uri the request is sent to.

content
HttpContent

傳送至伺服器的 HTTP 要求內容。The HTTP request content sent to the server.

傳回

Task<HttpResponseMessage>

工作物件,表示非同步作業。The task object representing the asynchronous operation.

例外狀況

requestUri 必須是絕對 URI,否則必須設定 BaseAddressThe requestUri must be an absolute URI or BaseAddress must be set.

要求已因基本問題 (例如網路連線、DNS 失敗、伺服器憑證驗證或逾時) 而失敗。The request failed due to an underlying issue such as network connectivity, DNS failure, server certificate validation or timeout.

僅限 .NET Core 與 .NET 5.0 和更新版本:要求因逾時而失敗。.NET Core and .NET 5.0 and later only: The request failed due to timeout.

備註

這項作業不會封鎖。This operation will not block. 傳回的 Task<TResult> 物件會在讀取完整的回應 (包括內容) 之後完成。The returned Task<TResult> object will complete after the whole response (including content) is read.

注意

如果有超時,則會在不同的 .NET 執行上擲回不同的例外狀況。In case of timeout, different exceptions are thrown on different .NET implementations.

適用於