HttpClient 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
提供一個類別,用於發送 HTTP 請求及接收由 URI 識別的資源的 HTTP 回應。
public ref class HttpClient : System::Net::Http::HttpMessageInvoker
public class HttpClient : System.Net.Http.HttpMessageInvoker
type HttpClient = class
inherit HttpMessageInvoker
Public Class HttpClient
Inherits HttpMessageInvoker
- 繼承
範例
// 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 備註。
建構函式
| 名稱 | Description |
|---|---|
| HttpClient() |
使用HttpClientHandler當該實例被處置時,初始化類別的新實例HttpClient。 |
| HttpClient(HttpMessageHandler, Boolean) |
初始化該類別的新實例 HttpClient ,並指定該處理器在處理時是否應被丟棄。 |
| HttpClient(HttpMessageHandler) |
初始化一個新的類別實例 HttpClient ,使用指定的處理器。 當該實例被處理時,處理者即被處置。 |
屬性
| 名稱 | Description |
|---|---|
| BaseAddress |
取得或設定用於發送請求時所使用的網際網路資源的統一資源識別碼(URI)的基底位址。 |
| DefaultProxy |
取得或設定全域 HTTP Proxy。 |
| DefaultRequestHeaders |
它會取得每個請求都應該傳送的標頭。 |
| DefaultRequestVersion |
取得或設定此實例後續請求 HttpClient 所使用的預設 HTTP 版本。 |
| DefaultVersionPolicy |
取得或設定便利方法中隱含產生請求的預設版本政策, GetAsync(String)PostAsync(String, HttpContent)例如。 |
| MaxResponseContentBufferSize |
讀取回應內容時,會取得或設定最大緩衝位元組數。 |
| Timeout |
它會設定等待時間跨度,直到請求逾時。 |