HttpResponseMessage Classe
Importante
Alcune informazioni sono relative alla release non definitiva del prodotto, che potrebbe subire modifiche significative prima della release definitiva. Microsoft non riconosce alcuna garanzia, espressa o implicita, in merito alle informazioni qui fornite.
Rappresenta un messaggio di risposta HTTP che include il codice di stato e i dati.
public ref class HttpResponseMessage : IDisposable
public class HttpResponseMessage : IDisposable
type HttpResponseMessage = class
interface IDisposable
Public Class HttpResponseMessage
Implements IDisposable
- Ereditarietà
-
HttpResponseMessage
- Implementazioni
// 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
Nell'esempio di codice precedente viene usato un async Task Main()
punto di ingresso. Questa funzionalità richiede C# 7.1 o versione successiva.
Un modo comune per ottenere un oggetto HttpResponseMessage è da uno dei HttpClient.SendAsync(HttpRequestMessage) metodi .
Http |
Inizializza una nuova istanza della classe HttpResponseMessage. |
Http |
Inizializza una nuova istanza della classe HttpResponseMessage con un StatusCode specifico. |
Content |
Ottiene o imposta il contenuto di un messaggio di risposta HTTP. |
Headers |
Ottiene la raccolta di intestazioni di risposta HTTP. |
Is |
Ottiene un valore che indica se la risposta HTTP ha avuto esito positivo. |
Reason |
Ottiene o imposta l'enunciazione generalmente inviata dai server con il codice di stato. |
Request |
Ottiene o imposta il messaggio di richiesta che ha portato a questo messaggio di risposta. |
Status |
Ottiene e imposta il codice di stato della risposta HTTP. |
Trailing |
Ottiene la raccolta di intestazioni finali incluse in una risposta HTTP. |
Version |
Ottiene o imposta la versione del messaggio HTTP. |
Dispose() |
Rilascia le risorse non gestite ed elimina le risorse non gestite utilizzate dall'oggetto HttpResponseMessage. |
Dispose(Boolean) |
Rilascia le risorse non gestite usate da HttpResponseMessage e, facoltativamente, elimina le risorse gestite. |
Ensure |
Genera un'eccezione se la proprietà IsSuccessStatusCode per la risposta HTTP è |
Equals(Object) |
Determina se l'oggetto specificato è uguale all'oggetto corrente. (Ereditato da Object) |
Get |
Funge da funzione hash predefinita. (Ereditato da Object) |
Get |
Ottiene l'oggetto Type dell'istanza corrente. (Ereditato da Object) |
Memberwise |
Crea una copia superficiale dell'oggetto Object corrente. (Ereditato da Object) |
To |
Restituisce una stringa che rappresenta l'oggetto corrente. |
To |
Crea un'istanza di Message da un'istanza di HttpResponseMessage. |
Prodotto | Versioni |
---|---|
.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 |
Feedback su .NET
.NET è un progetto open source. Seleziona un collegamento per fornire feedback: