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)

特定のStatusCodeを使用して、HttpResponseMessage クラスの新しいインスタンスを初期化します。

プロパティ

名前 説明
Content

HTTP 応答メッセージの内容を取得または設定します。

Headers

HTTP 応答ヘッダーのコレクションを取得します。

IsSuccessStatusCode

HTTP 応答が成功したかどうかを示す値を取得します。

ReasonPhrase

通常、サーバーによって状態コードと共に送信される理由フレーズを取得または設定します。

RequestMessage

この応答メッセージの原因となった要求メッセージを取得または設定します。

StatusCode

HTTP 応答の状態コードを取得または設定します。

TrailingHeaders

HTTP 応答に含まれる末尾のヘッダーのコレクションを取得します。

Version

HTTP メッセージのバージョンを取得または設定します。

メソッド

名前 説明
Dispose()

アンマネージ リソースを解放し、 HttpResponseMessageによって使用されるアンマネージ リソースを破棄します。

Dispose(Boolean)

HttpResponseMessageによって使用されるアンマネージ リソースを解放し、必要に応じてマネージド リソースを破棄します。

EnsureSuccessStatusCode()

HTTP 応答の IsSuccessStatusCode プロパティが false場合、例外をスローします。

Equals(Object)

指定したオブジェクトが現在のオブジェクトと等しいかどうかを判断します。

(継承元 Object)
GetHashCode()

既定のハッシュ関数として機能します。

(継承元 Object)
GetType()

現在のインスタンスの Type を取得します。

(継承元 Object)
MemberwiseClone()

現在の Objectの簡易コピーを作成します。

(継承元 Object)
ToString()

現在のオブジェクトを表す文字列を返します。

拡張メソッド

名前 説明
ToMessage(HttpResponseMessage)

Message インスタンスからHttpResponseMessage インスタンスを作成します。

適用対象