HttpClient 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
提供類別,用於從 URI 所識別的資源傳送 HTTP 要求和接收 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 備註。
建構函式
HttpClient() |
使用當處置此執行個體時會處置的 HttpClientHandler 來初始化 HttpClient 類別的新執行個體。 |
HttpClient(HttpMessageHandler) |
使用指定的處理常式初始化 HttpClient 類別的新執行個體。 當處置此執行個體時會處置該處理常式。 |
HttpClient(HttpMessageHandler, Boolean) |
使用提供的處理常式初始化 HttpClient 類別的新執行個體,並指定當處置此執行個體時是否應該處置該處理常式。 |
屬性
BaseAddress |
取得或設定傳送要求時所使用之網際網路資源的統一資源識別元 (URI) 基底位址。 |
DefaultProxy |
取得或設定全域 HTTP Proxy。 |
DefaultRequestHeaders |
取得應該在每個要求中傳送的標頭。 |
DefaultRequestVersion |
取得或設定用於這個 HttpClient 執行個體所提出後續要求的預設 HTTP 版本。 |
DefaultVersionPolicy |
取得或設定透過便利方法以隱含方式所建立要求的預設版本原則,例如 GetAsync(String) 和 PostAsync(String, HttpContent)。 |
MaxResponseContentBufferSize |
取得或設定讀取回應內容時要緩衝處理的位元組數目上限。 |
Timeout |
取得或設定要求逾時前等候的時間長度。 |