@@ -29,6 +29,7 @@ static Http()
29
29
| SecurityProtocolType . Tls11
30
30
| SecurityProtocolType . Tls12 ;
31
31
32
+ client = new HttpClient ( socketsHttpHandler , false ) ;
32
33
client . DefaultRequestHeaders . Add ( "User-Agent" , UserAgent ) ;
33
34
34
35
}
@@ -46,7 +47,7 @@ private get
46
47
}
47
48
}
48
49
49
- public static WebProxy WebProxy { get ; private set ; }
50
+ public static WebProxy WebProxy { get ; private set ; } = new WebProxy ( ) ;
50
51
51
52
/// <summary>
52
53
/// Update the Address of the Proxy to modify the client Proxy
@@ -74,11 +75,18 @@ public static void UpdateProxy()
74
75
}
75
76
}
76
77
77
- public static void Download ( [ NotNull ] string url , [ NotNull ] string filePath )
78
+ public async static Task Download ( [ NotNull ] string url , [ NotNull ] string filePath )
78
79
{
79
- var client = new WebClient { Proxy = WebProxy } ;
80
- client . Headers . Add ( "user-agent" , UserAgent ) ;
81
- client . DownloadFile ( url , filePath ) ;
80
+ using var response = await client . GetAsync ( url ) ;
81
+ if ( response . StatusCode == HttpStatusCode . OK )
82
+ {
83
+ using var fileStream = new FileStream ( filePath , FileMode . CreateNew ) ;
84
+ await response . Content . CopyToAsync ( fileStream ) ;
85
+ }
86
+ else
87
+ {
88
+ throw new WebException ( $ "Error code <{ response . StatusCode } > returned from <{ url } >") ;
89
+ }
82
90
}
83
91
84
92
public static async Task < string > Get ( [ NotNull ] string url , string encoding = "UTF-8" )
0 commit comments