/// <summary> /// Gets the temp file. /// </summary> /// <param name="options">The options.</param> /// <param name="tempFile">The temp file.</param> /// <param name="resumeCount">The resume count.</param> /// <returns>Task{System.String}.</returns> /// <exception cref="System.ArgumentNullException">progress</exception> /// <exception cref="HttpException"></exception> private async Task <string> GetTempFile(HttpRequestOptions options, string tempFile, int resumeCount) { ValidateParams(options.Url, options.CancellationToken); if (options.Progress == null) { throw new ArgumentNullException("progress"); } options.CancellationToken.ThrowIfCancellationRequested(); var message = new HttpRequestMessage(HttpMethod.Get, options.Url); if (!string.IsNullOrEmpty(options.UserAgent)) { message.Headers.Add("User-Agent", options.UserAgent); } if (options.ResourcePool != null) { await options.ResourcePool.WaitAsync(options.CancellationToken).ConfigureAwait(false); } options.Progress.Report(0); _logger.Info("HttpClientManager.GetTempFile url: {0}, temp file: {1}", options.Url, tempFile); try { options.CancellationToken.ThrowIfCancellationRequested(); using (var response = await GetHttpClient(GetHostFromUrl(options.Url)).SendAsync(message, HttpCompletionOption.ResponseHeadersRead, options.CancellationToken).ConfigureAwait(false)) { EnsureSuccessStatusCode(response); options.CancellationToken.ThrowIfCancellationRequested(); IEnumerable <string> lengthValues; if (!response.Headers.TryGetValues("content-length", out lengthValues) && !response.Content.Headers.TryGetValues("content-length", out lengthValues)) { // We're not able to track progress using (var stream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false)) { using (var fs = new FileStream(tempFile, FileMode.Create, FileAccess.Write, FileShare.Read, StreamDefaults.DefaultFileStreamBufferSize, FileOptions.Asynchronous)) { await stream.CopyToAsync(fs, StreamDefaults.DefaultCopyToBufferSize, options.CancellationToken).ConfigureAwait(false); } } } else { var length = long.Parse(string.Join(string.Empty, lengthValues.ToArray())); using (var stream = ProgressStream.CreateReadProgressStream(await response.Content.ReadAsStreamAsync().ConfigureAwait(false), options.Progress.Report, length)) { using (var fs = new FileStream(tempFile, FileMode.Create, FileAccess.Write, FileShare.Read, StreamDefaults.DefaultFileStreamBufferSize, FileOptions.Asynchronous)) { await stream.CopyToAsync(fs, StreamDefaults.DefaultCopyToBufferSize, options.CancellationToken).ConfigureAwait(false); } } } options.Progress.Report(100); options.CancellationToken.ThrowIfCancellationRequested(); } } catch (Exception ex) { HandleTempFileException(ex, options, tempFile); } finally { if (options.ResourcePool != null) { options.ResourcePool.Release(); } } return(tempFile); }
public async Task <HttpResponseInfo> GetTempFileResponse(HttpRequestOptions options) { ValidateParams(options); Directory.CreateDirectory(_appPaths.TempDirectory); var tempFile = Path.Combine(_appPaths.TempDirectory, Guid.NewGuid() + ".tmp"); if (options.Progress == null) { throw new ArgumentNullException("progress"); } options.CancellationToken.ThrowIfCancellationRequested(); var httpWebRequest = GetRequest(options, "GET", options.EnableHttpCompression); if (options.ResourcePool != null) { await options.ResourcePool.WaitAsync(options.CancellationToken).ConfigureAwait(false); } options.Progress.Report(0); if (options.LogRequest) { _logger.Info("HttpClientManager.GetTempFileResponse url: {0}", options.Url); } try { options.CancellationToken.ThrowIfCancellationRequested(); using (var response = await httpWebRequest.GetResponseAsync().ConfigureAwait(false)) { var httpResponse = (HttpWebResponse)response; EnsureSuccessStatusCode(httpResponse, options); options.CancellationToken.ThrowIfCancellationRequested(); var contentLength = GetContentLength(httpResponse); if (!contentLength.HasValue) { // We're not able to track progress using (var stream = httpResponse.GetResponseStream()) { using (var fs = _fileSystem.GetFileStream(tempFile, FileMode.Create, FileAccess.Write, FileShare.Read, true)) { await stream.CopyToAsync(fs, StreamDefaults.DefaultCopyToBufferSize, options.CancellationToken).ConfigureAwait(false); } } } else { using (var stream = ProgressStream.CreateReadProgressStream(httpResponse.GetResponseStream(), options.Progress.Report, contentLength.Value)) { using (var fs = _fileSystem.GetFileStream(tempFile, FileMode.Create, FileAccess.Write, FileShare.Read, true)) { await stream.CopyToAsync(fs, StreamDefaults.DefaultCopyToBufferSize, options.CancellationToken).ConfigureAwait(false); } } } options.Progress.Report(100); return(GetResponseInfo(httpResponse, tempFile, contentLength)); } } catch (Exception ex) { DeleteTempFile(tempFile); throw GetException(ex, options); } finally { if (options.ResourcePool != null) { options.ResourcePool.Release(); } } }
public async Task <HttpResponseInfo> GetTempFileResponse(HttpRequestOptions options) { ValidateParams(options.Url, options.CancellationToken); var tempFile = Path.Combine(_appPaths.TempDirectory, Guid.NewGuid() + ".tmp"); if (options.Progress == null) { throw new ArgumentNullException("progress"); } options.CancellationToken.ThrowIfCancellationRequested(); if (options.ResourcePool != null) { await options.ResourcePool.WaitAsync(options.CancellationToken).ConfigureAwait(false); } options.Progress.Report(0); _logger.Info("HttpClientManager.GetTempFile url: {0}, temp file: {1}", options.Url, tempFile); try { options.CancellationToken.ThrowIfCancellationRequested(); using (var message = GetHttpRequestMessage(options)) { using (var response = await GetHttpClient(GetHostFromUrl(options.Url), options.EnableHttpCompression).HttpClient.SendAsync(message, HttpCompletionOption.ResponseHeadersRead, options.CancellationToken).ConfigureAwait(false)) { EnsureSuccessStatusCode(response); options.CancellationToken.ThrowIfCancellationRequested(); var contentLength = GetContentLength(response); if (!contentLength.HasValue) { // We're not able to track progress using (var stream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false)) { using (var fs = _fileSystem.GetFileStream(tempFile, FileMode.Create, FileAccess.Write, FileShare.Read, true)) { await stream.CopyToAsync(fs, StreamDefaults.DefaultCopyToBufferSize, options.CancellationToken).ConfigureAwait(false); } } } else { using (var stream = ProgressStream.CreateReadProgressStream(await response.Content.ReadAsStreamAsync().ConfigureAwait(false), options.Progress.Report, contentLength.Value)) { using (var fs = _fileSystem.GetFileStream(tempFile, FileMode.Create, FileAccess.Write, FileShare.Read, true)) { await stream.CopyToAsync(fs, StreamDefaults.DefaultCopyToBufferSize, options.CancellationToken).ConfigureAwait(false); } } } options.Progress.Report(100); return(new HttpResponseInfo { TempFilePath = tempFile, StatusCode = response.StatusCode, ContentType = response.Content.Headers.ContentType.MediaType }); } } } catch (Exception ex) { throw GetTempFileException(ex, options, tempFile); } finally { if (options.ResourcePool != null) { options.ResourcePool.Release(); } } }