public async Task DownloadFileAsync(string url, string fileName) { try { var fileInfo = new FileInfo(fileName); if (fileInfo.Directory != null && !fileInfo.Directory.Exists) { fileInfo.Directory.Create(); } _logger.Debug("Downloading [{0}] to [{1}]", url, fileName); var stopWatch = Stopwatch.StartNew(); var uri = new HttpUri(url); using (var webClient = new GZipWebClient()) { webClient.Headers.Add(HttpRequestHeader.UserAgent, _userAgentBuilder.GetUserAgent()); webClient.Proxy = GetProxy(uri); await webClient.DownloadFileTaskAsync(url, fileName); stopWatch.Stop(); _logger.Debug("Downloading Completed. took {0:0}s", stopWatch.Elapsed.Seconds); } } catch (WebException e) { _logger.Warn("Failed to get response from: {0} {1}", url, e.Message); if (File.Exists(fileName)) { File.Delete(fileName); } throw; } catch (Exception e) { _logger.Warn(e, "Failed to get response from: " + url); if (File.Exists(fileName)) { File.Delete(fileName); } throw; } }
public async Task DownloadFileAsync(string url, string filePath, string referer = null) { var webclient = new GZipWebClient(); if(!string.IsNullOrEmpty(referer)) webclient.Headers.Add("Referer", referer); try { _logger.Info($"Downloading file: {url} and saving to {filePath}"); await webclient.DownloadFileTaskAsync(new Uri(url), filePath); _analyticsService.ReportEvent(AnalyticEvent.Download, url); } catch (WebException ex) { _logger.Error(ex); _analyticsService.ReportEvent(AnalyticEvent.DownloadFailed, url); throw; } }
public async Task DownloadFileAsync(string url, string filePath, string referer = null) { var webclient = new GZipWebClient(); if (!string.IsNullOrEmpty(referer)) { webclient.Headers.Add("Referer", referer); } try { _logger.Info($"Downloading file: {url} and saving to {filePath}"); await webclient.DownloadFileTaskAsync(new Uri(url), filePath); _analyticsService.ReportEvent(AnalyticEvent.Download, url); } catch (WebException ex) { _logger.Error(ex); _analyticsService.ReportEvent(AnalyticEvent.DownloadFailed, url); throw; } }