HttpResponseMessage 类  
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
表示包括状态代码和数据的 HTTP 响应消息。
public ref class HttpResponseMessage : IDisposablepublic class HttpResponseMessage : IDisposabletype HttpResponseMessage = class
    interface IDisposablePublic 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() | 释放由 HttpResponseMessage 使用的非托管资源。 | 
| Dispose(Boolean) | 释放由 HttpResponseMessage 使用的非托管资源,并可根据需要释放托管资源。 | 
| EnsureSuccessStatusCode() | 如果 HTTP 响应的 IsSuccessStatusCode 属性是  | 
| Equals(Object) | 确定指定对象是否等于当前对象。(继承自 Object) | 
| GetHashCode() | 作为默认哈希函数。(继承自 Object) | 
| GetType() | 获取当前实例的 Type。(继承自 Object) | 
| MemberwiseClone() | 创建当前 Object 的浅表副本。(继承自 Object) | 
| ToString() | 返回表示当前对象的字符串。 | 
扩展方法
| ToMessage(HttpResponseMessage) | 通过 Message 实例创建 HttpResponseMessage 实例。 |