@@ -13,70 +13,87 @@ public static class Http
13
13
{
14
14
private const string UserAgent = @"Mozilla/5.0 (Trident/7.0; rv:11.0) like Gecko" ;
15
15
16
+ private static HttpClient client ;
17
+ private static SocketsHttpHandler socketsHttpHandler = new SocketsHttpHandler ( )
18
+ {
19
+ UseProxy = true ,
20
+ Proxy = WebProxy
21
+ } ;
22
+
16
23
static Http ( )
17
24
{
18
25
// need to be added so it would work on a win10 machine
19
26
ServicePointManager . Expect100Continue = true ;
20
27
ServicePointManager . SecurityProtocol |= SecurityProtocolType . Tls
21
28
| SecurityProtocolType . Tls11
22
29
| SecurityProtocolType . Tls12 ;
30
+
31
+ client . DefaultRequestHeaders . Add ( "User-Agent" , UserAgent ) ;
32
+
33
+ }
34
+ private static HttpProxy proxy ;
35
+ public static HttpProxy Proxy
36
+ {
37
+ private get
38
+ {
39
+ return proxy ;
40
+ }
41
+ set
42
+ {
43
+ proxy = value ;
44
+ UpdateProxy ( ) ;
45
+ }
23
46
}
24
47
25
- public static HttpProxy Proxy { private get ; set ; }
26
- public static IWebProxy WebProxy ( )
48
+ public static WebProxy WebProxy { get ; private set ; }
49
+
50
+ /// <summary>
51
+ /// Update the Address of the Proxy to modify the client Proxy
52
+ /// </summary>
53
+ public static void UpdateProxy ( )
54
+ // TODO: need test with a proxy
27
55
{
28
56
if ( Proxy != null && Proxy . Enabled && ! string . IsNullOrEmpty ( Proxy . Server ) )
29
57
{
30
58
if ( string . IsNullOrEmpty ( Proxy . UserName ) || string . IsNullOrEmpty ( Proxy . Password ) )
31
59
{
32
- var webProxy = new WebProxy ( Proxy . Server , Proxy . Port ) ;
33
- return webProxy ;
60
+ WebProxy . Address = new Uri ( $ "http:// { Proxy . Server } : { Proxy . Port } " ) ;
61
+ WebProxy . Credentials = null ;
34
62
}
35
63
else
36
64
{
37
- var webProxy = new WebProxy ( Proxy . Server , Proxy . Port )
38
- {
39
- Credentials = new NetworkCredential ( Proxy . UserName , Proxy . Password )
40
- } ;
41
- return webProxy ;
65
+ WebProxy . Address = new Uri ( $ "http://{ Proxy . Server } :{ Proxy . Port } ") ;
66
+ WebProxy . Credentials = new NetworkCredential ( Proxy . UserName , Proxy . Password ) ;
42
67
}
43
68
}
44
69
else
45
70
{
46
- return WebRequest . GetSystemWebProxy ( ) ;
71
+ WebProxy . Address = new WebProxy ( ) . Address ;
72
+ WebProxy . Credentials = null ;
47
73
}
48
74
}
49
75
50
76
public static void Download ( [ NotNull ] string url , [ NotNull ] string filePath )
51
77
{
52
- var client = new WebClient { Proxy = WebProxy ( ) } ;
78
+ var client = new WebClient { Proxy = WebProxy } ;
53
79
client . Headers . Add ( "user-agent" , UserAgent ) ;
54
80
client . DownloadFile ( url , filePath ) ;
55
81
}
56
82
57
83
public static async Task < string > Get ( [ NotNull ] string url , string encoding = "UTF-8" )
58
84
{
59
85
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 ( ) ;
68
-
69
- using ( var reader = new StreamReader ( stream , Encoding . GetEncoding ( encoding ) ) )
86
+ var response = await client . GetAsync ( url ) ;
87
+ using var stream = await response . Content . ReadAsStreamAsync ( ) ;
88
+ using var reader = new StreamReader ( stream , Encoding . GetEncoding ( encoding ) ) ;
89
+ var content = await reader . ReadToEndAsync ( ) ;
90
+ if ( response . StatusCode == HttpStatusCode . OK )
70
91
{
71
- var content = await reader . ReadToEndAsync ( ) ;
72
- if ( response . StatusCode == HttpStatusCode . OK )
73
- {
74
- return content ;
75
- }
76
- else
77
- {
78
- throw new HttpRequestException ( $ "Error code <{ response . StatusCode } > with content <{ content } > returned from <{ url } >") ;
79
- }
92
+ return content ;
93
+ }
94
+ else
95
+ {
96
+ throw new HttpRequestException ( $ "Error code <{ response . StatusCode } > with content <{ content } > returned from <{ url } >") ;
80
97
}
81
98
}
82
99
}
0 commit comments