6
6
using JetBrains . Annotations ;
7
7
using Flow . Launcher . Infrastructure . Logger ;
8
8
using Flow . Launcher . Infrastructure . UserSettings ;
9
+ using System ;
10
+ using System . ComponentModel ;
9
11
10
12
namespace Flow . Launcher . Infrastructure . Http
11
13
{
12
14
public static class Http
13
15
{
14
16
private const string UserAgent = @"Mozilla/5.0 (Trident/7.0; rv:11.0) like Gecko" ;
15
17
18
+ private static HttpClient client ;
19
+
20
+ private static SocketsHttpHandler socketsHttpHandler = new SocketsHttpHandler ( )
21
+ {
22
+ UseProxy = true ,
23
+ Proxy = WebProxy
24
+ } ;
25
+
16
26
static Http ( )
17
27
{
18
28
// need to be added so it would work on a win10 machine
19
29
ServicePointManager . Expect100Continue = true ;
20
30
ServicePointManager . SecurityProtocol |= SecurityProtocolType . Tls
21
31
| SecurityProtocolType . Tls11
22
32
| SecurityProtocolType . Tls12 ;
33
+
34
+ client = new HttpClient ( socketsHttpHandler , false ) ;
35
+ client . DefaultRequestHeaders . Add ( "User-Agent" , UserAgent ) ;
23
36
}
24
37
25
- public static HttpProxy Proxy { private get ; set ; }
26
- public static IWebProxy WebProxy ( )
38
+ private static HttpProxy proxy ;
39
+
40
+ public static HttpProxy Proxy
27
41
{
28
- if ( Proxy != null && Proxy . Enabled && ! string . IsNullOrEmpty ( Proxy . Server ) )
42
+ private get { return proxy ; }
43
+ set
29
44
{
30
- if ( string . IsNullOrEmpty ( Proxy . UserName ) || string . IsNullOrEmpty ( Proxy . Password ) )
31
- {
32
- var webProxy = new WebProxy ( Proxy . Server , Proxy . Port ) ;
33
- return webProxy ;
34
- }
35
- else
45
+ proxy = value ;
46
+ proxy . PropertyChanged += UpdateProxy ;
47
+ }
48
+ }
49
+
50
+ public static WebProxy WebProxy { get ; } = new WebProxy ( ) ;
51
+
52
+ /// <summary>
53
+ /// Update the Address of the Proxy to modify the client Proxy
54
+ /// </summary>
55
+ public static void UpdateProxy ( ProxyProperty property )
56
+ {
57
+ ( WebProxy . Address , WebProxy . Credentials ) = property switch
58
+ {
59
+ ProxyProperty . Enabled => Proxy . Enabled switch
36
60
{
37
- var webProxy = new WebProxy ( Proxy . Server , Proxy . Port )
61
+ true => Proxy . UserName switch
38
62
{
39
- Credentials = new NetworkCredential ( Proxy . UserName , Proxy . Password )
40
- } ;
41
- return webProxy ;
42
- }
63
+ var userName when ! string . IsNullOrEmpty ( userName ) =>
64
+ ( new Uri ( $ "http://{ Proxy . Server } :{ Proxy . Port } ") , null ) ,
65
+ _ => ( new Uri ( $ "http://{ Proxy . Server } :{ Proxy . Port } ") ,
66
+ new NetworkCredential ( Proxy . UserName , Proxy . Password ) )
67
+ } ,
68
+ false => ( null , null )
69
+ } ,
70
+ ProxyProperty . Server => ( new Uri ( $ "http://{ Proxy . Server } :{ Proxy . Port } ") , WebProxy . Credentials ) ,
71
+ ProxyProperty . Port => ( new Uri ( $ "http://{ Proxy . Server } :{ Proxy . Port } ") , WebProxy . Credentials ) ,
72
+ ProxyProperty . UserName => ( WebProxy . Address , new NetworkCredential ( Proxy . UserName , Proxy . Password ) ) ,
73
+ ProxyProperty . Password => ( WebProxy . Address , new NetworkCredential ( Proxy . UserName , Proxy . Password ) ) ,
74
+ _ => throw new ArgumentOutOfRangeException ( )
75
+ } ;
76
+ }
77
+
78
+ public static async Task Download ( [ NotNull ] string url , [ NotNull ] string filePath )
79
+ {
80
+ using var response = await client . GetAsync ( url ) ;
81
+ if ( response . StatusCode == HttpStatusCode . OK )
82
+ {
83
+ await using var fileStream = new FileStream ( filePath , FileMode . CreateNew ) ;
84
+ await response . Content . CopyToAsync ( fileStream ) ;
43
85
}
44
86
else
45
87
{
46
- return WebRequest . GetSystemWebProxy ( ) ;
88
+ throw new HttpRequestException ( $ "Error code < { response . StatusCode } > returned from < { url } >" ) ;
47
89
}
48
90
}
49
91
50
- public static void Download ( [ NotNull ] string url , [ NotNull ] string filePath )
92
+ /// <summary>
93
+ /// 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
95
+ /// </summary>
96
+ /// <param name="url"></param>
97
+ /// <returns></returns>
98
+ public static Task < string > GetAsync ( [ NotNull ] string url )
51
99
{
52
- var client = new WebClient { Proxy = WebProxy ( ) } ;
53
- client . Headers . Add ( "user-agent" , UserAgent ) ;
54
- client . DownloadFile ( url , filePath ) ;
100
+ Log . Debug ( $ "|Http.Get|Url <{ url } >") ;
101
+ return GetAsync ( new Uri ( url . Replace ( "#" , "%23" ) ) ) ;
55
102
}
56
103
57
- public static async Task < string > Get ( [ NotNull ] string url , string encoding = "UTF-8" )
104
+ public static async Task < string > GetAsync ( [ NotNull ] Uri url )
58
105
{
59
106
Log . Debug ( $ "|Http.Get|Url <{ url } >") ;
60
- var request = WebRequest . CreateHttp ( url ) ;
61
- request . Method = "GET" ;
62
- request . Timeout = 1000 ;
63
- request . Proxy = WebProxy ( ) ;
64
- request . UserAgent = UserAgent ;
65
- var response = await request . GetResponseAsync ( ) as HttpWebResponse ;
66
- response = response . NonNull ( ) ;
67
- var stream = response . GetResponseStream ( ) . NonNull ( ) ;
107
+ using var response = await client . GetAsync ( url ) ;
108
+ var content = await response . Content . ReadAsStringAsync ( ) ;
109
+ if ( response . StatusCode == HttpStatusCode . OK )
110
+ {
111
+ return content ;
112
+ }
113
+ else
114
+ {
115
+ throw new HttpRequestException (
116
+ $ "Error code <{ response . StatusCode } > with content <{ content } > returned from <{ url } >") ;
117
+ }
118
+ }
68
119
69
- using var reader = new StreamReader ( stream , Encoding . GetEncoding ( encoding ) ) ;
70
- var content = await reader . ReadToEndAsync ( ) ;
71
- if ( response . StatusCode != HttpStatusCode . OK )
72
- throw new HttpRequestException ( $ "Error code <{ response . StatusCode } > with content <{ content } > returned from <{ url } >") ;
73
-
74
- return content ;
120
+ /// <summary>
121
+ /// Asynchrously get the result as stream from url.
122
+ /// </summary>
123
+ /// <param name="url"></param>
124
+ /// <returns></returns>
125
+ public static async Task < Stream > GetStreamAsync ( [ NotNull ] string url )
126
+ {
127
+ Log . Debug ( $ "|Http.Get|Url <{ url } >") ;
128
+ var response = await client . GetAsync ( url ) ;
129
+ return await response . Content . ReadAsStreamAsync ( ) ;
75
130
}
76
131
}
77
- }
132
+ }
0 commit comments