1
1
using System . Net ;
2
+ using System . Net . Http ;
3
+ using System . Threading . Tasks ;
2
4
using CommunityToolkit . Mvvm . Input ;
3
5
using Flow . Launcher . Core ;
4
6
using Flow . Launcher . Infrastructure . UserSettings ;
@@ -8,49 +10,43 @@ namespace Flow.Launcher.SettingPages.ViewModels;
8
10
9
11
public partial class SettingsPaneProxyViewModel : BaseModel
10
12
{
11
- private readonly Updater _updater ;
12
13
public Settings Settings { get ; }
13
14
15
+ private readonly Updater _updater ;
16
+
14
17
public SettingsPaneProxyViewModel ( Settings settings , Updater updater )
15
18
{
16
- _updater = updater ;
17
19
Settings = settings ;
20
+ _updater = updater ;
18
21
}
19
22
20
23
[ RelayCommand ]
21
- private void OnTestProxyClicked ( )
24
+ private async Task OnTestProxyClickedAsync ( )
22
25
{
23
- var message = TestProxy ( ) ;
26
+ var message = await TestProxyAsync ( ) ;
24
27
App . API . ShowMsgBox ( App . API . GetTranslation ( message ) ) ;
25
28
}
26
29
27
- private string TestProxy ( )
30
+ private async Task < string > TestProxyAsync ( )
28
31
{
29
32
if ( string . IsNullOrEmpty ( Settings . Proxy . Server ) ) return "serverCantBeEmpty" ;
30
33
if ( Settings . Proxy . Port <= 0 ) return "portCantBeEmpty" ;
31
34
32
- HttpWebRequest request = ( HttpWebRequest ) WebRequest . Create ( _updater . GitHubRepository ) ;
33
-
34
- if ( string . IsNullOrEmpty ( Settings . Proxy . UserName ) || string . IsNullOrEmpty ( Settings . Proxy . Password ) )
35
+ var handler = new HttpClientHandler
35
36
{
36
- request . Proxy = new WebProxy ( Settings . Proxy . Server , Settings . Proxy . Port ) ;
37
- }
38
- 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 ) )
39
41
{
40
- request . Proxy = new WebProxy ( Settings . Proxy . Server , Settings . Proxy . Port )
41
- {
42
- Credentials = new NetworkCredential ( Settings . Proxy . UserName , Settings . Proxy . Password )
43
- } ;
42
+ handler . Proxy . Credentials = new NetworkCredential ( Settings . Proxy . UserName , Settings . Proxy . Password ) ;
44
43
}
45
44
45
+ using var client = new HttpClient ( handler ) ;
46
46
try
47
47
{
48
- var response = ( HttpWebResponse ) request . GetResponse ( ) ;
49
- return response . StatusCode switch
50
- {
51
- HttpStatusCode . OK => "proxyIsCorrect" ,
52
- _ => "proxyConnectFailed"
53
- } ;
48
+ var response = await client . GetAsync ( _updater . GitHubRepository ) ;
49
+ return response . IsSuccessStatusCode ? "proxyIsCorrect" : "proxyConnectFailed" ;
54
50
}
55
51
catch
56
52
{
0 commit comments