/// <summary> /// 检测代理 /// </summary> /// <param name="testThread"></param> /// <param name="proxy"></param> private static void Testing(TestThread testThread, HttpProxy proxy) { var sw = new Stopwatch(); sw.Start(); var result = _testProxyHelper.TestProxy(proxy); sw.Stop(); #region var testResult = result.IsAlive; testThread.Description = proxy.IpAndPort + (testResult ? " Alive " : " Dead ") + sw.ElapsedMilliseconds + "ms"; var model = ProxyData.GetCopy(proxy.Ip, proxy.Port); try { if (model != null) { if (Config.LocalSetting.CheckArea) { var location = SearchIp(proxy.Ip); model.country = location; } if (testResult) { model.response = int.Parse(sw.ElapsedMilliseconds.ToString("F0")); model.testdate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); model.description = string.Format("{0} {1}", model.testdate, sw.ElapsedMilliseconds.ToString("F0") + "ms"); model.status = 1; } else { model.description = Config.LocalLanguage.Messages.ProxyIsDead + sw.ElapsedMilliseconds.ToString("F0") + "ms"; model.response = 99999; model.status = 0; model.testdate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); } ProxyData.Set(model); } } catch { ProxyData.Remove(model); } #endregion }
/// <summary> /// 删除代理 /// </summary> public void DeleteProxy() { foreach (DataGridViewRow dgvr in dgvProxyList.SelectedRows) { var model = (ProxyServer)dgvr.DataBoundItem; ProxyData.Remove(model); } BindData(); _strIp = ""; _strPort = ""; }
/// <summary> /// 自动切换代理 /// </summary> public void SwitchingProxy() { if (Config.MainForm.AutoSwitchingStatus.Text == Config.LocalLanguage.Messages.AutomaticSwitchingOff) { return; } try { if (Config.AutoProxyQueue.Count > 0) { Config.MainForm.tsslCountdown.Text = Config.LocalLanguage.Messages.Swithing; #region 如果有代理队列 HttpProxy httpProxy = Config.AutoProxyQueue.Dequeue(); var result = _testProxyHelper.TestProxy(httpProxy); var canUse = result.IsAlive; var speed = result.Response; if (canUse) { #region 更换代理 if (speed < Config.LocalSetting.AutoProxySpeed * 1000) { if (Config.ProxyApplicatioin == "IE") { var ph = new ProxyHelper(); ph.SetIEProxy(httpProxy.IpAndPort, 1); Config.MainForm.SetProxyStatusLabel(); } else { SetBrowserProxy(httpProxy.IpAndPort); } Config.TsCountDown = new TimeSpan(0, 0, Config.LocalSetting.AutoChangeProxyInterval); Config.MainForm.TimerAutoSwitchingProxy.Start(); } else { Config.MainForm.tsslCountdown.Text = Config.LocalLanguage.Messages.SpeedDoestConformToTheRequirement + "," + Config.LocalLanguage.Messages.Swithing + "..."; Config.MainForm.SetToolTipText(httpProxy.IpAndPort + Config.LocalLanguage.Messages .SpeedDoestConformToTheRequirement + "," + Config.LocalLanguage.Messages.Swithing + "..."); Thread.Sleep(1000); SwitchingProxy(); } #endregion } else { #region 经验证代理已失效 Config.MainForm.tsslCountdown.Text = Config.LocalLanguage.Messages.ProxyIsDead + "," + Config.LocalLanguage.Messages.Swithing + "..."; ProxyData.Remove(httpProxy.Ip, httpProxy.Port); Config.MainForm.SetToolTipText( string.Format(Config.LocalLanguage.Messages.ProxyIsDeadAutoSwithing, httpProxy.IpAndPort)); Thread.Sleep(1000); SwitchingProxy(); #endregion } #endregion } else { if (_counts < 20) //如果队列为空,自动设置队列20次停止 { SetQueue(); _counts++; } else { //Config.ProxyViewForm.StopAutoChange(); Config.MainForm.tsslCountdown.Text = string.Empty; Config.ConsoleEx.Debug(Config.LocalLanguage.Messages.AutoSwitchingProxyListIsEmpty); Config.MainForm.SetToolTipText(Config.LocalLanguage.Messages.AutoSwitchingProxyListIsEmpty); } } } catch (Exception ex) { LogHelper.WriteException(ex); Thread.Sleep(3000); SwitchingProxy(); } }