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