HttpResponseMessage 類別

定義

表示包含狀態碼及資料的 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) 一種方法。

建構函式

HttpResponseMessage()

初始化 HttpResponseMessage 類別的新執行個體。

HttpResponseMessage(HttpStatusCode)

使用特定 HttpResponseMessage,初始化 StatusCode 類別的新執行個體。

屬性

Content

取得或設定 HTTP 回應訊息的內容。

Headers

取得 HTTP 回應標頭的集合。

IsSuccessStatusCode

取得指示 HTTP 回應是否成功的值。

ReasonPhrase

取得或設定通常由伺服器連同狀態碼傳送的原因片語。

RequestMessage

取得或設定導致此回應訊息的要求訊息。

StatusCode

取得或設定 HTTP 回應的狀態碼。

TrailingHeaders

取得包含在 HTTP 回應中的尾端標頭集合。

Version

取得或設定 HTTP 訊息版本。

方法

Dispose()

釋放 Unmanaged 資源,並處置 HttpResponseMessage 所使用的 Unmanaged 資源。

Dispose(Boolean)

釋放 HttpResponseMessage 所使用的 Unmanaged 資源,並選擇性處置 Managed 資源。

EnsureSuccessStatusCode()

如果 HTTP 回應的 IsSuccessStatusCode 屬性是 false,即會擲回例外狀況。

Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
ToString()

傳回代表目前物件的字串。

擴充方法

ToMessage(HttpResponseMessage)

Message 執行個體建立 HttpResponseMessage 執行個體。

適用於