private void btnSignIn_Click(object sender, EventArgs e) { dynamic NewUser; //Try converting age. try { //Validations int userAge = int.Parse(age.Text); if (userAge < 18 || userAge > 120) { MessageBox.Show("User age must be between 18 and 120."); return; } if (name.Text.Length < 2 || name.Text.Length > 10) { MessageBox.Show("User name must contains 2 - 10 chars."); return; } //Post Request for Login var httpWebRequest = (HttpWebRequest)WebRequest.Create(@"http://localhost:57034/Login"); httpWebRequest.ContentType = "application/json"; httpWebRequest.Method = "POST"; using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream())) { NewUser = new User { UserName = name.Text, Age = int.Parse(age.Text) }; string NewUserString = Newtonsoft.Json.JsonConvert.SerializeObject(NewUser, Formatting.None); streamWriter.Write(NewUserString); streamWriter.Flush(); streamWriter.Close(); } //Gettting response var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse(); //Reading response using (var streamReader = new System.IO.StreamReader(httpResponse.GetResponseStream(), ASCIIEncoding.ASCII)) { string result = streamReader.ReadToEnd(); //If Login succeeded if (result.Contains("true")) { ChoosePartner choosePartner = new ChoosePartner(); choosePartner.Show(); GlobalProp.CurrentUser = NewUser; } //Printing the matching error else { MessageBox.Show(result); } } } catch { MessageBox.Show("Age must be a number!"); } }
private void btnSignIn_Click(object sender, EventArgs e) { dynamic credential; try { int userAge = int.Parse(age.Text); var httpWebRequest = (HttpWebRequest)WebRequest.Create(@"http://localhost:57034/Login"); httpWebRequest.ContentType = "application/json"; httpWebRequest.Method = "POST"; using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream())) { credential = new User { UserName = name.Text, Age = int.Parse(age.Text) }; string credentialString = Newtonsoft.Json.JsonConvert.SerializeObject(credential, Formatting.None); streamWriter.Write(credentialString); streamWriter.Flush(); streamWriter.Close(); } var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse(); using (var streamReader = new System.IO.StreamReader(httpResponse.GetResponseStream(), ASCIIEncoding.ASCII)) { string result = streamReader.ReadToEnd(); if (result.Contains("true")) { ChoosePartner choosePartner = new ChoosePartner(); choosePartner.Show(); GlobalProp.CurrentUser = credential; } else { MessageBox.Show(result); } } } catch { MessageBox.Show("Age must be a number!"); } }
private void btnSignIn_Click(object sender, EventArgs e) { //Try converting age. try { string userName = name.Text; int userAge = int.Parse(age.Text); //Validations if (userAge < 18 || userAge > 120) { MessageBox.Show("User age must be between 18 and 120."); return; } if (userName.Length < 2 || userName.Length > 10) { MessageBox.Show("User name must contains 2 - 10 chars."); return; } // HTTP POST string url = $"{GlobalProp.Host}/user/signIn"; var httpWebRequest = (HttpWebRequest)WebRequest.Create(@url); var postData = $"userName={userName}"; postData += $"&age={userAge}"; var data = Encoding.ASCII.GetBytes(postData); httpWebRequest.Method = "POST"; httpWebRequest.ContentType = "application/x-www-form-urlencoded"; httpWebRequest.ContentLength = data.Length; using (var stream = httpWebRequest.GetRequestStream()) { stream.Write(data, 0, data.Length); } var response = (HttpWebResponse)httpWebRequest.GetResponse(); var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd(); //Getting response //If Login succeeded if (responseString.Equals("true")) { ChoosePartner choosePartner = new ChoosePartner(); choosePartner.Show(); GlobalProp.CurrentUser = new User() { UserName = userName, Age = userAge }; } //Printing the matching error else { MessageBox.Show(responseString); } } catch (Exception ex) { MessageBox.Show(ex.Message); } }