xamarin表单 - HttpClient发布请求postAsync不工作

时间:2017-03-30 06:21:11

标签: post xamarin.forms httpclient

我尝试从HttpClient发布数据,但它无法正常工作。

我收到如下错误消息:

{StatusCode:500,ReasonPhrase:'Internal Server Error',Version:1.1,Content:System.Net.Http.StreamContent,Headers: { 缓存控制:无缓存 Pragma:没有缓存 服务器:Microsoft-IIS / 10.0 X-AspNet-Version:4.0.30319 X-Powered-By:ASP.NET 日期:星期四,2017年3月30日06:10:50 GMT Content-Type:application / json;字符集= utf-8的 到期:-1 内容长度:36 }}

我的代码如下:

public void RefreshDataAsync(Persons selectedData)
        {
                using (var client = new HttpClient())
                {
                    var persons = new StringContent(JsonConvert.SerializeObject(selectedData), Encoding.UTF8, "application/json");
                    var httpResponseMessage = client.PostAsync("http://169.254.80.80:62510/api/Person/SavePerson", persons).Result;
                    if (httpResponseMessage.IsSuccessStatusCode)
                    {
                        var responceString = httpResponseMessage.Content.ReadAsStringAsync().Result;
                    }
                }
        }

获取数据方法如下:

        [HttpPost]
        public int SavePerson([FromBody]string persons)
        {

            JavaScriptSerializer js = new JavaScriptSerializer();
            var aa = JsonConvert.DeserializeObject<Persons>(persons);
            int Success = 0;
            using (SqlConnection con = new SqlConnection(ConnectionString))
            {
                using (var cmd = new SqlCommand("SavePerson", con))
                {
                    cmd.CommandType = CommandType.StoredProcedure;

                    cmd.Parameters.Add(new SqlParameter("@Name", SqlDbType.NVarChar, 100)).Value = aa.Name;
                    cmd.Parameters.Add(new SqlParameter("@Age", SqlDbType.Int)).Value = aa.Age;
                    var parm1 = new SqlParameter("@Success", SqlDbType.VarChar, 500);
                    parm1.Size = 500;
                    parm1.Direction = ParameterDirection.Output;
                    cmd.Parameters.Add(parm1);
                    cmd.Connection.Open();
                    object retValue = cmd.ExecuteNonQuery();

                    if (cmd.Parameters["@Success"].Value != DBNull.Value)
                        Success = Convert.ToInt32(cmd.Parameters["@Success"].Value);

                    cmd.Connection.Close();
                    return Success;
                }
            }
        }

我尝试使用“ HttpRequester ”工具运行,以便它正常运行。我在json字符串中得到响应。但是当我从模拟器调用此方法时,它无法正常工作。

0 个答案:

没有答案