Skip to content

Commit c55e889

Browse files
committed
Change Download to HttpClient as well (which change it to async as well)
1 parent bfd1c9a commit c55e889

File tree

1 file changed

+13
-5
lines changed
  • Flow.Launcher.Infrastructure/Http

1 file changed

+13
-5
lines changed

Flow.Launcher.Infrastructure/Http/Http.cs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ static Http()
2929
| SecurityProtocolType.Tls11
3030
| SecurityProtocolType.Tls12;
3131

32+
client = new HttpClient(socketsHttpHandler, false);
3233
client.DefaultRequestHeaders.Add("User-Agent", UserAgent);
3334

3435
}
@@ -46,7 +47,7 @@ private get
4647
}
4748
}
4849

49-
public static WebProxy WebProxy { get; private set; }
50+
public static WebProxy WebProxy { get; private set; } = new WebProxy();
5051

5152
/// <summary>
5253
/// Update the Address of the Proxy to modify the client Proxy
@@ -74,11 +75,18 @@ public static void UpdateProxy()
7475
}
7576
}
7677

77-
public static void Download([NotNull] string url, [NotNull] string filePath)
78+
public async static Task Download([NotNull] string url, [NotNull] string filePath)
7879
{
79-
var client = new WebClient { Proxy = WebProxy };
80-
client.Headers.Add("user-agent", UserAgent);
81-
client.DownloadFile(url, filePath);
80+
using var response = await client.GetAsync(url);
81+
if (response.StatusCode == HttpStatusCode.OK)
82+
{
83+
using var fileStream = new FileStream(filePath, FileMode.CreateNew);
84+
await response.Content.CopyToAsync(fileStream);
85+
}
86+
else
87+
{
88+
throw new WebException($"Error code <{response.StatusCode}> returned from <{url}>");
89+
}
8290
}
8391

8492
public static async Task<string> Get([NotNull] string url, string encoding = "UTF-8")

0 commit comments

Comments
 (0)