Заголовки ответа в C#
Заголовки ответа отделены от тела. Имена не
чувствительны к регистру; тип содержимого чаще
берут из Content.Headers.ContentType.
Запрос HEAD на example.com даёт 200
без тела. Заголовок "Content-Type" обычно
содержит "text/html":
using HttpClient client = new();
using HttpResponseMessage response = await client.SendAsync(
new HttpRequestMessage(HttpMethod.Head, "https://example.com/"));
Console.WriteLine((int)response.StatusCode);
Console.WriteLine(response.Content.Headers.ContentType?.MediaType);
Дата в response.Headers.Date может
отсутствовать. Перед чтением проверяют HasValue:
using HttpClient client = new();
using HttpResponseMessage response = await client.GetAsync("https://example.com/");
Console.WriteLine(response.Headers.Date.HasValue);
Выведите тип содержимого ответа
"https://httpbin.org/html" одной строкой.