@@ -7,48 +7,45 @@ namespace CG.Web.MegaApiClient
77 using System . Reflection ;
88 using System . Text ;
99 using System . Threading ;
10+ #if NET471 || NETSTANDARD
1011 using System . Security . Authentication ;
11-
12+ #endif
1213 using System . Net . Http ;
1314 using System . Net . Http . Headers ;
1415
1516 public class WebClient : IWebClient
1617 {
1718 private const int DefaultResponseTimeout = Timeout . Infinite ;
1819
20+ private static readonly HttpClient s_sharedHttpClient = CreateHttpClient ( DefaultResponseTimeout , GenerateUserAgent ( ) ) ;
21+
1922 private readonly HttpClient _httpClient ;
2023
2124 public WebClient ( int responseTimeout = DefaultResponseTimeout , ProductInfoHeaderValue userAgent = null )
22- : this ( responseTimeout , userAgent , false )
23- {
24- }
25-
26- internal WebClient ( int responseTimeout , ProductInfoHeaderValue userAgent , bool connectionClose )
2725 {
28- BufferSize = Options . DefaultBufferSize ;
29- #if NET471 || NETSTANDARD
30- _httpClient = new HttpClient ( new HttpClientHandler { SslProtocols = SslProtocols . Tls12 } , true ) ;
31- #elif NET47
26+ #if NET47
3227 if ( ! ServicePointManager . SecurityProtocol . HasFlag ( SecurityProtocolType . Tls12 ) && ! ServicePointManager . SecurityProtocol . HasFlag ( SecurityProtocolType . SystemDefault ) )
3328 {
3429 throw new NotSupportedException ( "mega.nz API requires support for TLS v1.2 or higher. Check https://gpailler.github.io/MegaApiClient/#compatibility for additional information" ) ;
3530 }
36-
37- _httpClient = new HttpClient ( ) ;
38- #else
31+ #elif NET45 || NET46
3932 if ( ! ServicePointManager . SecurityProtocol . HasFlag ( SecurityProtocolType . Tls12 ) )
4033 {
4134 throw new NotSupportedException ( "mega.nz API requires support for TLS v1.2 or higher. Check https://gpailler.github.io/MegaApiClient/#compatibility for additional information" ) ;
4235 }
43-
44- _httpClient = new HttpClient ( ) ;
4536#endif
46- _httpClient . Timeout = TimeSpan . FromMilliseconds ( responseTimeout ) ;
47- _httpClient . DefaultRequestHeaders . UserAgent . Add ( userAgent ?? GenerateUserAgent ( ) ) ;
48- _httpClient . DefaultRequestHeaders . ConnectionClose = connectionClose ;
37+
38+ if ( responseTimeout == DefaultResponseTimeout && userAgent == null )
39+ {
40+ _httpClient = s_sharedHttpClient ;
41+ }
42+ else
43+ {
44+ _httpClient = CreateHttpClient ( responseTimeout , userAgent ?? GenerateUserAgent ( ) ) ;
45+ }
4946 }
5047
51- public int BufferSize { get ; set ; }
48+ public int BufferSize { get ; set ; } = Options . DefaultBufferSize ;
5249
5350 public string PostRequestJson ( Uri url , string jsonData )
5451 {
@@ -112,9 +109,23 @@ private string StreamToString(Stream stream)
112109 }
113110 }
114111
115- private ProductInfoHeaderValue GenerateUserAgent ( )
112+ private static HttpClient CreateHttpClient ( int timeout , ProductInfoHeaderValue userAgent )
113+ {
114+ #if NET471 || NETSTANDARD
115+ var httpClient = new HttpClient ( new HttpClientHandler { SslProtocols = SslProtocols . Tls12 } , true ) ;
116+ #else
117+ var httpClient = new HttpClient ( ) ;
118+ #endif
119+
120+ httpClient . Timeout = TimeSpan . FromMilliseconds ( timeout ) ;
121+ httpClient . DefaultRequestHeaders . UserAgent . Add ( userAgent ) ;
122+
123+ return httpClient ;
124+ }
125+
126+ private static ProductInfoHeaderValue GenerateUserAgent ( )
116127 {
117- var assemblyName = GetType ( ) . GetTypeInfo ( ) . Assembly . GetName ( ) ;
128+ var assemblyName = typeof ( WebClient ) . GetTypeInfo ( ) . Assembly . GetName ( ) ;
118129 return new ProductInfoHeaderValue ( assemblyName . Name , assemblyName . Version . ToString ( 2 ) ) ;
119130 }
120131 }
0 commit comments