protected async Task DownloadFileToStreamAsync(string url, Stream writer, IProgress <DownloadBytesProgress> progressReport) { int receivedBytes = 0; int totalBytes = 0; using (var dclient = this.CreateClient()) { var client = dclient.Client; var response = await client.GetAsync(this.GetUri(url), HttpCompletionOption.ResponseHeadersRead).ConfigureAwait(false); if (!(await this.HandleResponseAsync(response).ConfigureAwait(false))) { return; } var stream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false); byte[] buffer = new byte[4096]; totalBytes = (int)response.Content.Headers.ContentLength.Value; for (; ;) { int bytesRead = await stream.ReadAsync(buffer, 0, buffer.Length).ConfigureAwait(false); if (bytesRead == 0) { break; } writer.Write(buffer, 0, bytesRead); receivedBytes += bytesRead; if (progressReport != null) { var args = new DownloadBytesProgress(url, receivedBytes, totalBytes); progressReport.Report(args); } } } }
protected async Task DownloadFileToStreamAsync(string url, Stream writer, IProgress<DownloadBytesProgress> progressReport) { int receivedBytes = 0; int totalBytes = 0; using (var dclient = this.CreateClient()) { var client = dclient.Client; var response = await client.GetAsync(this.GetUri(url), HttpCompletionOption.ResponseHeadersRead).ConfigureAwait(false); if (!(await this.HandleResponseAsync(response).ConfigureAwait(false))) return; var stream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false); byte[] buffer = new byte[4096]; totalBytes = (int)response.Content.Headers.ContentLength.Value; for (; ; ) { int bytesRead = await stream.ReadAsync(buffer, 0, buffer.Length).ConfigureAwait(false); if (bytesRead == 0) break; writer.Write(buffer, 0, bytesRead); receivedBytes += bytesRead; if (progressReport != null) { var args = new DownloadBytesProgress(url, receivedBytes, totalBytes); progressReport.Report(args); } } } }