Saltar al contenido principal

HttpClientFileDownloader class

Namespace: Velopack.Sources

A simple abstractable file downloader

public class HttpClientFileDownloader : IFileDownloader

Implements: IFileDownloader

Constructors

ConstructorSummary
HttpClientFileDownloader

HttpClientFileDownloader()

public HttpClientFileDownloader()

Properties

PropertySummary
UserAgentThe User-Agent sent with requests

UserAgent

public static ProductInfoHeaderValue UserAgent { get; }

The User-Agent sent with requests

Methods

MethodSummary
DownloadFileDownloads a remote file to the specified local path
DownloadBytesReturns a byte array containing the contents of the file at the specified url
DownloadStringReturns a string containing the contents of the specified url
TryDownloadThenLowercase<T>Tries to download a string from the specified url. If it fails, it will attempt to
DownloadToStreamInternalAsynchronously downloads a remote url to the specified destination stream while
CreateHttpClientHandlerCreates a new HttpClientHandler with default settings, used for
CreateHttpClientCreates a new HttpClient for every request.

DownloadFile(url, targetFile, progress, headers, timeout, cancelToken)

public virtual Task DownloadFile(string url, string targetFile, Action<int> progress, IDictionary<string, string>? headers, double timeout, CancellationToken cancelToken = default)

Downloads a remote file to the specified local path

Parameters

NameTypeDescription
urlstringThe url which will be downloaded.
targetFilestringThe local path where the file will be stored
If a file exists at this path, it will be overwritten.
progressAction<int>A delegate for reporting download progress, with expected values from 0-100.
headersIDictionary<string, string>Headers that can be passed to Http Downloader, e.g. Accept or Authorization.
timeoutdoubleThe maximum time in minutes to wait for the download to complete.
cancelTokenCancellationTokenOptional token to cancel the request.

Returns Task

DownloadBytes(url, headers, timeout)

public virtual Task<byte[]> DownloadBytes(string url, IDictionary<string, string>? headers, double timeout)

Returns a byte array containing the contents of the file at the specified url

Parameters

NameTypeDescription
urlstring
headersIDictionary<string, string>
timeoutdouble

Returns Task<byte[]>

DownloadString(url, headers, timeout)

public virtual Task<string> DownloadString(string url, IDictionary<string, string>? headers, double timeout)

Returns a string containing the contents of the specified url

Parameters

NameTypeDescription
urlstring
headersIDictionary<string, string>
timeoutdouble

Returns Task<string>

TryDownloadThenLowercase(downloadFunc, url)

protected virtual Task<T> TryDownloadThenLowercase<T>(Func<string, Task<T>> downloadFunc, string url)

Tries to download a string from the specified url. If it fails, it will attempt to download the string again with the url lowercased. This is useful for services that are case-sensitive yet corrupt the case on upload.

Parameters

NameTypeDescription
downloadFuncFunc<string, Task<T>>
urlstring

Returns Task<T>

DownloadToStreamInternal(client, requestUri, destination, progress, cancelToken)

protected virtual Task DownloadToStreamInternal(HttpClient client, string requestUri, Stream destination, Action<int>? progress = null, CancellationToken cancelToken = default)

Asynchronously downloads a remote url to the specified destination stream while providing progress updates.

Parameters

NameTypeDescription
clientHttpClient
requestUristring
destinationStream
progressAction<int>
cancelTokenCancellationToken

Returns Task

CreateHttpClientHandler()

protected virtual HttpClientHandler CreateHttpClientHandler()

Creates a new HttpClientHandler with default settings, used for new HttpClient's. Override this function to add client certificates, proxy configurations, cookies, or change other http behaviors.

Returns HttpClientHandler

CreateHttpClient(headers, timeout)

protected virtual HttpClient CreateHttpClient(IDictionary<string, string>? headers, double timeout)

Creates a new HttpClient for every request.

Parameters

NameTypeDescription
headersIDictionary<string, string>
timeoutdouble

Returns HttpClient


Generated from Velopack 1.1.1