/
aprogrammer
/
dotnet-docs
Обзор
Документация
Войти
/
aprogrammer
/
dotnet-docs
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
docs/fundamentals/networking/snippets/httpclient/Program.Responses.cs
48 строк
1 KB
David Pine
Add code examples and update content for HTTP error handling (#31780)
14 окт 2022, 23:18
Не верифицирован
14 окт 2022, 23:18
2a44b4b
Код
Авторство
О чём код?
static partial class Program { static async Task HandleResponsesAsync<T>(HttpClient httpClient) { using HttpRequestMessage request = new( HttpMethod.Head, "https://www.example.com"); // <request> using HttpResponseMessage response = await httpClient.SendAsync(request); // </request> // <isstatuscode> if (response is { StatusCode: HttpStatusCode.OK }) { // Omitted for brevity... } // </isstatuscode> // <issuccessstatuscode> if (response.IsSuccessStatusCode) { // Omitted for brevity... } // </issuccessstatuscode> // <ensurestatuscode> response.EnsureSuccessStatusCode(); // </ensurestatuscode> // <stream> await using Stream responseStream = await response.Content.ReadAsStreamAsync(); // </stream> // <array> byte[] responseByteArray = await response.Content.ReadAsByteArrayAsync(); // </array> // <string> string responseString = await response.Content.ReadAsStringAsync(); // </string> // <json> T? result = await response.Content.ReadFromJsonAsync<T>(); // </json> } }