C# http

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Net;
using System.Threading;
using System.Windows.Forms;
using main_loader.Utility;
using Newtonsoft.Json;

namespace main_loader.frames
{
    // Token: 0x0200000E RID: 14
    public class LoginForm : Form
    {
     
        public bool IsLoggedIn
        {
            get
            {
                return this.hasBeenLoggedIn;
            }
        }

     
        public string SessionKey
        {
            get
            {
                return this.session_key;
            }
        }


        public NetClient.AuthResult AuthResult
        {
            get
            {
                return this.auth_result;
            }
        }

        public LoginForm()
        {
            this.InitializeComponent();
            this.loginTextBox.Text = Config.Login;
            this.paswordTextBox.Text = Config.Password;
        }

        private void Register_Click(object sender, EventArgs e)
        {
            RegisterForm registerForm = new RegisterForm
            {
                StartPosition = FormStartPosition.CenterParent
            };
            if (sender is string)
            {
                registerForm.license_key.Text = (sender as string);
            }
            registerForm.Closing += delegate(object o, CancelEventArgs args)
            {
                if (registerForm.IsRegisteredSuccessfully)
                {
                    this.loginTextBox.Text = registerForm.Login;
                    this.paswordTextBox.Text = registerForm.Password;
                    Config.Login = registerForm.Login;
                    Config.Password = registerForm.Password;
                    Config.Save();
                }
            };
            DialogResult dialogResult = registerForm.ShowDialog();
            if (dialogResult == DialogResult.OK)
            {
                Application.Run(registerForm);
            }
        }

        private void Login_Click(object sender, EventArgs e)
        {
            this.DisableControls();
            try
            {
                HttpWebRequest httpWebRequest = WebRequest.Create("http://xxx.xx.xx.xx/loader/login.php") as HttpWebRequest;
                Dictionary<string, string> dictionary = new Dictionary<string, string>();
                dictionary["login"] = this.loginTextBox.Text;
                dictionary["password"] = this.paswordTextBox.Text;
                byte[] array = NetClient.SerializeParameters(dictionary);
                httpWebRequest.Method = "POST";
                httpWebRequest.ContentType = "application/x-www-form-urlencoded";
                httpWebRequest.ContentLength = (long)array.Length;
                this.is_aborted = false;
                ThreadPool.RegisterWaitForSingleObject(httpWebRequest.BeginGetRequestStream(new AsyncCallback(this.UploadValuesCallback), httpWebRequest).AsyncWaitHandle, new WaitOrTimerCallback(this.TimeoutCallback), httpWebRequest, 60000, true);
            }
            catch (WebException ex)
            {
                if (ex.Status != WebExceptionStatus.RequestCanceled)
                {
                    MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                }
                this.EnableControls();
            }
            catch (Exception ex2)
            {
                this.EnableControls();
                MessageBox.Show(ex2.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand);
            }
        }

