I'm using the following code to make a post request and everything is working fine, but if I enter a wrong password the response data gets error message (100 or any thing) my app crashes. What's the best way to check if user has entered the correct password or api is returning good to go?
var client = new WebClient();
var method = "POST"; // If your endpoint expects a GET then do it.
var parameters = new NameValueCollection();
parameters.Add("Email", Email);
parameters.Add("Password",Password);
parameters.Add("Device","Postman");
parameters.Add("OS","Win10");
/* Always returns a byte[] array data as a response. */
ServicePointManager.ServerCertificateValidationCallback +=(sender, cert, chain, sslPolicyErrors) => true;
var response_data = client.UploadValues(url, method, parameters);
// Parse the returned data (if any) if needed.
var responseString = UnicodeEncoding.UTF8.GetString(response_data);
var jsonObject = JObject.Parse(responseString);
authToken = jsonObject["AuthenticationToken"].ToString();
employeId = jsonObject["EmployeeID"].ToString();
Enclose the code above in a try/catch block. This also allows you to see what Exception is being generated.
try
{
….. YOUR CODE HERE
}
catch(Exception e)
{
..... check the exception here
}