|
|
|
@ -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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|