Внутри консольного приложения (созданного с помощью dotnet new console --framework net5.0) я есть в основном этот код
Код: Выделить всё
using System;
using System.Collections.Specialized;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
....
var client = new HttpClient();
var request = new HttpRequestMessage();
request.Method = HttpMethod.Get;
// add uri and header and so on ...
HttpResponseMessage response = await client.SendAsync(request);
PrintHeaders(response);
Код: Выделить всё
public static void PrintHeaders(HttpResponseMessage response)
{
// cast from HttpRequestHeaders
// and from HttpResponseHeaders
PrintHeaders((HttpHeaders)response.RequestMessage.Headers);
PrintHeaders((HttpHeaders)response.Headers);
}
Код: Выделить всё
public static void PrintHeaders(HttpHeaders headers)
{
Console.WriteLine( " Printing Headers " );
Console.WriteLine( " KEY VALUE" );
var headersEnumerator = headers.GetEnumerator();
while (headersEnumerator.MoveNext())
{
Console.WriteLine( " {0,-25} {1}"
, headersEnumerator.Current.Key
, String.Join(" ",headersEnumerator.Current.Value.GetEnumerator()) );
}
Console.WriteLine();
}
Код работает настолько, что получает ответ от сервера. И записывает заголовки в консоль.
Вывод
Код: Выделить всё
Printing Headers
KEY VALUE
Authorization System.String[]
Printing Headers
KEY VALUE
Date System.String[]
Date System.SZGenericArrayEnumerator`1[System.String]
Connection System.String[]
Код: Выделить всё
// System.String[] for
headersEnumerator.Current.Value.ToString()
// System.SZGenericArrayEnumerator`1[System.String] for
String.Join(" ",headersEnumerator.Current.Value.GetEnumerator())
Не работает получение значения header.value в виде простой строки, например, для даты поля заголовка< /code> что-то вроде среды, 7 сентября 2022 г., 10:45:04 GMT
Вместо этого я получаю только System.String[] или что-то подобное. р>
Вопрос
Что я могу сделать, чтобы вывести все ключи заголовков и их значения в виде обычного текста на консоль?
Подробнее здесь: https://stackoverflow.com/questions/736 ... to-console
Мобильная версия