public static ProxyClient Parse(ProxyType proxyType, string proxyAddress) { if (proxyAddress == null) { throw new ArgumentNullException(nameof(proxyAddress)); } if (proxyAddress.Length == 0) { throw ExceptionHelper.EmptyString(nameof(proxyAddress)); } string[] strArray = proxyAddress.Split(':'); int port = 0; string host = strArray[0]; if (strArray.Length >= 2) { try { port = int.Parse(strArray[1]); } catch (Exception ex) { if (ex is FormatException || ex is OverflowException) { throw new FormatException(Resources.InvalidOperationException_ProxyClient_WrongPort, ex); } throw; } if (!ExceptionHelper.ValidateTcpPort(port)) { throw new FormatException(Resources.InvalidOperationException_ProxyClient_WrongPort); } } string username = (string)null; string password = (string)null; if (strArray.Length >= 3) { username = strArray[2]; } if (strArray.Length >= 4) { password = strArray[3]; } return(ProxyHelper.CreateProxyClient(proxyType, host, port, username, password)); }
public static bool TryParse(ProxyType proxyType, string proxyAddress, out ProxyClient result) { result = (ProxyClient)null; if (string.IsNullOrEmpty(proxyAddress)) { return(false); } string[] strArray = proxyAddress.Split(':'); int result1 = 0; string host = strArray[0]; if (strArray.Length >= 2 && (!int.TryParse(strArray[1], out result1) || !ExceptionHelper.ValidateTcpPort(result1))) { return(false); } string username = (string)null; string password = (string)null; if (strArray.Length >= 3) { username = strArray[2]; } if (strArray.Length >= 4) { password = strArray[3]; } try { result = ProxyHelper.CreateProxyClient(proxyType, host, result1, username, password); } catch (InvalidOperationException ex) { return(false); } return(true); }
/// <summary> /// Преобразует строку в экземпляр класса прокси-клиента, унаследованный от <see cref="ProxyClient"/>. /// </summary> /// <param name="proxyType">Тип прокси-сервера.</param> /// <param name="proxyAddress">Строка вида - хост:порт:имя_пользователя:пароль. Три последних параметра являются необязательными.</param> /// <returns>Экземпляр класса прокси-клиента, унаследованный от <see cref="ProxyClient"/>.</returns> /// <exception cref="System.ArgumentNullException">Значение параметра <paramref name="proxyAddress"/> равно <see langword="null"/>.</exception> /// <exception cref="System.ArgumentException">Значение параметра <paramref name="proxyAddress"/> является пустой строкой.</exception> /// <exception cref="System.FormatException">Формат порта является неправильным.</exception> /// <exception cref="System.InvalidOperationException">Получен неподдерживаемый тип прокси-сервера.</exception> public static ProxyClient Parse(ProxyType proxyType, string proxyAddress) { #region Проверка параметров if (proxyAddress == null) { throw new ArgumentNullException("proxyAddress"); } if (proxyAddress.Length == 0) { throw ExceptionHelper.EmptyString("proxyAddress"); } #endregion string[] values = proxyAddress.Split(':'); int port = 0; string host = values[0]; if (values.Length >= 2) { #region Получение порта try { port = int.Parse(values[1]); } catch (Exception ex) { if (ex is FormatException || ex is OverflowException) { throw new FormatException( "The port can not be less than 1 or greater than 65535.", ex); } throw; } if (!ExceptionHelper.ValidateTcpPort(port)) { throw new FormatException( "The port can not be less than 1 or greater than 65535."); } #endregion } string username = null; string password = null; if (values.Length >= 3) { username = values[2]; } if (values.Length >= 4) { password = values[3]; } return(ProxyHelper.CreateProxyClient(proxyType, host, port, username, password)); }
/// <summary> /// 转换字符串类实例的代理客户,继承了<see cref="ProxyClient"/>. /// </summary> /// <param name="proxyType">代理服务器类型。</param> /// <param name="proxyAddress">行看主机:港口:名字_用户:密码。最后的三参数是可选的。</param> /// <returns>客户的代理类实例,继承了<see cref="ProxyClient"/>.</returns> /// <exception cref="System.ArgumentNullException">参数值<paramref name="proxyAddress"/> 等于<see langword="null"/>.</exception> /// <exception cref="System.ArgumentException">参数值<paramref name="proxyAddress"/> 是空字符串。</exception> /// <exception cref="System.FormatException">格式不对。港口是</exception> /// <exception cref="System.InvalidOperationException">获得代理服务器类型中。</exception> public static ProxyClient Parse(ProxyType proxyType, string proxyAddress) { #region 检查参数 if (proxyAddress == null) { throw new ArgumentNullException("proxyAddress"); } if (proxyAddress.Length == 0) { throw ExceptionHelper.EmptyString("proxyAddress"); } #endregion string[] values = proxyAddress.Split(':'); int port = 0; string host = values[0]; if (values.Length >= 2) { #region 接收端口 try { port = int.Parse(values[1]); } catch (Exception ex) { if (ex is FormatException || ex is OverflowException) { throw new FormatException( Resources.InvalidOperationException_ProxyClient_WrongPort, ex); } throw; } if (!ExceptionHelper.ValidateTcpPort(port)) { throw new FormatException( Resources.InvalidOperationException_ProxyClient_WrongPort); } #endregion } string username = null; string password = null; if (values.Length >= 3) { username = values[2]; } if (values.Length >= 4) { password = values[3]; } return(ProxyHelper.CreateProxyClient(proxyType, host, port, username, password)); }