@@ -75,23 +75,31 @@ public static void UpdateProxy(ProxyProperty property)
7575 } ;
7676 }
7777
78- public static async Task Download ( [ NotNull ] string url , [ NotNull ] string filePath )
78+ public static async Task DownloadAsync ( [ NotNull ] string url , [ NotNull ] string filePath )
7979 {
80- using var response = await client . GetAsync ( url ) ;
81- if ( response . StatusCode == HttpStatusCode . OK )
80+ try
8281 {
83- await using var fileStream = new FileStream ( filePath , FileMode . CreateNew ) ;
84- await response . Content . CopyToAsync ( fileStream ) ;
82+ using var response = await client . GetAsync ( url ) ;
83+ if ( response . StatusCode == HttpStatusCode . OK )
84+ {
85+ await using var fileStream = new FileStream ( filePath , FileMode . CreateNew ) ;
86+ await response . Content . CopyToAsync ( fileStream ) ;
87+ }
88+ else
89+ {
90+ throw new HttpRequestException ( $ "Error code <{ response . StatusCode } > returned from <{ url } >") ;
91+ }
8592 }
86- else
93+ catch ( HttpRequestException e )
8794 {
88- throw new HttpRequestException ( $ "Error code <{ response . StatusCode } > returned from <{ url } >") ;
95+ Log . Exception ( "Infrastructure.Http" , "Http Request Error" , e , "DownloadAsync" ) ;
96+ throw ;
8997 }
9098 }
9199
92100 /// <summary>
93101 /// Asynchrously get the result as string from url.
94- /// When supposing the result is long and large , try using GetStreamAsync to avoid reading as string
102+ /// When supposing the result larger than 83kb , try using GetStreamAsync to avoid reading as string
95103 /// </summary>
96104 /// <param name="url"></param>
97105 /// <returns></returns>
@@ -101,19 +109,33 @@ public static Task<string> GetAsync([NotNull] string url)
101109 return GetAsync ( new Uri ( url . Replace ( "#" , "%23" ) ) ) ;
102110 }
103111
112+ /// <summary>
113+ /// Asynchrously get the result as string from url.
114+ /// When supposing the result larger than 83kb, try using GetStreamAsync to avoid reading as string
115+ /// </summary>
116+ /// <param name="url"></param>
117+ /// <returns></returns>
104118 public static async Task < string > GetAsync ( [ NotNull ] Uri url )
105119 {
106120 Log . Debug ( $ "|Http.Get|Url <{ url } >") ;
107- using var response = await client . GetAsync ( url ) ;
108- var content = await response . Content . ReadAsStringAsync ( ) ;
109- if ( response . StatusCode == HttpStatusCode . OK )
121+ try
110122 {
111- return content ;
123+ using var response = await client . GetAsync ( url ) ;
124+ var content = await response . Content . ReadAsStringAsync ( ) ;
125+ if ( response . StatusCode == HttpStatusCode . OK )
126+ {
127+ return content ;
128+ }
129+ else
130+ {
131+ throw new HttpRequestException (
132+ $ "Error code <{ response . StatusCode } > with content <{ content } > returned from <{ url } >") ;
133+ }
112134 }
113- else
135+ catch ( HttpRequestException e )
114136 {
115- throw new HttpRequestException (
116- $ "Error code < { response . StatusCode } > with content < { content } > returned from < { url } >" ) ;
137+ Log . Exception ( "Infrastructure.Http" , "Http Request Error" , e , "GetAsync" ) ;
138+ throw ;
117139 }
118140 }
119141
@@ -124,9 +146,17 @@ public static async Task<string> GetAsync([NotNull] Uri url)
124146 /// <returns></returns>
125147 public static async Task < Stream > GetStreamAsync ( [ NotNull ] string url )
126148 {
127- Log . Debug ( $ "|Http.Get|Url <{ url } >") ;
128- var response = await client . GetAsync ( url ) ;
129- return await response . Content . ReadAsStreamAsync ( ) ;
149+ try
150+ {
151+ Log . Debug ( $ "|Http.Get|Url <{ url } >") ;
152+ var response = await client . GetAsync ( url ) ;
153+ return await response . Content . ReadAsStreamAsync ( ) ;
154+ }
155+ catch ( HttpRequestException e )
156+ {
157+ Log . Exception ( "Infrastructure.Http" , "Http Request Error" , e , "GetStreamAsync" ) ;
158+ throw ;
159+ }
130160 }
131161 }
132162}
0 commit comments