.net检查api响应数据c#post request

时间:2018-06-09 19:28:21

标签: c# .net api post

我正在使用以下代码发布帖子请求,一切正常,但如果我输入错误的密码,响应数据会收到错误消息(100或任何事情)我的应用程序崩溃。什么是最好的方法来检查用户是否输入了正确的密码,或者api返回正常状态?

  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();

1 个答案:

答案 0 :(得分:0)

将上面的代码包含在try / catch块中。这也允许您查看正在生成的异常。

    try
    {

      ….. YOUR CODE HERE

    }
    catch(Exception e)
    {
       ..... check the exception here
    }