예제 #1
0
        private void btnLogin_Click(object sender, EventArgs e)
        {
            this.lblWarning.Text = "";
            string username = this.txtUsername.Text.Trim();
            string password = this.txtPwd.Text.Trim();

            if (String.IsNullOrEmpty(username) || String.IsNullOrEmpty(password))
            {
                this.lblWarning.Text = "用户名或密码不能为空!";
                return;
            }
            string postData = "tuser="******"&pw=" + password);
            postData += "&from=1";

            HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(ApplicationContext.ServerHost() + "login2.php");

            myRequest.Method      = "POST";
            myRequest.ContentType = "application/x-www-form-urlencoded";
            Stream       newStream = myRequest.GetRequestStream();
            StreamWriter sw        = new StreamWriter(newStream, Encoding.Default);

            sw.Write(postData);
            sw.Close();
            newStream.Close();
            try
            {
                HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
                StreamReader    reader     = new StreamReader(myResponse.GetResponseStream(), Encoding.Default);
                string          content    = reader.ReadToEnd();
                JsonData        jsonData   = JsonMapper.ToObject(content);
                CredentialModel credential = new CredentialModel();
                credential.StatusCode = (int)jsonData["success"];
                credential.Message    = (string)jsonData["msg"];
                if (credential.StatusCode != 1)
                {
                    this.txtPwd.Text     = "";
                    this.lblWarning.Text = credential.Message;
                }
                else
                {
                    credential.UserId             = (string)jsonData["emp_no"];
                    credential.SessionId          = (string)jsonData["sid"];
                    ApplicationContext.Credential = credential;
                    try
                    {
                        string commandText = "INSERT INTO user (id, username, password) VALUES (1, '"
                                             + username
                                             + "', '"
                                             + password
                                             + "')";
                        SQLiteHelper.ExecuteNonQuery(commandText, CommandType.Text);
                    }
                    catch (Exception ex)
                    {
                    }
                    this.startForm.ActiveTimer();
                    this.Close();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                this.txtPwd.Text     = "";
                this.lblWarning.Text = "登录失败!";
            }
        }