        private void UploadValuesCallback(IAsyncResult result)
        {
            try
            {
                HttpWebRequest httpWebRequest = result.AsyncState as HttpWebRequest;
                Stream stream = httpWebRequest.EndGetRequestStream(result);
                Dictionary<string, string> dictionary = new Dictionary<string, string>();
                dictionary["login"] = this.loginTextBox.Text;
                dictionary["password"] = this.paswordTextBox.Text;
                byte[] array = NetClient.SerializeParameters(dictionary);
                stream.Write(array, 0, array.Length);
                stream.Close();
                ThreadPool.RegisterWaitForSingleObject(httpWebRequest.BeginGetResponse(new AsyncCallback(this.ResponseCallback), httpWebRequest).AsyncWaitHandle, new WaitOrTimerCallback(this.TimeoutCallback), httpWebRequest, 60000, true);
            }
            catch (WebException ex)
            {
                if (ex.Status != WebExceptionStatus.RequestCanceled)
                {
                    MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                }
                this.EnableControls();
            }
            catch (Exception ex2)
            {
                MessageBox.Show(ex2.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                this.EnableControls();
            }
        }

        private void TimeoutCallback(object state, bool timedOut)
        {
            if (timedOut)
            {
                HttpWebRequest httpWebRequest = state as HttpWebRequest;
                if (httpWebRequest != null)
                {
                    if (!this.is_aborted)
                    {
                        this.EnableControls();
                        this.is_aborted = true;
                        MessageBox.Show("无法连接到服务器!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                    }
                    httpWebRequest.Abort();
                }
            }
        }

        private void ResponseCallback(IAsyncResult result)
        {
            this.EnableControls();
            try
            {
                using (Stream responseStream = ((result.AsyncState as HttpWebRequest).EndGetResponse(result) as HttpWebResponse).GetResponseStream())
                {
                    using (StreamReader streamReader = new StreamReader(responseStream))
                    {
                        NetClient.AuthResult authResult = JsonConvert.DeserializeObject<NetClient.AuthResult>(Encryption.Base64Decode(streamReader.ReadToEnd()));
                        this.auth_result = authResult;
                        responseStream.Close();
                        streamReader.Close();
                        if (authResult.CanRegister)
                        {
                            base.Invoke(new MethodInvoker(delegate()
                            {
                                this.Register_Click(this.loginTextBox.Text, null);
                            }));
                        }
                        else if (!authResult.IsAuthenticated)
                        {
                            MessageBox.Show(authResult.ErrorMessage, "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                        }
                        else
                        {
                            Config.Login = this.loginTextBox.Text;
                            Config.Password = this.paswordTextBox.Text;
                            Config.Save();
                            this.hasBeenLoggedIn = true;
                            this.session_key = authResult.AuthData.Session;
                            base.BeginInvoke(new MethodInvoker(delegate()
                            {
                                base.Close();
                            }));
                        }
                    }
                }
            }
            catch (WebException ex)
            {
                if (ex.Status != WebExceptionStatus.RequestCanceled)
                {
                    MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                }
            }
            catch (Exception ex2)
            {
                MessageBox.Show(ex2.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand);
            }
        }

        private void DisableControls()
        {
            this.Login.SetPropertyThreadSafe(() => this.Login.Enabled, false);
            this.Register.SetPropertyThreadSafe(() => this.Register.Enabled, false);
            this.loginTextBox.SetPropertyThreadSafe(() => this.loginTextBox.Enabled, false);
            this.paswordTextBox.SetPropertyThreadSafe(() => this.paswordTextBox.Enabled, false);
        }

        private void EnableControls()
        {
            this.Login.SetPropertyThreadSafe(() => this.Login.Enabled, true);
            this.Register.SetPropertyThreadSafe(() => this.Register.Enabled, true);
            this.loginTextBox.SetPropertyThreadSafe(() => this.loginTextBox.Enabled, true);
            this.paswordTextBox.SetPropertyThreadSafe(() => this.paswordTextBox.Enabled, true);
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && this.components != null)
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void InitializeComponent()
        {
            this.Login = new Button();
            this.Register = new Button();
            this.loginTextBox = new TextBox();
            this.paswordTextBox = new TextBox();
            base.SuspendLayout();
            this.Login.Location = new Point(233, 36);
            this.Login.Name = "Login";
            this.Login.Size = new Size(75, 23);
            this.Login.TabIndex = 0;
            this.Login.Text = "登录";
            this.Login.UseVisualStyleBackColor = true;
            this.Login.Click += this.Login_Click;
            this.Register.Location = new Point(12, 77);
            this.Register.Name = "Register";
            this.Register.Size = new Size(75, 23);
            this.Register.TabIndex = 1;
            this.Register.Text = "注册";
            this.Register.UseVisualStyleBackColor = true;
            this.Register.Click += this.Register_Click;
            this.loginTextBox.Location = new Point(12, 12);
            this.loginTextBox.Name = "loginTextBox";
            this.loginTextBox.Size = new Size(215, 20);
            this.loginTextBox.TabIndex = 2;
            this.loginTextBox.Text = "注册码";
            this.paswordTextBox.Location = new Point(12, 38);
            this.paswordTextBox.Name = "paswordTextBox";
            this.paswordTextBox.Size = new Size(215, 20);
            this.paswordTextBox.TabIndex = 3;
            this.paswordTextBox.Text = "密码";
            base.AutoScaleDimensions = new SizeF(6f, 13f);
            base.AutoScaleMode = AutoScaleMode.Font;
            this.BackColor = SystemColors.Control;
            base.ClientSize = new Size(407, 112);
            base.Controls.Add(this.paswordTextBox);
            base.Controls.Add(this.loginTextBox);
            base.Controls.Add(this.Register);
            base.Controls.Add(this.Login);
            base.FormBorderStyle = FormBorderStyle.FixedDialog;
            base.Name = "LoginForm";
            base.ShowIcon = false;
            base.StartPosition = FormStartPosition.CenterScreen;
            this.Text = "xXX.Loader";
            base.ResumeLayout(false);
            base.PerformLayout();
        }

        private bool is_aborted;

        private bool hasBeenLoggedIn;

        private string session_key;

        private NetClient.AuthResult auth_result;

        private IContainer components;

        private Button Login;

        private Button Register;

        private TextBox loginTextBox;

        private TextBox paswordTextBox;
    }
}

  

C# http

上一篇:Winform中实现拖拽文件到ListView获取文件类型(附代码下载)


下一篇:Winform中实现向窗体中拖放照片并显示以及拖放文件夹显示树形结构(附代码下载)