@@ -303,10 +303,12 @@ public class Download
303303 private static readonly ILog log =
304304 LogManager . GetLogger ( System . Reflection . MethodBase . GetCurrentMethod ( ) . DeclaringType ) ;
305305
306+ static readonly HttpClient client = new HttpClient ( ) ;
307+
306308 public static async Task < string > PostAsync ( string uri , string data )
307309 {
308- var httpClient = new HttpClient ( ) ;
309- var response = await httpClient . PostAsync ( uri , new StringContent ( data ) ) ;
310+ var request = new HttpRequestMessage ( HttpMethod . Post , uri ) { Content = new StringContent ( data ) } ;
311+ var response = await client . SendAsync ( request ) ;
310312
311313 response . EnsureSuccessStatusCode ( ) ;
312314
@@ -316,8 +318,9 @@ public static async Task<string> PostAsync(string uri, string data)
316318
317319 public static async Task < string > GetAsync ( string uri )
318320 {
319- var httpClient = new HttpClient ( ) ;
320- var content = await httpClient . GetStringAsync ( uri ) ;
321+ var response = await client . SendAsync ( new HttpRequestMessage ( HttpMethod . Get , uri ) ) ;
322+ response . EnsureSuccessStatusCode ( ) ;
323+ var content = await response . Content . ReadAsStringAsync ( ) ;
321324 return await Task . Run ( ( ) => ( content ) ) ;
322325 }
323326
@@ -332,8 +335,7 @@ public struct HTTPResult
332335 /// </summary>
333336 public static async Task < HTTPResult > GetAsyncWithStatus ( string uri )
334337 {
335- var httpClient = new HttpClient ( ) ;
336- var response = await httpClient . GetAsync ( uri ) ;
338+ var response = await client . SendAsync ( new HttpRequestMessage ( HttpMethod . Get , uri ) ) ;
337339 var content = await response . Content . ReadAsStringAsync ( ) ;
338340 return await Task . Run ( ( ) => ( new HTTPResult ( ) { content = content , status = response . StatusCode } ) ) ;
339341 }
@@ -347,7 +349,6 @@ public static async Task<bool> getFilefromNetAsync(string url, string saveto, Ac
347349 log . Info ( "Get " + url ) ;
348350
349351 var request = new HttpRequestMessage ( HttpMethod . Get , url ) ;
350-
351352 RequestModification ? . Invoke ( url , request ) ;
352353
353354 using ( var response = await client . SendAsync ( request , completionOption : HttpCompletionOption . ResponseHeadersRead ) . ConfigureAwait ( false ) )
@@ -444,18 +445,15 @@ static Download()
444445 {
445446 if ( ! String . IsNullOrEmpty ( Settings . Instance . UserAgent ) )
446447 client . DefaultRequestHeaders . Add ( "User-Agent" , Settings . Instance . UserAgent ) ;
448+ client . Timeout = TimeSpan . FromSeconds ( 30 ) ;
447449 }
448450
449- static HttpClient client = new HttpClient ( ) ;
450451 public static bool getFilefromNet ( string url , string saveto , Action < int , string > status = null )
451452 {
452453 try
453454 {
454455 lock ( log )
455456 log . Info ( url ) ;
456- var client = new HttpClient ( ) ;
457- client . DefaultRequestHeaders . Add ( "User-Agent" , Settings . Instance . UserAgent ) ;
458- client . Timeout = TimeSpan . FromSeconds ( 30 ) ;
459457
460458 // Get the response.
461459 var response = client . GetAsync ( url , completionOption : HttpCompletionOption . ResponseHeadersRead ) . Result ;
@@ -569,9 +567,6 @@ public static bool CheckHTTPFileExists(string url)
569567 if ( url == null || url == "" || uri == null )
570568 return false ;
571569
572- var client = new HttpClient ( ) ;
573- client . DefaultRequestHeaders . Add ( "User-Agent" , Settings . Instance . UserAgent ) ;
574- client . Timeout = TimeSpan . FromSeconds ( 30 ) ;
575570 var resp = client . SendAsync ( new HttpRequestMessage ( HttpMethod . Head , url ) ) . Result ;
576571 return resp . IsSuccessStatusCode ;
577572
0 commit comments