Example #1
0
        private void button1_Click(object sender, EventArgs e)
        {
            //加密当前mac地址
            string verification_code = CommonUtil.Encrypt();
            //操作工工号
            string employee_no = this.txtLoginName.Text.Trim();
            //密码
            string upwd = this.txtLoginPwd.Text.Trim();

            this.button1.Text    = "登录中...";
            this.button1.Enabled = false;
            try
            {
                this.label_msg.Text = "发送登录请求。。。";
                //封装客户端登录所传数据
                JObject json = new JObject {
                    { "employee_no", employee_no },
                    { "ppassword", upwd },
                    { "verification_code", verification_code }
                };
                ASCIIEncoding  encoding = new ASCIIEncoding();
                String         content  = "";
                byte[]         data     = encoding.GetBytes(json.ToString());
                string         url      = CommonUtil.getServerIpAndPort() + "Login/userLoginOfWinform.action";
                HttpWebRequest request  = (HttpWebRequest)HttpWebRequest.Create(url);
                request.KeepAlive     = false;
                request.Method        = "POST";
                request.ContentType   = "application/json;characterSet:UTF-8";
                request.ContentLength = data.Length;
                using (Stream sm = request.GetRequestStream())
                {
                    sm.Write(data, 0, data.Length);
                }
                this.label_msg.Text = "登录验证中。。。";
                HttpWebResponse response       = (HttpWebResponse)request.GetResponse();
                Stream          streamResponse = response.GetResponseStream();
                using (StreamReader sr = new StreamReader(streamResponse))
                {
                    content = sr.ReadToEnd();
                }
                response.Close();
                if (content != null)
                {
                    //如果返回的数据为"{}"
                    this.label_msg.Text = "";
                    if (content.Trim().Contains("{}"))
                    {
                        this.label_msg.Text = "登录异常!";
                        MessagePrompt.Show("登录异常!");
                    }
                    else
                    {
                        //如果登录成功,返回的数据格式为{success:'True/False',msg:'',rowsData:''},rowsData存放的为当前登录用户的信息
                        JObject jobject   = JObject.Parse(content);
                        string  loginFlag = jobject["success"].ToString().Trim();
                        string  msg       = jobject["msg"].ToString().Trim();
                        if (loginFlag.Contains("True"))
                        {
                            string rowsJson = jobject["rowsData"].ToString();
                            if (rowsJson != null)
                            {
                                //登录成功返回当前登录用户的信息,将返回的json格式的信息转换成指定对象
                                this.label_msg.Text = "登录成功,初始化控件中...";
                                Person person = JsonConvert.DeserializeObject <Person>(rowsJson);
                                IndexWindow.getForm().Show();
                                NumberKeyboardForm.getForm();
                                AlphabetKeyboardForm.getForm();
                                ScanerHook.executeScanerHook();
                                this.Hide();
                            }
                            else
                            {
                                this.label_msg.Text = "系统繁忙,请稍后重试!";
                                MessagePrompt.Show("系统繁忙,请稍后重试!");
                            }
                        }
                        else
                        {
                            this.label_msg.Text = msg;
                            MessagePrompt.Show(msg);
                        }
                    }
                }
                this.button1.Enabled = true;
                this.button1.Text    = "登录";
            }
            catch (WebException ex) {
                MessagePrompt.Show("网络错误,错误信息:" + ex.Message);
                this.button1.Enabled = true;
                this.button1.Text    = "登录";
            }
            catch (Exception ec)
            {
                MessagePrompt.Show("连接服务器失败,失败原因:" + ec.Message);
                this.button1.Enabled = true;
                this.button1.Text    = "登录";
            }
        }