HttpResponseMessage 類別
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
表示包含狀態碼及資料的 HTTP 回應訊息。
public ref class HttpResponseMessage : IDisposable
public class HttpResponseMessage : IDisposable
type HttpResponseMessage = class
interface IDisposable
Public Class HttpResponseMessage
Implements IDisposable
- 繼承
-
HttpResponseMessage
- 實作
// 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
上述程式代碼範例會使用 async Task Main()
進入點。 此功能需要 C# 7.1 或更新版本。
取得的常見方式 HttpResponseMessage 是來自其中 HttpClient.SendAsync(HttpRequestMessage) 一種方法。
Http |
初始化 HttpResponseMessage 類別的新執行個體。 |
Http |
使用特定 HttpResponseMessage,初始化 StatusCode 類別的新執行個體。 |
Content |
取得或設定 HTTP 回應訊息的內容。 |
Headers |
取得 HTTP 回應標頭的集合。 |
Is |
取得指示 HTTP 回應是否成功的值。 |
Reason |
取得或設定通常由伺服器連同狀態碼傳送的原因片語。 |
Request |
取得或設定導致此回應訊息的要求訊息。 |
Status |
取得或設定 HTTP 回應的狀態碼。 |
Trailing |
取得包含在 HTTP 回應中的尾端標頭集合。 |
Version |
取得或設定 HTTP 訊息版本。 |
Dispose() |
釋放 Unmanaged 資源,並處置 HttpResponseMessage 所使用的 Unmanaged 資源。 |
Dispose(Boolean) |
釋放 HttpResponseMessage 所使用的 Unmanaged 資源,並選擇性處置 Managed 資源。 |
Ensure |
如果 HTTP 回應的 IsSuccessStatusCode 屬性是 |
Equals(Object) |
判斷指定的物件是否等於目前的物件。 (繼承來源 Object) |
Get |
做為預設雜湊函式。 (繼承來源 Object) |
Get |
取得目前執行個體的 Type。 (繼承來源 Object) |
Memberwise |
建立目前 Object 的淺層複製。 (繼承來源 Object) |
To |
傳回代表目前物件的字串。 |
To |
從 Message 執行個體建立 HttpResponseMessage 執行個體。 |
產品 | 版本 |
---|---|
.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 |