示例#1
0
        private async void SaveProxyButtonClickAsync(Object sender, EventArgs e)
        {
            if (_addressTextBox.Text == Globals.ProxyAddress.GetValue() &&
                _portTextBox.Text == Globals.ProxyPort.GetValue().ToString() &&
                _loginTextBox.Text == Globals.ProxyLogin.GetValue() &&
                _passwordTextBox.Text == Globals.ProxyPassword.GetValue())
            {
                Close();
                return;
            }

            if (_addressTextBox.Text == @"127.0.0.1" && String.IsNullOrEmpty(_loginTextBox.Text) && String.IsNullOrEmpty(_passwordTextBox.Text))
            {
                ResetProxy();
                Close();
                return;
            }


            Int32.TryParse(_portTextBox.Text, out Int32 port);

            if (!NetworkUtils.ValidatePort(port))
            {
                new MessageForm(Globals.Localization.InvalidProxyPort, Globals.Localization.Error, Images.Basic.Warning, Images.Basic.Warning,
                                MessageBoxButtons.OK, new[] { Globals.Localization.OK }).ShowDialog();
                return;
            }

            WebProxy proxy = ProxyUtils.CreateProxy(_addressTextBox.Text, port, _loginTextBox.Text, _passwordTextBox.Text);
            Task <HttpStatusCode> checkTask = ProxyUtils.CheckProxyAsync(proxy);

            _addressTextBox.Enabled   = false;
            _portTextBox.Enabled      = false;
            _loginTextBox.Enabled     = false;
            _passwordTextBox.Enabled  = false;
            _resetProxyButton.Enabled = false;
            _saveProxyButton.Enabled  = false;

            HttpStatusCode check = HttpStatusCode.ServiceUnavailable;

            if (await Task.WhenAny(checkTask, Task.Delay(3000)).ConfigureAwait(true) == checkTask)
            {
                check = await checkTask.ConfigureAwait(true);
            }

            switch (check)
            {
            case HttpStatusCode.OK:
                Globals.WebProxy = proxy;
                Globals.ProxyAddress.SetValue(_addressTextBox.Text);
                Globals.ProxyPort.SetValue(port);
                Globals.ProxyLogin.SetValue(_loginTextBox.Text);
                Globals.ProxyPassword.SetValue(_passwordTextBox.Text);
                Close();
                break;

            case HttpStatusCode.Forbidden:
                new MessageForm(Globals.Localization.ProxyConnectionInvalid, Globals.Localization.Error, Images.Basic.Warning, Images.Basic.Warning,
                                MessageBoxButtons.OK, new[] { Globals.Localization.OK }).ShowDialog();
                break;

            case HttpStatusCode.ProxyAuthenticationRequired:
                new MessageForm(Globals.Localization.InvalidProxyCredentials, Globals.Localization.Error, Images.Basic.Warning, Images.Basic.Warning,
                                MessageBoxButtons.OK, new[] { Globals.Localization.OK }).ShowDialog();
                break;

            case HttpStatusCode.ServiceUnavailable:
                new MessageForm(Globals.Localization.ProxyIsUnreachable, Globals.Localization.Error, Images.Basic.Warning, Images.Basic.Warning,
                                MessageBoxButtons.OK, new[] { Globals.Localization.OK }).ShowDialog();
                break;

            default:
                new MessageForm($"{Globals.Localization.UnknownError}\n{check}", Globals.Localization.Error, Images.Basic.Warning, Images.Basic.Warning,
                                MessageBoxButtons.OK, new[] { Globals.Localization.OK }).ShowDialog();
                break;
            }

            _addressTextBox.Enabled   = true;
            _portTextBox.Enabled      = true;
            _loginTextBox.Enabled     = true;
            _passwordTextBox.Enabled  = true;
            _resetProxyButton.Enabled = true;
            CheckAddressValid();
        }