File size: 1,164 Bytes
fab29d7 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
using System;
using System.IO;
using System.Net.Http;
using System.Threading.Tasks;
namespace VersOne.Epub.Environment.Implementation
{
internal class ContentDownloader : IContentDownloader
{
private static readonly HttpClient httpClient;
static ContentDownloader()
{
httpClient = new HttpClient();
}
public async Task<Stream> DownloadAsync(string url, string userAgent)
{
try
{
using HttpRequestMessage httpRequestMessage = new(HttpMethod.Get, url);
httpRequestMessage.Headers.Add("User-Agent", userAgent);
HttpResponseMessage httpResponseMessage = await httpClient.SendAsync(httpRequestMessage, HttpCompletionOption.ResponseHeadersRead);
httpResponseMessage.EnsureSuccessStatusCode();
return await httpResponseMessage.Content.ReadAsStreamAsync();
}
catch (Exception exception)
{
throw new EpubContentDownloaderException("There was an error while downloading a remote content file.", exception, url);
}
}
}
}
|