2022-08-21 15:13:36 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Net;
|
|
|
|
|
using System.Threading;
|
2022-08-21 19:13:12 +08:00
|
|
|
|
using System.IO;
|
2022-08-21 15:13:36 +08:00
|
|
|
|
|
|
|
|
|
namespace RageCoop.Core
|
|
|
|
|
{
|
|
|
|
|
internal static class HttpHelper
|
|
|
|
|
{
|
|
|
|
|
public static void DownloadFile(string url,string destination,Action<int> progressCallback)
|
|
|
|
|
{
|
2022-08-21 19:13:12 +08:00
|
|
|
|
if (File.Exists(destination)) { File.Delete(destination); }
|
2022-08-21 15:13:36 +08:00
|
|
|
|
AutoResetEvent ae=new AutoResetEvent(false);
|
|
|
|
|
WebClient client = new WebClient();
|
|
|
|
|
|
|
|
|
|
// TLS only
|
|
|
|
|
ServicePointManager.Expect100Continue = true;
|
|
|
|
|
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
|
|
|
|
|
ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
|
|
|
|
|
|
2022-08-21 19:13:12 +08:00
|
|
|
|
client.DownloadProgressChanged += (s, e1) => progressCallback?.Invoke(e1.ProgressPercentage);
|
2022-08-21 15:13:36 +08:00
|
|
|
|
client.DownloadFileCompleted += (s, e2) =>
|
|
|
|
|
{
|
|
|
|
|
ae.Set();
|
|
|
|
|
};
|
|
|
|
|
client.DownloadFileAsync(new Uri(url), destination);
|
|
|
|
|
ae.WaitOne();
|
|
|
|
|
}
|
2022-08-21 19:13:12 +08:00
|
|
|
|
public static string DownloadString(string url)
|
|
|
|
|
{
|
|
|
|
|
// TLS only
|
|
|
|
|
ServicePointManager.Expect100Continue = true;
|
2022-09-05 13:02:09 +02:00
|
|
|
|
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 |
|
|
|
|
|
SecurityProtocolType.Tls11 |
|
|
|
|
|
SecurityProtocolType.Tls;
|
2022-08-21 19:13:12 +08:00
|
|
|
|
ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
|
|
|
|
|
|
|
|
|
|
WebClient client = new WebClient();
|
|
|
|
|
return client.DownloadString(url);
|
|
|
|
|
}
|
2022-08-21 15:13:36 +08:00
|
|
|
|
}
|
|
|
|
|
}
|