Example #1
0
        /// <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));
        }