HttpClient 類別
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
提供類別,用於從 URI 所識別的資源傳送 HTTP 要求和接收 HTTP 回應。
public ref class HttpClient : System::Net::Http::HttpMessageInvoker
C#
public class HttpClient : System.Net.Http.HttpMessageInvoker
type HttpClient = class
inherit HttpMessageInvoker
Public Class HttpClient
Inherits HttpMessageInvoker
- 繼承
C#
// HttpClient is intended to be instantiated once per application, rather than per-use. See Remarks.
static readonly HttpClient client = new HttpClient();
static async Task Main()
{
// Call asynchronous network methods in a try/catch block to handle exceptions.
try
{
using HttpResponseMessage response = await client.GetAsync("http://www.contoso.com/");
response.EnsureSuccessStatusCode();
string responseBody = await response.Content.ReadAsStringAsync();
// Above three lines can be replaced with new helper method below
// string responseBody = await client.GetStringAsync(uri);
Console.WriteLine(responseBody);
}
catch (HttpRequestException e)
{
Console.WriteLine("\nException Caught!");
Console.WriteLine("Message :{0} ", e.Message);
}
}
open System.Net.Http
// HttpClient is intended to be instantiated once per application, rather than per-use. See Remarks.
let client = new HttpClient()
let main =
task {
// Call asynchronous network methods in a try/catch block to handle exceptions.
try
use! response = client.GetAsync "http://www.contoso.com/"
response.EnsureSuccessStatusCode() |> ignore
let! responseBody = response.Content.ReadAsStringAsync()
// Above three lines can be replaced with new helper method below
// let! responseBody = client.GetStringAsync uri
printfn $"{responseBody}"
with
| :? HttpRequestException as e ->
printfn "\nException Caught!"
printfn $"Message :{e.Message} "
}
main.Wait()
' HttpClient is intended to be instantiated once per application, rather than per-use. See Remarks.
Shared ReadOnly client As HttpClient = New HttpClient()
Private Shared Async Function Main() As Task
' Call asynchronous network methods in a try/catch block to handle exceptions.
Try
Using response As HttpResponseMessage = Await client.GetAsync("http://www.contoso.com/")
response.EnsureSuccessStatusCode()
Dim responseBody As String = Await response.Content.ReadAsStringAsync()
' Above three lines can be replaced with new helper method below
' Dim responseBody As String = Await client.GetStringAsync(uri)
Console.WriteLine(responseBody)
End Using
Catch e As HttpRequestException
Console.WriteLine(Environment.NewLine & "Exception Caught!")
Console.WriteLine("Message :{0} ", e.Message)
End Try
End Function
如需此 API 的詳細資訊,請參閱 HttpClient 的補充 API 備註。
Http |
使用當處置此執行個體時會處置的 HttpClientHandler 來初始化 HttpClient 類別的新執行個體。 |
Http |
使用指定的處理常式初始化 HttpClient 類別的新執行個體。 當處置此執行個體時會處置該處理常式。 |
Http |
使用提供的處理常式初始化 HttpClient 類別的新執行個體,並指定當處置此執行個體時是否應該處置該處理常式。 |
Base |
取得或設定傳送要求時所使用之網際網路資源的統一資源識別元 (URI) 基底位址。 |
Default |
取得或設定全域 HTTP Proxy。 |
Default |
取得應該在每個要求中傳送的標頭。 |
Default |
取得或設定用於這個 HttpClient 執行個體所提出後續要求的預設 HTTP 版本。 |
Default |
取得或設定透過便利方法以隱含方式所建立要求的預設版本原則,例如 GetAsync(String) 和 PostAsync(String, HttpContent)。 |
Max |
取得或設定讀取回應內容時要緩衝處理的位元組數目上限。 |
Timeout |
取得或設定要求逾時前等候的時間長度。 |
產品 | 版本 |
---|---|
.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 |
.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 |