コード例 #1
0
        public async Task <string> StartDownload(DownloadRequest downloadRequest, AppSettings appSettings)
        {
            string fileName = null;

            SetClientRequestHeaders(downloadRequest, appSettings);

            using (var response = await Client.GetAsync(downloadRequest.DownloadUrl, HttpCompletionOption.ResponseHeadersRead))
            {
                if (response.StatusCode == System.Net.HttpStatusCode.Unauthorized)
                {
                    Log.Error("Download failed: You need to authorize access before downloading file");
                }
                else if (!response.IsSuccessStatusCode)
                {
                    var message = $"Download failed for url: {downloadRequest.DownloadUrl}, - response from server was: {response.StatusCode} - {response.ReasonPhrase}";
                    Log.Error(message);
                    Console.WriteLine(message);
                }
                else
                {
                    using (var contentStream = await response.Content.ReadAsStreamAsync())
                    {
                        string destinationFilePath = downloadRequest.GetDestinationFilePath(response);
                        fileName = downloadRequest.GetDestinationFileName(response);
                        var totalBytes = response.Content.Headers.ContentLength;
                        await ProcessContentStream(totalBytes, contentStream, destinationFilePath);
                    }
                }
            }
            return(fileName);
        }
コード例 #2
0
        public async Task <string> StartDownload(DownloadRequest downloadRequest, AppSettings appSettings)
        {
            string fileName = null;

            SetClientRequestHeaders(downloadRequest, appSettings);

            using (var response = await Client.GetAsync(downloadRequest.DownloadUrl, HttpCompletionOption.ResponseHeadersRead))
            {
                if (!response.IsSuccessStatusCode)
                {
                    Console.WriteLine("Download failed - response from server was: " + response.StatusCode + " - " + response.ReasonPhrase);
                }
                else
                {
                    using (var contentStream = await response.Content.ReadAsStreamAsync())
                    {
                        string destinationFilePath = downloadRequest.GetDestinationFilePath(response);
                        fileName = downloadRequest.GetDestinationFileName(response);
                        var totalBytes = response.Content.Headers.ContentLength;
                        await ProcessContentStream(totalBytes, contentStream, destinationFilePath);
                    }
                }
            }
            return(fileName);
        }