Optimize file download

pull/3234/head
2dust 2 years ago
parent 7eb869ab1d
commit ec59249d79

@ -13,6 +13,62 @@ namespace v2rayN.Base
{
}
public async Task<string> DownloadStringAsync(IWebProxy webProxy, string url, string? userAgent, int timeout)
{
if (string.IsNullOrEmpty(url))
{
return null;
}
var cancellationToken = new CancellationTokenSource();
cancellationToken.CancelAfter(timeout * 1000);
Uri uri = new Uri(url);
//Authorization Header
var headers = new WebHeaderCollection();
if (!Utils.IsNullOrEmpty(uri.UserInfo))
{
headers.Add(HttpRequestHeader.Authorization, "Basic " + Utils.Base64Encode(uri.UserInfo));
}
var downloadOpt = new DownloadConfiguration()
{
Timeout = timeout * 1000,
MaxTryAgainOnFailover = 2,
RequestConfiguration =
{
Headers = headers,
UserAgent = userAgent,
Timeout = timeout * 1000,
Proxy = webProxy
}
};
string text = string.Empty;
using (var downloader = new DownloadService(downloadOpt))
{
downloader.DownloadFileCompleted += (sender, value) =>
{
if (value.Error != null)
{
throw new Exception(string.Format("{0}", value.Error.Message));
}
};
using (var stream = await downloader.DownloadFileTaskAsync(address: url, cancellationToken: cancellationToken.Token))
{
using (StreamReader reader = new StreamReader(stream))
{
text = reader.ReadToEnd();
}
}
}
downloadOpt = null;
return text;
}
public async Task DownloadDataAsync4Speed(IWebProxy webProxy, string url, IProgress<string> progress, int timeout)
{
if (string.IsNullOrEmpty(url))
@ -38,7 +94,8 @@ namespace v2rayN.Base
int totalSecond = 0;
var hasValue = false;
double maxSpeed = 0;
var downloader = new DownloadService(downloadOpt);
using (var downloader = new DownloadService(downloadOpt))
{
//downloader.DownloadStarted += (sender, value) =>
//{
// if (progress != null)
@ -74,16 +131,8 @@ namespace v2rayN.Base
progress.Report("......");
await downloader.DownloadFileTaskAsync(address: url, cancellationToken: cancellationToken.Token);
//var stream = await downloader.DownloadFileTaskAsync(url);
//using (StreamReader reader = new StreamReader(stream))
//{
// string text = reader.ReadToEnd();
// stream.Dispose();
//}
}
downloader.Dispose();
downloader = null;
downloadOpt = null;
}
@ -118,7 +167,8 @@ namespace v2rayN.Base
var progressPercentage = 0;
var hasValue = false;
var downloader = new DownloadService(downloadOpt);
using (var downloader = new DownloadService(downloadOpt))
{
downloader.DownloadStarted += (sender, value) =>
{
if (progress != null)
@ -148,9 +198,8 @@ namespace v2rayN.Base
};
await downloader.DownloadFileTaskAsync(url, fileName, cancellationToken: cancellationToken.Token);
}
downloader.Dispose();
downloader = null;
downloadOpt = null;
}
}

@ -2,7 +2,6 @@
using System.IO;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Net.Sockets;
using v2rayN.Base;
using v2rayN.Resx;
@ -80,7 +79,8 @@ namespace v2rayN.Handler
}
};
_ = DownloaderHelper.Instance.DownloadFileAsync(GetWebProxy(blProxy),
var webProxy = GetWebProxy(blProxy);
_ = DownloaderHelper.Instance.DownloadFileAsync(webProxy,
url,
Utils.GetTempPath(Utils.GetDownloadFileName(url)),
progress,
@ -129,28 +129,9 @@ namespace v2rayN.Handler
try
{
Utils.SetSecurityProtocol(LazyConfig.Instance.GetConfig().enableSecurityProtocolTls13);
var client = new HttpClient(new SocketsHttpHandler()
{
Proxy = GetWebProxy(blProxy)
});
if (Utils.IsNullOrEmpty(userAgent))
{
userAgent = $"{Utils.GetVersion(false)}";
}
client.DefaultRequestHeaders.UserAgent.TryParseAdd(userAgent);
Uri uri = new Uri(url);
//Authorization Header
if (!Utils.IsNullOrEmpty(uri.UserInfo))
{
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Utils.Base64Encode(uri.UserInfo));
}
var cts = new CancellationTokenSource();
cts.CancelAfter(1000 * 30);
var result = await HttpClientHelper.GetInstance().GetAsync(client, url, cts.Token);
var webProxy = GetWebProxy(blProxy);
var result = await DownloaderHelper.Instance.DownloadStringAsync(webProxy, url, userAgent, 30);
return result;
}
catch (Exception ex)

Loading…
Cancel
Save