/// <summary> /// Converts a string to a class client proxy instance inherited from <see cref="ProxyClient"/>. Gets a value indicating whether the conversion was successfully. /// </summary> /// <param name="proxyType">Proxy Type.</param> /// <param name="proxyAddress">String type - host:port:username:password. The last three are optional.</param> /// <param name="result">If the conversion is successful, it contains an instance of the proxy client, inherited from <see cref="ProxyClient"/>, otherwise <see langword="null"/>.</param> /// <returns>Value <see langword="true"/>, if the parameter <paramref name="proxyAddress"/> converted successfully, otherwise <see langword="false"/>.</returns> public static bool TryParse(ProxyType proxyType, string proxyAddress, out ProxyClient result) { result = null; #region Check settings if (string.IsNullOrEmpty(proxyAddress)) { return(false); } #endregion string[] values = proxyAddress.Split(':'); int port = 0; string host = values[0]; if (values.Length >= 2) { if (!int.TryParse(values[1], out port) || !ExceptionHelper.ValidateTcpPort(port)) { return(false); } } string username = null; string password = null; if (values.Length >= 3) { username = values[2]; } if (values.Length >= 4) { password = values[3]; } try { result = ProxyHelper.CreateProxyClient(proxyType, host, port, username, password); } catch (InvalidOperationException) { return(false); } return(true); }
/// <summary> /// Converts a string to a class client proxy instance inherited from <see cref="ProxyClient"/>. /// </summary> /// <param name="proxyType">Proxy Type.</param> /// <param name="proxyAddress">String type - host:port:username:password. The last three are optional.</param> /// <returns>An instance of a client proxy, inherited from <see cref="ProxyClient"/>.</returns> /// <exception cref="System.ArgumentNullException">parameter <paramref name="proxyAddress"/> equally <see langword="null"/>.</exception> /// <exception cref="System.ArgumentException">parameter <paramref name="proxyAddress"/> It is an empty string.</exception> /// <exception cref="System.FormatException">port format is wrong.</exception> /// <exception cref="System.InvalidOperationException">Received an unsupported type of proxy server.</exception> public static ProxyClient Parse(ProxyType proxyType, string proxyAddress) { #region Check settings 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 Getting the port 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)); }