如何从ASP.NET Web API获取响应

时间:2018-03-05 01:46:31

标签: c# xamarin asp.net-web-api response

以下是向api发送请求以创建用户并将其保存到MySQL中的代码。

signUp.Click += async (s,e) =>
        {
            string name = uName.Text;
            string pin = pWord.Text;
            string rePin = rePWord.Text;
            string em = email.Text;
            string encoded="";

            if ((name != "") && (pin != "") && (rePin != "") && (em != ""))
            {
                if (pin.Equals(rePin))
                {
                    try
                    {
                        string orig = pin;

                        byte[] salt;
                        new RNGCryptoServiceProvider().GetBytes(salt = new byte[16]);

                        var pbkdf2 = new Rfc2898DeriveBytes(orig, salt, 10000);

                        byte[] hash = pbkdf2.GetBytes(20);
                        byte[] hashBytes = new byte[36];

                        Array.Copy(salt, 0, hashBytes, 0, 16);
                        Array.Copy(hash, 0, hashBytes, 16, 20);

                        encoded = Convert.ToBase64String(hashBytes);

                        Console.WriteLine(encoded);

                    }
                    catch
                    {
                        Console.WriteLine("Encode errors.");
                    }
                }
                else
                {
                    Console.WriteLine("The password doesn't match");
                }
                var response = await RunPostAsync(name, encoded, em).ConfigureAwait(false);

            }
            else
            {
                Console.WriteLine("Some value are missing");
            }


        };

这是RunPostAsync方法的代码。

public async Task<Person> RunPostAsync(string name, string pword, string email)
{

    try
    {
        HttpClient client = new HttpClient();
        var RestUrl = "http://10.20.1.68:45455/api/Person/";
        var request = new HttpRequestMessage(HttpMethod.Post, RestUrl);

        var p = new Person();

        p.name = name;
        p.password = pword;
        p.email = email;

        var json = JsonConvert.SerializeObject(p);
        request.Content = new StringContent(json, Encoding.UTF8, "application/json");

        var response = await client.SendAsync(request);

        return p;
    }
    catch (Exception ex)
    {
        return null;
    }
}

这是我的API中的代码在这里,它检查表中是否存在从客户端恢复的用户名。如果名称已存在,则返回字符串&#34;用户已经存在!&#34;否则,创建新用户并返回字符串&#34;已添加!&#34;。

// POST: api/Person
[HttpPost]
public string Post([FromBody]Person person)
{
    string postReult;
    ArrayList unameList = new ArrayList();
    ConnectMysql();
    string checkString = "SELECT uname FROM users";

    MySqlCommand checkCmd = new MySqlCommand(checkString, conn);

    MySqlDataReader myReader = checkCmd.ExecuteReader();

    while (myReader.Read())
    {
        string pname = myReader["uname"].ToString();

        unameList.Add(pname);
    }
    myReader.Close();

    if (!unameList.Contains(person.name))
    {
        string queryString = "INSERT INTO `users` (`uname`, `pword`, `email`) VALUES ('" + person.name + "', '" + person.password + "', '" + person.email + "');";
        MySqlCommand cmd = new MySqlCommand(queryString, conn);
        cmd.ExecuteNonQuery();
        postReult = "Added!";
    }
    else
    {
        postReult = "The user is already exsit!";
    }
    return postReult;
}

我的问题是如何获得不同的返回字符串&#34;已添加!&#34;或&#34;用户已经存在!&#34;作为API对客户的响应?

0 个答案:

没有答案