Skip to content

Commit a16cc5b

Browse files
committed
Move Old HttpWebRequest to HttpClient Instance (solve connection timeout)
1 parent 135f63a commit a16cc5b

File tree

2 files changed

+49
-31
lines changed

2 files changed

+49
-31
lines changed

Flow.Launcher.Core/Updater.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ private async Task<UpdateManager> GitHubUpdateManager(string repository)
139139
var latest = releases.Where(r => !r.Prerelease).OrderByDescending(r => r.PublishedAt).First();
140140
var latestUrl = latest.HtmlUrl.Replace("/tag/", "/download/");
141141

142-
var client = new WebClient { Proxy = Http.WebProxy() };
142+
var client = new WebClient { Proxy = Http.WebProxy };
143143
var downloader = new FileDownloader(client);
144144

145145
var manager = new UpdateManager(latestUrl, urlDownloader: downloader);

Flow.Launcher.Infrastructure/Http/Http.cs

Lines changed: 48 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -6,77 +6,95 @@
66
using JetBrains.Annotations;
77
using Flow.Launcher.Infrastructure.Logger;
88
using Flow.Launcher.Infrastructure.UserSettings;
9+
using System;
910

1011
namespace Flow.Launcher.Infrastructure.Http
1112
{
1213
public static class Http
1314
{
1415
private const string UserAgent = @"Mozilla/5.0 (Trident/7.0; rv:11.0) like Gecko";
1516

17+
private static HttpClient client;
18+
private static SocketsHttpHandler socketsHttpHandler = new SocketsHttpHandler()
19+
{
20+
UseProxy = true,
21+
Proxy = WebProxy
22+
};
23+
1624
static Http()
1725
{
1826
// need to be added so it would work on a win10 machine
1927
ServicePointManager.Expect100Continue = true;
2028
ServicePointManager.SecurityProtocol |= SecurityProtocolType.Tls
2129
| SecurityProtocolType.Tls11
2230
| 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+
}
2347
}
2448

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
2756
{
2857
if (Proxy != null && Proxy.Enabled && !string.IsNullOrEmpty(Proxy.Server))
2958
{
3059
if (string.IsNullOrEmpty(Proxy.UserName) || string.IsNullOrEmpty(Proxy.Password))
3160
{
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;
3463
}
3564
else
3665
{
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);
4268
}
4369
}
4470
else
4571
{
46-
return WebRequest.GetSystemWebProxy();
72+
WebProxy.Address = new WebProxy().Address;
73+
WebProxy.Credentials = null;
4774
}
4875
}
4976

5077
public static void Download([NotNull] string url, [NotNull] string filePath)
5178
{
52-
var client = new WebClient { Proxy = WebProxy() };
79+
var client = new WebClient { Proxy = WebProxy };
5380
client.Headers.Add("user-agent", UserAgent);
5481
client.DownloadFile(url, filePath);
5582
}
5683

5784
public static async Task<string> Get([NotNull] string url, string encoding = "UTF-8")
5885
{
5986
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)
7092
{
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}>");
8098
}
8199
}
82100
}

0 commit comments

Comments
 (0)