Files
RAGECOOP-V/Core/Networking/HttpHelper.cs
Sardelka9515 cac2385c35 Initial migration commit to .NET 7
Menu, sync and other stuff except resource system should be working.
We're far from finished
2023-01-28 20:51:29 +08:00

41 lines
1.6 KiB
C#

using System;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Threading;
namespace RageCoop.Core
{
internal static class HttpHelper
{
public static void DownloadFile(string url, string destination, Action<int> progressCallback = null)
{
if (File.Exists(destination)) File.Delete(destination);
var ae = new AutoResetEvent(false);
var client = new WebClient();
// TLS only
ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
client.DownloadProgressChanged += (s, e1) => progressCallback?.Invoke(e1.ProgressPercentage);
client.DownloadFileCompleted += (s, e2) => { ae.Set(); };
client.DownloadFileAsync(new Uri(url), destination);
ae.WaitOne();
}
public static string DownloadString(string url)
{
// TLS only
ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 |
SecurityProtocolType.Tls11 |
SecurityProtocolType.Tls;
ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
var client = new HttpClient();
return client.GetStringAsync(url).GetAwaiter().GetResult();
}
}
}