diff --git a/v2rayN/v2rayN/Handler/DownloadHandle.cs b/v2rayN/v2rayN/Handler/DownloadHandle.cs index e2970659..04d777b4 100644 --- a/v2rayN/v2rayN/Handler/DownloadHandle.cs +++ b/v2rayN/v2rayN/Handler/DownloadHandle.cs @@ -62,7 +62,7 @@ namespace v2rayN.Handler } return ws; } - + void ws_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e) { if (UpdateCompleted != null) @@ -108,7 +108,7 @@ namespace v2rayN.Handler if (e.Error == null || Utils.IsNullOrEmpty(e.Error.ToString())) { - + ((WebClientEx)sender).Dispose(); TimeSpan ts = (DateTime.Now - totalDatetime); string speed = string.Format("{0} M/s", (totalBytesToReceive / ts.TotalMilliseconds / 1000).ToString("#0.0")); UpdateCompleted(this, new ResultEventArgs(true, speed.PadLeft(8, ' '))); @@ -189,5 +189,34 @@ namespace v2rayN.Handler } } + public WebClientEx DownloadDataAsync(string url, WebProxy webProxy, int downloadTimeout) + { + WebClientEx ws = new WebClientEx(); + try + { + Utils.SetSecurityProtocol(); + UpdateCompleted?.Invoke(this, new ResultEventArgs(false, UIRes.I18N("Downloading"))); + + progressPercentage = -1; + totalBytesToReceive = 0; + + DownloadTimeout = downloadTimeout; + if (webProxy != null) + { + ws.Proxy = webProxy; + } + + ws.DownloadProgressChanged += ws_DownloadProgressChanged; + ws.DownloadDataCompleted += ws_DownloadFileCompleted; + ws.DownloadDataAsync(new Uri(url)); + } + catch (Exception ex) + { + Utils.SaveLog(ex.Message, ex); + + Error?.Invoke(this, new ErrorEventArgs(ex)); + } + return ws; + } } } diff --git a/v2rayN/v2rayN/Handler/SpeedtestHandler.cs b/v2rayN/v2rayN/Handler/SpeedtestHandler.cs index 09fc71e7..084e09c0 100644 --- a/v2rayN/v2rayN/Handler/SpeedtestHandler.cs +++ b/v2rayN/v2rayN/Handler/SpeedtestHandler.cs @@ -220,11 +220,12 @@ namespace v2rayN.Handler int httpPort = _config.GetLocalPort("speedtest"); WebProxy webProxy = new WebProxy(Global.Loopback, httpPort + itemIndex); - var ws = downloadHandle2.DownloadFileAsync(url, webProxy, timeout - 2); + var ws = downloadHandle2.DownloadDataAsync(url, webProxy, timeout - 2); Thread.Sleep(1000 * timeout); ws.CancelAsync(); + ws.Dispose(); Thread.Sleep(1000 * 2); }