@@ -51,14 +51,32 @@ public FileDownloadResponse DownloadFile(string url, bool sendAuthCookie = false
5151 }
5252
5353 using ( var resp = ( HttpWebResponse ) req . GetResponse ( ) )
54+ using ( var stream = resp . GetResponseStream ( ) )
5455 {
55- using ( var stream = resp . GetResponseStream ( ) )
56+ if ( resp . StatusCode == HttpStatusCode . OK )
5657 {
57- if ( resp . StatusCode == HttpStatusCode . OK )
58+ var data = stream . ToByteArray ( ) ;
59+ if ( data != null && data . Length != 0 )
5860 {
59- var data = stream . ToByteArray ( ) ;
60- var cd = new ContentDisposition ( resp . Headers [ "Content-Disposition" ] ) ;
61- var fileName = cd . FileName ;
61+ string fileName = null ;
62+
63+ var contentDisposition = resp . Headers [ "Content-Disposition" ] ;
64+ if ( contentDisposition . HasValue ( ) )
65+ {
66+ var cd = new ContentDisposition ( contentDisposition ) ;
67+ fileName = cd . FileName ;
68+ }
69+
70+ if ( fileName . IsEmpty ( ) )
71+ {
72+ try
73+ {
74+ var uri = new Uri ( url ) ;
75+ fileName = Path . GetFileName ( uri . LocalPath ) ;
76+ }
77+ catch { }
78+ }
79+
6280 return new FileDownloadResponse ( data , fileName , resp . ContentType ) ;
6381 }
6482 }
@@ -79,39 +97,51 @@ public async Task DownloadAsync(FileDownloadManagerContext context, IEnumerable<
7997
8098 private async Task DownloadFiles ( FileDownloadManagerContext context , IEnumerable < FileDownloadManagerItem > items )
8199 {
82- var client = new HttpClient ( ) ;
83-
84- client . DefaultRequestHeaders . CacheControl = new CacheControlHeaderValue ( ) ;
85- client . DefaultRequestHeaders . CacheControl . NoCache = true ;
86- client . DefaultRequestHeaders . Add ( "Connection" , "Keep-alive" ) ;
100+ try
101+ {
102+ using ( var client = new HttpClient ( ) )
103+ {
104+ client . DefaultRequestHeaders . CacheControl = new CacheControlHeaderValue ( ) ;
105+ client . DefaultRequestHeaders . CacheControl . NoCache = true ;
106+ client . DefaultRequestHeaders . Add ( "Connection" , "Keep-alive" ) ;
87107
88- if ( context . Timeout . TotalMilliseconds > 0 && context . Timeout != Timeout . InfiniteTimeSpan )
89- client . Timeout = context . Timeout ;
108+ if ( context . Timeout . TotalMilliseconds > 0 && context . Timeout != Timeout . InfiniteTimeSpan )
109+ client . Timeout = context . Timeout ;
90110
91- IEnumerable < Task > downloadTasksQuery =
92- from item in items
93- select ProcessUrl ( context , client , item ) ;
111+ IEnumerable < Task > downloadTasksQuery =
112+ from item in items
113+ select ProcessUrl ( context , client , item ) ;
94114
95- // now execute the bunch
96- List < Task > downloadTasks = downloadTasksQuery . ToList ( ) ;
115+ // now execute the bunch
116+ List < Task > downloadTasks = downloadTasksQuery . ToList ( ) ;
97117
98- while ( downloadTasks . Count > 0 )
99- {
100- // identify the first task that completes
101- Task firstFinishedTask = await Task . WhenAny ( downloadTasks ) ;
118+ while ( downloadTasks . Count > 0 )
119+ {
120+ // identify the first task that completes
121+ Task firstFinishedTask = await Task . WhenAny ( downloadTasks ) ;
102122
103- // process only once
104- downloadTasks . Remove ( firstFinishedTask ) ;
123+ // process only once
124+ downloadTasks . Remove ( firstFinishedTask ) ;
105125
106- await firstFinishedTask ;
126+ await firstFinishedTask ;
127+ }
128+ }
129+ }
130+ catch ( Exception exception )
131+ {
132+ if ( context . Logger != null )
133+ context . Logger . ErrorsAll ( exception ) ;
107134 }
108135 }
109136
110137 private async Task ProcessUrl ( FileDownloadManagerContext context , HttpClient client , FileDownloadManagerItem item )
111138 {
112139 try
113140 {
114- Task < Stream > task = client . GetStreamAsync ( item . Url ) ;
141+ //HttpResponseMessage response = await client.GetAsync(item.Url, HttpCompletionOption.ResponseHeadersRead);
142+ //Task<Stream> task = response.Content.ReadAsStreamAsync();
143+
144+ Task < Stream > task = client . GetStreamAsync ( item . Url ) ;
115145 await task ;
116146
117147 int count ;
@@ -132,29 +162,31 @@ private async Task ProcessUrl(FileDownloadManagerContext context, HttpClient cli
132162
133163 item . Success = ( ! task . IsFaulted && ! canceled ) ;
134164 }
135- catch ( Exception exc )
165+ catch ( Exception exception )
136166 {
137- item . Success = false ;
138- item . ErrorMessage = exc . ToAllMessages ( ) ;
139-
140- var webExc = exc . InnerException as WebException ;
141- if ( webExc != null )
142- item . ExceptionStatus = webExc . Status ;
167+ try
168+ {
169+ item . Success = false ;
170+ item . ErrorMessage = exception . ToAllMessages ( ) ;
143171
144- if ( context . Logger != null )
145- context . Logger . Error ( item . ToString ( ) , exc ) ;
172+ var webExc = exception . InnerException as WebException ;
173+ if ( webExc != null )
174+ item . ExceptionStatus = webExc . Status ;
175+
176+ if ( context . Logger != null )
177+ context . Logger . Error ( item . ToString ( ) , exception ) ;
178+ }
179+ catch { }
146180 }
147181 }
148-
149182 }
150183
184+
151185 public class FileDownloadResponse
152186 {
153187 public FileDownloadResponse ( byte [ ] data , string fileName , string contentType )
154188 {
155189 Guard . ArgumentNotNull ( ( ) => data ) ;
156- Guard . ArgumentNotEmpty ( ( ) => fileName ) ;
157- Guard . ArgumentNotEmpty ( ( ) => contentType ) ;
158190
159191 this . Data = data ;
160192 this . FileName = fileName ;
@@ -182,12 +214,12 @@ public class FileDownloadManagerContext
182214 /// <summary>
183215 /// Optional logger to log errors
184216 /// </summary>
185- public TraceLogger Logger { get ; set ; }
217+ public ILogger Logger { get ; set ; }
186218
187219 /// <summary>
188220 /// Cancellation token
189221 /// </summary>
190- public CancellationTokenSource CancellationToken { get ; set ; }
222+ public CancellationToken CancellationToken { get ; set ; }
191223
192224 /// <summary>
193225 /// Timeout for the HTTP client
0 commit comments