Skip to content

Commit 4ea5dd5

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

File tree

2 files changed

+48
-31
lines changed

2 files changed

+48
-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: 47 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -13,70 +13,87 @@ public static class Http
1313
{
1414
private const string UserAgent = @"Mozilla/5.0 (Trident/7.0; rv:11.0) like Gecko";
1515

16+
private static HttpClient client;
17+
private static SocketsHttpHandler socketsHttpHandler = new SocketsHttpHandler()
18+
{
19+
UseProxy = true,
20+
Proxy = WebProxy
21+
};
22+
1623
static Http()
1724
{
1825
// need to be added so it would work on a win10 machine
1926
ServicePointManager.Expect100Continue = true;
2027
ServicePointManager.SecurityProtocol |= SecurityProtocolType.Tls
2128
| SecurityProtocolType.Tls11
2229
| 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+
}
2346
}
2447

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
2755
{
2856
if (Proxy != null && Proxy.Enabled && !string.IsNullOrEmpty(Proxy.Server))
2957
{
3058
if (string.IsNullOrEmpty(Proxy.UserName) || string.IsNullOrEmpty(Proxy.Password))
3159
{
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;
3462
}
3563
else
3664
{
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);
4267
}
4368
}
4469
else
4570
{
46-
return WebRequest.GetSystemWebProxy();
71+
WebProxy.Address = new WebProxy().Address;
72+
WebProxy.Credentials = null;
4773
}
4874
}
4975

5076
public static void Download([NotNull] string url, [NotNull] string filePath)
5177
{
52-
var client = new WebClient { Proxy = WebProxy() };
78+
var client = new WebClient { Proxy = WebProxy };
5379
client.Headers.Add("user-agent", UserAgent);
5480
client.DownloadFile(url, filePath);
5581
}
5682

5783
public static async Task<string> Get([NotNull] string url, string encoding = "UTF-8")
5884
{
5985
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)
7091
{
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}>");
8097
}
8198
}
8299
}

0 commit comments

Comments
 (0)