45 lines
1.9 KiB
C#
45 lines
1.9 KiB
C#
using System.Net;
|
||
|
||
namespace YandexMusic.API.Common.Providers;
|
||
|
||
/// <summary>Стандартный провайдер HTTP-запросов с использованием HttpClient.</summary>
|
||
public class DefaultRequestProvider : CommonRequestProvider
|
||
{
|
||
/// <summary>Инициализирует новый экземпляр провайдера.</summary>
|
||
/// <param name="authStorage">Хранилище авторизации.</param>
|
||
public DefaultRequestProvider(AuthStorage authStorage) : base(authStorage) { }
|
||
|
||
/// <summary>Выполняет HTTP-запрос и возвращает ответ.</summary>
|
||
/// <param name="message">HTTP-запрос.</param>
|
||
/// <param name="completionOption">Опция завершения запроса.</param>
|
||
/// <returns>HTTP-ответ.</returns>
|
||
public override async Task<HttpResponseMessage> GetWebResponseAsync(
|
||
HttpRequestMessage message,
|
||
HttpCompletionOption completionOption = HttpCompletionOption.ResponseContentRead)
|
||
{
|
||
using var handler = new SocketsHttpHandler
|
||
{
|
||
Proxy = storage.Context.WebProxy,
|
||
AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate,
|
||
UseCookies = true,
|
||
CookieContainer = storage.Context.Cookies,
|
||
AllowAutoRedirect = true,
|
||
MaxAutomaticRedirections = 10
|
||
};
|
||
|
||
using var client = new HttpClient(handler);
|
||
|
||
try
|
||
{
|
||
return await client.SendAsync(message, completionOption);
|
||
}
|
||
catch (HttpRequestException ex)
|
||
{
|
||
// Пытаемся извлечь тело ошибки, если оно доступно
|
||
if (ex.InnerException == null)
|
||
throw;
|
||
|
||
throw new Exception($"Ошибка HTTP-запроса: {ex.Message}", ex);
|
||
}
|
||
}
|
||
} |