Ejemplo n.º 1
0
        private void buttonLogin_Click(object sender, EventArgs e)
        {
            if (textBoxAccount.Text.Trim() == "" || textBoxPassword.Text.Trim() == "")
            {
                MessageBox.Show("账号或密码不能为空,请重新输入");
                this.DialogResult = DialogResult.None;
            }
            else
            {
                try
                {
                    XmlDocument xmldoc = new XmlDocument();
                    xmldoc.Load(Application.StartupPath + "/LoginConfig.xml");
                    XmlNode address = xmldoc.SelectSingleNode("//address");
                    XmlNode account = xmldoc.SelectSingleNode("//account");
                    XmlNode password = xmldoc.SelectSingleNode("//password");
                    XmlNode token = xmldoc.SelectSingleNode("//token");
                    XmlNode isLoginDefault = xmldoc.SelectSingleNode("//isLoginDefault");

                    address.InnerText = m_strAddr;
                    account.InnerText = this.textBoxAccount.Text.Trim();
                    password.InnerText = "";
                    isLoginDefault.InnerText = "false";

                    xmldoc.Save(Application.StartupPath + "/LoginConfig.xml");
                }
                catch (Exception ex)
                {
                    string strErr = ex.Message;
                    MessageBox.Show("配置文件出错.请重新安装程序修复", "提示");
                }

                try
                {
                    HttpHelper httphelper = new HttpHelper();
                    NameValueCollection namevaluec = new NameValueCollection();
                    namevaluec.Add("client_id", "100006");
                    namevaluec.Add("username", this.textBoxAccount.Text.Trim());
                    namevaluec.Add("password", this.textBoxPassword.Text);
                    namevaluec.Add("grant_type", "password");

                    string loginrequest = httphelper.postHttpRequest("http://" + m_strAddr + "/oauth/token", namevaluec);

                    if(LoginjJsonResult(loginrequest))
                    {
                        this.Hide();
                        m_loginProg = new LoginProgress(this);
                        m_loginProg.Show();

                        Thread downloadThread = new Thread(new ThreadStart(DownloadUserInfo));
                        downloadThread.Start();

                        while (string.IsNullOrEmpty(PaperInfoForm.static_postpaperrequest))
                        {
                            Thread.Sleep(sleep);
                            loop++;

                            if (loop == tryMax)
                            {
                                break;
                            }
                        }

                        m_loginProg.Close();

                        if (progressBarRet)
                        {
                            this.Close();
                        }
                        else
                        {
                            this.Show();
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "错误");
                }
            }
        }
Ejemplo n.º 2
0
        private bool LoginJsonUserSetting()
        {
            XmlDocument xmldocc = new XmlDocument();
            string postpaperrequest = string.Empty;

            try
            {
                xmldocc.Load(Application.StartupPath + "/LoginConfig.xml");
                XmlNode access_token = xmldocc.SelectSingleNode("//access_token");
                XmlNode refresh_token = xmldocc.SelectSingleNode("//refresh_token");

                if (string.IsNullOrEmpty(access_token.InnerText) || string.IsNullOrEmpty(refresh_token.InnerText))
                {
                    return false;      //config.xml不存在
                }

                XmlNode address = xmldocc.SelectSingleNode("//address");
                XmlNode account = xmldocc.SelectSingleNode("//account");
                XmlNode password = xmldocc.SelectSingleNode("//password");
                XmlNode token_type = xmldocc.SelectSingleNode("//token_type");
                XmlNode openid = xmldocc.SelectSingleNode("//openid");
                XmlNode postpaper = xmldocc.SelectSingleNode("//postpaper");

                HttpHelper httphelper = new HttpHelper();
                NameValueCollection namevaluec = new NameValueCollection();
                namevaluec.Add("access_token", access_token.InnerText);
                namevaluec.Add("refresh_token", refresh_token.InnerText);
                namevaluec.Add("token_type", token_type.InnerText);
                namevaluec.Add("openid", openid.InnerText);
                namevaluec.Add("userid", account.InnerText);
                namevaluec.Add("client_id", "100006");
                namevaluec.Add("clientip", "127.0.0.1");
                namevaluec.Add("oauth_version", "2.a");
                namevaluec.Add("pagesize", "100");

                postpaperrequest = httphelper.postHttpRequest("http://graph.ciwong.com/paperclip/get_paper_clip", namevaluec);

                if(string.IsNullOrEmpty(postpaperrequest))
                {
                    return false;      //登录失败
                }

                PaperInfoForm.static_postpaperrequest = postpaperrequest;
                return true;       //成功
            }
            catch(Exception ex)
            {
                string strErr = ex.Message;
                MessageBox.Show("可能由于网络原因,试卷上传失败,请稍后再重新上传!", "提示");
                return false;
            }
        }