6.22The HttpFileDownloadBuilder builder
In addition to the methods above, you can also use the HttpFileDownloadBuilder builder to configure the various settings required for downloading network resources.
var fileTransferResult = await httpRemoteService.SendAsync(HttpRequestBuilder.DownloadFile("https://furion.net/img/furionlogo.png", @"C:\Workspaces\"));The HttpFileDownloadBuilder builder provides the various settings specifically used by the framework to download network resources. The constructor of HttpFileDownloadBuilder is private, so it cannot be instantiated directly with the new keyword; however, the framework provides several static overloads of HttpRequestBuilder.DownloadFile to create instances of HttpFileDownloadBuilder.
HttpRequestBuilder.DownloadFile(httpMethod, requestUri, destinationPath, onProgressChanged, fileExistsBehavior, configure);HttpRequestBuilder.DownloadFile(requestUri, destinationPath, onProgressChanged, fileExistsBehavior, configure); // Defaults to a GET requestIn addition, HttpFileDownloadBuilder includes the following configuration features:
// Defaults to a GET request. If the save file name is not specified, the file name is resolved automatically, e.g., the final download path is C:\Workspaces\furionlogo.pngHttpRequestBuilder.DownloadFile("https://furion.net/img/furionlogo.png", @"C:\Workspaces\") // Sets the buffer size used for the transfer operation, in bytes; the default value is 80 KB .SetBufferSize(80 * 1024) // Sets the destination path where the file is saved; it can be set to null, in which case the DefaultFileDownloadDirectory property of HttpRemoteOptions or the application's execution directory is used .SetDestinationPath(@"C:\Workspaces\") // Sets the behavior when the target file already exists .SetFileExistsBehavior(FileExistsBehavior.Overwrite) // Sets the interval for file transfer progress (notifications) .SetProgressInterval(TimeSpan.FromSeconds(1)) // Sets the action to run when the file transfer starts .SetOnTransferStarted(() => {}) // Sets the delegate to execute when the transfer progress changes .SetOnProgressChanged(async progress => { }) // Sets the action to run when the file transfer completes; the delegate parameter is the total time spent on the file transfer (in milliseconds) .SetOnTransferCompleted(duration => {}) // Sets the action to run when an exception occurs during the file transfer .SetOnTransferFailed(exception => {}) // Sets the action to run when the file exists and is configured to be skipped .SetOnFileExistAndSkip(() => {}) // Sets the HTTP file transfer event handler; CustomFileTransferEventHandler is a type that implements the IHttpFileTransferEventHandler interface .SetEventHandler<CustomFileTransferEventHandler>() .SetEventHandler(typeof(CustomFileTransferEventHandler)) // Sets the HttpRequestBuilder instance .With(builder => {}) // Supports further extension // Sets the maximum number of download threads .SetMaxThreads(4) // Sets the maximum idle wait time for a single data read (sliding window timeout) .SetChunkTimeout(TimeSpan.FromSeconds(100)) // Sets the maximum number of retries for multi-threaded chunked downloads .SetChunkMaxRetries(3) // Enables high-speed download mode .EnableHighSpeedMode(); // Supports passing in the maximum number of download threadsAfter successfully building a HttpFileDownloadBuilder instance via the HttpRequestBuilder.DownloadFile method, you can use the Send method or the asynchronous SendAsync method to execute the send operation.
var fileTransferResult = httpRemoteService.Send(httpFileDownloadBuilder, cancellationToken);var fileTransferResult = await httpRemoteService.SendAsync(httpFileDownloadBuilder, cancellationToken);