Esempio n. 1
0
        public static ProxyClient Parse(ProxyType proxyType, string proxyAddress)
        {
            if (proxyAddress == null)
            {
                throw new ArgumentNullException("proxyAddress");
            }
            if (proxyAddress.Length == 0)
            {
                throw ExceptionHelper.EmptyString("proxyAddress");
            }
            string[] array = proxyAddress.Split(':');
            int      port  = 0;
            string   host  = array[0];

            if (array.Length >= 2)
            {
                try
                {
                    port = int.Parse(array[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 = null;
            string password = null;

            if (array.Length >= 3)
            {
                username = array[2];
            }
            if (array.Length >= 4)
            {
                password = array[3];
            }
            return(ProxyHelper.CreateProxyClient(proxyType, host, port, username, password));
        }
Esempio n. 2
0
        public static bool TryParse(ProxyType proxyType, string proxyAddress, out ProxyClient result)
        {
            result = null;
            if (string.IsNullOrEmpty(proxyAddress))
            {
                return(false);
            }
            string[] array   = proxyAddress.Split(':');
            int      result2 = 0;
            string   host    = array[0];

            if (array.Length >= 2 && (!int.TryParse(array[1], out result2) || !ExceptionHelper.ValidateTcpPort(result2)))
            {
                return(false);
            }
            string username = null;
            string password = null;

            if (array.Length >= 3)
            {
                username = array[2];
            }
            if (array.Length >= 4)
            {
                password = array[3];
            }
            try
            {
                result = ProxyHelper.CreateProxyClient(proxyType, host, result2, username, password);
            }
            catch (InvalidOperationException)
            {
                return(false);
            }
            return(true);
        }
Esempio n. 3
0
        /// <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));
        }