Пример #1
0
 public void Init(RegisterPage rp)
 {
     _registerPage = rp;
     registerReturned = false;
     registerTimer = new DispatcherTimer();
     registerTimer.Interval = TimeSpan.FromSeconds(10);
     rr = new RegisterResponce();
 }
Пример #2
0
        public void Register(string username, string password, string password1, RegisterPage rp)
        {
            if (!CheckUserNameFormat(username))
            {
                rr.code = 101;
                rr.content.message = "用户名格式不正确";
                RegisterFailed();
                return;
            }
            if (!CheckPasswordFormat(password))
            {
                rr.code = 102;
                rr.content.message = "密码长度应为6—20";
                RegisterFailed();
                return;
            }
            if(!CheckEqualPasswords(password, password1))
            {
                rr.code = 103;
                rr.content.message = "两次输入的密码不相同";
                RegisterFailed();
                return;
            }

            password = GetMd5Hash(password);

            StringBuilder parameters = new StringBuilder();
            parameters.AppendFormat("{0}={1}", "username", HttpUtility.UrlEncode(username));
            parameters.AppendFormat("&{0}={1}", "password", HttpUtility.UrlEncode(password));

            Uri registerPath = new Uri(GlobalVars.WebServer + "/register");

            WebClient registerWC = new WebClient();
            registerWC.Headers["Content-Type"] = "application/x-www-form-urlencoded";
            registerWC.Encoding = Encoding.UTF8;
            registerWC.UploadStringCompleted += (sender, e) =>
            {
                if (registerReturned)
                    return;
                registerReturned = true;
                if (e.Error != null)
                {
                    rr = new RegisterResponce() { code = 404, content = new Model.ResRegisterContent() { message = e.Error.Message } };
                }
                else
                {
                    MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(e.Result));
                    //RegisterResult rr = new RegisterResult();
                    DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(RegisterResponce));
                    rr = (RegisterResponce)serializer.ReadObject(ms);
                }
                if (rr.code == 200)
                {
                    RegisterSuccess();
                }
                else
                {
                    RegisterFailed();
                }
            };
            registerWC.UploadStringAsync(registerPath, "POST", parameters.ToString());

            registerTimer.Tick += (sen, er) =>
            {
                if (registerReturned)
                    return;
                registerReturned = true;
                registerTimer.Stop();
                rr = new RegisterResponce() { code = 400, content = new Model.ResRegisterContent() { message = "连接服务器超时." } };
                RegisterFailed();
            };
            
            registerTimer.Start();
        }