@@ -141,9 +141,7 @@ public static string HttpGet(
141141#else
142142 var handler = HttpClientHelper . GetHttpClientHandler ( null , SenparcHttpClientWebProxy , DecompressionMethods . GZip ) ;
143143
144-
145144 HttpClient httpClient = serviceProvider . GetRequiredService < SenparcHttpClient > ( ) . Client ;
146-
147145 return httpClient . GetStringAsync ( url ) . Result ;
148146#endif
149147 }
@@ -188,11 +186,18 @@ public static string HttpGet(
188186
189187 var httpClient = HttpGet_Common_NetCore ( serviceProvider , url , cookieContainer , encoding , cer , refererUrl , useAjax , headerAddition , timeOut ) ;
190188
191- var response = httpClient . GetAsync ( url ) . GetAwaiter ( ) . GetResult ( ) ; //获取响应信息
189+ using ( var cts = new System . Threading . CancellationTokenSource ( timeOut ) )
190+ {
191+ try
192+ {
193+ var response = httpClient . GetAsync ( url , cancellationToken : cts . Token ) . GetAwaiter ( ) . GetResult ( ) ; //获取响应信息
192194
193- HttpClientHelper . SetResponseCookieContainer ( cookieContainer , response ) ; //设置 Cookie
195+ HttpClientHelper . SetResponseCookieContainer ( cookieContainer , response ) ; //设置 Cookie
194196
195- return response . Content . ReadAsStringAsync ( ) . GetAwaiter ( ) . GetResult ( ) ;
197+ return response . Content . ReadAsStringAsync ( ) . GetAwaiter ( ) . GetResult ( ) ;
198+ }
199+ catch { throw ; }
200+ }
196201#endif
197202 }
198203
@@ -240,15 +245,22 @@ public static HttpWebResponse HttpResponseGet(string url, CookieContainer cookie
240245 public static HttpResponseMessage HttpResponseGet (
241246 IServiceProvider serviceProvider ,
242247 string url , CookieContainer cookieContainer = null , Encoding encoding = null , X509Certificate2 cer = null ,
243- string refererUrl = null , bool useAjax = false , Dictionary < string , string > headerAddition = null , int timeOut = Config . TIME_OUT )
248+ string refererUrl = null , bool useAjax = false , Dictionary < string , string > headerAddition = null , int timeOut = Config . TIME_OUT )
244249 {
245250 var httpClient = HttpGet_Common_NetCore ( serviceProvider , url , cookieContainer , encoding , cer , refererUrl , useAjax , headerAddition , timeOut ) ;
246- var task = httpClient . GetAsync ( url ) ;
247- HttpResponseMessage response = task . Result ;
251+ using ( var cts = new System . Threading . CancellationTokenSource ( timeOut ) )
252+ {
253+ try
254+ {
255+ var task = httpClient . GetAsync ( url , cancellationToken : cts . Token ) ;
256+ HttpResponseMessage response = task . Result ;
248257
249- HttpClientHelper . SetResponseCookieContainer ( cookieContainer , response ) ; //设置 Cookie
258+ HttpClientHelper . SetResponseCookieContainer ( cookieContainer , response ) ; //设置 Cookie
250259
251- return response ;
260+ return response ;
261+ }
262+ catch { throw ; }
263+ }
252264 }
253265
254266#endif
@@ -325,13 +337,20 @@ public static async Task<string> HttpGetAsync(
325337#else
326338 var httpClient = HttpGet_Common_NetCore ( serviceProvider , url , cookieContainer , encoding , cer , refererUrl , useAjax , headerAddition , timeOut ) ;
327339
328- var response = await httpClient . GetAsync ( url ) . ConfigureAwait ( false ) ; //获取响应信息
340+ using ( var cts = new System . Threading . CancellationTokenSource ( timeOut ) )
341+ {
342+ try
343+ {
344+ var response = await httpClient . GetAsync ( url , cancellationToken : cts . Token ) . ConfigureAwait ( false ) ; //获取响应信息
329345
330- HttpClientHelper . SetResponseCookieContainer ( cookieContainer , response ) ; //设置 Cookie
346+ HttpClientHelper . SetResponseCookieContainer ( cookieContainer , response ) ; //设置 Cookie
331347
332- var retString = await response . Content . ReadAsStringAsync ( ) . ConfigureAwait ( false ) ;
348+ var retString = await response . Content . ReadAsStringAsync ( ) . ConfigureAwait ( false ) ;
333349
334- return retString ;
350+ return retString ;
351+ }
352+ catch { throw ; }
353+ }
335354#endif
336355 }
337356
@@ -379,15 +398,22 @@ public static async Task<HttpWebResponse> HttpResponseGetAsync(string url, Cooki
379398 public static async Task < HttpResponseMessage > HttpResponseGetAsync (
380399 IServiceProvider serviceProvider ,
381400 string url , CookieContainer cookieContainer = null , Encoding encoding = null , X509Certificate2 cer = null ,
382- string refererUrl = null , bool useAjax = false , Dictionary < string , string > headerAddition = null , int timeOut = Config . TIME_OUT )
401+ string refererUrl = null , bool useAjax = false , Dictionary < string , string > headerAddition = null , int timeOut = Config . TIME_OUT )
383402 {
384403 var httpClient = HttpGet_Common_NetCore ( serviceProvider , url , cookieContainer , encoding , cer , refererUrl , useAjax , headerAddition , timeOut ) ;
385- var task = httpClient . GetAsync ( url ) ;
386- HttpResponseMessage response = await task ;
404+ using ( var cts = new System . Threading . CancellationTokenSource ( timeOut ) )
405+ {
406+ try
407+ {
408+ var task = httpClient . GetAsync ( url , cancellationToken : cts . Token ) ;
409+ HttpResponseMessage response = await task ;
387410
388- HttpClientHelper . SetResponseCookieContainer ( cookieContainer , response ) ; //设置 Cookie
411+ HttpClientHelper . SetResponseCookieContainer ( cookieContainer , response ) ; //设置 Cookie
389412
390- return response ;
413+ return response ;
414+ }
415+ catch { throw ; }
416+ }
391417 }
392418
393419#endif
0 commit comments