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) .

생성자

Name Description
HttpResponseMessage()

HttpResponseMessage 클래스의 새 인스턴스를 초기화합니다.

HttpResponseMessage(HttpStatusCode)

특정 StatusCode클래스를 사용하여 클래스의 HttpResponseMessage 새 인스턴스를 초기화합니다.

속성

Name Description
Content

HTTP 응답 메시지의 콘텐츠를 가져오거나 설정합니다.

Headers

HTTP 응답 헤더의 컬렉션을 가져옵니다.

IsSuccessStatusCode

HTTP 응답이 성공했는지를 나타내는 값을 가져옵니다.

ReasonPhrase

일반적으로 서버에서 상태 코드와 함께 전송되는 이유 구를 가져오거나 설정합니다.

RequestMessage

이 응답 메시지로 이어진 요청 메시지를 가져오거나 설정합니다.

StatusCode

HTTP 응답의 상태 코드를 가져오거나 설정합니다.

TrailingHeaders

HTTP 응답에 포함된 후행 헤더의 컬렉션을 가져옵니다.

Version

HTTP 메시지 버전을 가져오거나 설정합니다.

메서드

Name Description
Dispose()

관리되지 않는 리소스를 해제하고 해당 리소스에서 사용하는 관리되지 않는 리소스를 HttpResponseMessage삭제합니다.

Dispose(Boolean)

관리되는 리소스에 사용되는 관리되지 않는 리소스를 HttpResponseMessage 해제하고 필요에 따라 관리되는 리소스를 삭제합니다.

EnsureSuccessStatusCode()

HTTP 응답에 대한 속성이 IsSuccessStatusCode .인 경우 예외를 throw합니다 false.

Equals(Object)

지정한 개체와 현재 개체가 같은지 여부를 확인합니다.

(다음에서 상속됨 Object)
GetHashCode()

기본 해시 함수로 작동합니다.

(다음에서 상속됨 Object)
GetType()

현재 인스턴스의 Type 가져옵니다.

(다음에서 상속됨 Object)
MemberwiseClone()

현재 Object단순 복사본을 만듭니다.

(다음에서 상속됨 Object)
ToString()

현재 개체를 나타내는 문자열을 반환합니다.

확장명 메서드

Name Description
ToMessage(HttpResponseMessage)

Message 인스턴스에서 인스턴스를 HttpResponseMessage 만듭니다.

적용 대상