/
aprogrammer
/
dotnet-docs
Обзор
Документация
Войти
/
aprogrammer
/
dotnet-docs
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
docs/fundamentals/networking/snippets/httpclient/Program.CancellationStream.cs
25 строк
785 B
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 WithCancellationExtensionsAsync(HttpClient httpClient) { // <helpers> try { // These extension methods will throw HttpRequestException // with StatusCode set when the HTTP request status code isn't 2xx: // // GetByteArrayAsync // GetStreamAsync // GetStringAsync using var stream = await httpClient.GetStreamAsync( "https://localhost:5001/doesNotExists"); } catch (HttpRequestException ex) when (ex is { StatusCode: HttpStatusCode.NotFound }) { // Handle 404 Console.WriteLine($"Not found: {ex.Message}"); } // </helpers> } }