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() |
释放由 HttpResponseMessage 使用的非托管资源。 |
Dispose(Boolean) |
释放由 HttpResponseMessage 使用的非托管资源,并可根据需要释放托管资源。 |
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 |