Exemple #1
0

        
Exemple #2
0
        private async void btnLogin_Click(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrEmpty(tbUserName.Text))
            {
                tbUserName.Focus(FocusState.Keyboard);
                return;
            }
            if (string.IsNullOrEmpty(tbPassword.Password))
            {
                tbPassword.Focus(FocusState.Keyboard);
                return;
            }
            if (string.IsNullOrEmpty(tbCheckcode.Text.Trim()))
            {
                tbCheckcode.Focus(FocusState.Keyboard);
                return;
            }

            if (user.UserInfo == null)
            {
                user.UserInfo = new UserJWGLEntity();
            }
            user.UserInfo.UserName = tbUserName.Text.Trim();
            user.UserInfo.Password = tbPassword.Password.Trim();
            string checkCode = tbCheckcode.Text.Trim();

            try
            {
                tbTips.Text = "登录中,请稍后···";

                //简单单例模式,献丑了
                gd = GDJWGL.GDJWGLInit();
                if (client == null)
                {
                    await new MessageDialog("重新输入验证码").ShowAsync();
                    imgCheckcode_PointerPressed(null, null);
                    tbCheckcode.Text = "";
                    return;
                }
                HttpResponseMessage response = await client.GetAsync(new Uri("http://gdjwgl.bjut.edu.cn/default2.aspx"));

                //HttpCookieCollection cookieCollection = filter.CookieManager.GetCookies(new Uri("http://gdjwgl.bjut.edu.cn/default2.aspx"));


                string responseHtml = await response.Content.ReadAsStringAsync();

                __VIEWSTATEString = gd.GetVIEWSTATE(responseHtml);


                if (__VIEWSTATEString == "")
                {
                    tbTips.Text         = "登录失败!";
                    tbPassword.Password = "";
                    imgCheckcode_PointerPressed(null, null);
                    tbCheckcode.Text = "";
                    return;
                }


                IDictionary <string, string> parameters = new Dictionary <string, string>();
                parameters.Add("__VIEWSTATE", __VIEWSTATEString);

                parameters.Add("txtUserName", user.UserInfo.UserName);
                parameters.Add("TextBox2", user.UserInfo.Password);
                parameters.Add("txtSecretCode", checkCode);
                parameters.Add("RadioButtonList1", "学生");
                parameters.Add("Button1", "");
                parameters.Add("lbLanguage", "");
                parameters.Add("hidPdrs", "");
                parameters.Add("hidsc", "");

                HttpFormUrlEncodedContent postData = new HttpFormUrlEncodedContent(parameters);

                //HttpStringContent content = new HttpStringContent(postData);
                HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, new Uri("http://gdjwgl.bjut.edu.cn/default2.aspx"));

                //request.Headers["Referer"] = "http://gdjwgl.bjut.edu.cn/default2.aspx";
                request.Headers["ContentType"] = "application/x-www-form-urlencoded";

                request.Content = postData;

                HttpResponseMessage respones = await client.SendRequestAsync(request);

                string stringRespones = await respones.Content.ReadAsStringAsync();

                //Windows.Storage.Streams.IInputStream streamRespones = await respones.Content.ReadAsInputStreamAsync()

                gd.StudentName    = gd.GetName(stringRespones);
                gd.StudentID      = user.UserInfo.UserName;
                gd.loginInfo.ergs = null;
                gd.loginInfo.ob   = filter;

                if (gd.StudentName == "")
                {
                    tbTips.Text         = "登录失败!检查用户名或密码!";
                    tbPassword.Password = "";
                    imgCheckcode_PointerPressed(null, null);
                    tbCheckcode.Text = "";
                    return;
                }
                await new MessageDialog(String.Format("欢迎你,{0}同学!", gd.StudentName)).ShowAsync();
                btnLogoff.IsEnabled = true;
                btnLogin.IsEnabled  = false;
                if (cbSave.IsChecked == true)
                {
                    user.SaveUserInfo();
                }
                //登录成功,隐藏登录界面,显示教务管理主页
                gridContent.Visibility = Visibility.Visible;
                gridLogin.Visibility   = Visibility.Collapsed;
            }
            catch (Exception ex)
            {
                tbTips.Text = "登录失败!检查你的网络连接!";
                imgCheckcode_PointerPressed(null, null);
                tbCheckcode.Text = "";
            }
        }