Skip to content

Commit e23bab2

Browse files
committed
Use http client for code quality
1 parent 337c983 commit e23bab2

File tree

1 file changed

+14
-19
lines changed

1 file changed

+14
-19
lines changed

Flow.Launcher/SettingPages/ViewModels/SettingsPaneProxyViewModel.cs

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
using System.Net;
2+
using System.Net.Http;
3+
using System.Threading.Tasks;
24
using CommunityToolkit.Mvvm.Input;
35
using Flow.Launcher.Core;
46
using Flow.Launcher.Infrastructure.UserSettings;
@@ -19,39 +21,32 @@ public SettingsPaneProxyViewModel(Settings settings, Updater updater)
1921
}
2022

2123
[RelayCommand]
22-
private void OnTestProxyClicked()
24+
private async Task OnTestProxyClickedAsync()
2325
{
24-
var message = TestProxy();
26+
var message = await TestProxyAsync();
2527
App.API.ShowMsgBox(App.API.GetTranslation(message));
2628
}
2729

28-
private string TestProxy()
30+
private async Task<string> TestProxyAsync()
2931
{
3032
if (string.IsNullOrEmpty(Settings.Proxy.Server)) return "serverCantBeEmpty";
3133
if (Settings.Proxy.Port <= 0) return "portCantBeEmpty";
3234

33-
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(_updater.GitHubRepository);
34-
35-
if (string.IsNullOrEmpty(Settings.Proxy.UserName) || string.IsNullOrEmpty(Settings.Proxy.Password))
35+
var handler = new HttpClientHandler
3636
{
37-
request.Proxy = new WebProxy(Settings.Proxy.Server, Settings.Proxy.Port);
38-
}
39-
else
37+
Proxy = new WebProxy(Settings.Proxy.Server, Settings.Proxy.Port)
38+
};
39+
40+
if (!string.IsNullOrEmpty(Settings.Proxy.UserName) && !string.IsNullOrEmpty(Settings.Proxy.Password))
4041
{
41-
request.Proxy = new WebProxy(Settings.Proxy.Server, Settings.Proxy.Port)
42-
{
43-
Credentials = new NetworkCredential(Settings.Proxy.UserName, Settings.Proxy.Password)
44-
};
42+
handler.Proxy.Credentials = new NetworkCredential(Settings.Proxy.UserName, Settings.Proxy.Password);
4543
}
4644

45+
using var client = new HttpClient(handler);
4746
try
4847
{
49-
var response = (HttpWebResponse)request.GetResponse();
50-
return response.StatusCode switch
51-
{
52-
HttpStatusCode.OK => "proxyIsCorrect",
53-
_ => "proxyConnectFailed"
54-
};
48+
var response = await client.GetAsync(_updater.GitHubRepository);
49+
return response.IsSuccessStatusCode ? "proxyIsCorrect" : "proxyConnectFailed";
5550
}
5651
catch
5752
{

0 commit comments

Comments
 (0)