【问题标题】:Error on data posting in xamarin.forms在 xamarin.forms 中发布数据时出错
【发布时间】:2016-07-03 11:59:17
【问题描述】:

在 xamarin.forms 中使用 .net Web API 将数据发布到 sql 时出错

StatusCode: 204, ReasonPhrase: 'No Content', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:{Cache-Control: no-cache Pragma: no-cache Server: Microsoft-IIS/8.5 X-AspNet-Version: 4.0.30319 X-Powered-By: ASP.NET Access-Control-Allow-Headers: Content-Type Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS Date: Thu, 17 Mar 2016 08:32:28 GMT Expires: -1 }}

这是我发布数据的代码

 T returnResult = default(T);
        HttpClient client = null;
        try
        {
            client = new HttpClient();
            client.BaseAddress = new Uri(HostName);
            client.DefaultRequestHeaders.Add("Accept", "application/json");
            client.Timeout = new TimeSpan(0, 0, 15);
            HttpResponseMessage result = null;
            StringContent data = null;
            if (content != null)
                //     data = new StringContent(JsonConvert.SerializeObject(content), UTF8Encoding.UTF8, "application/json");
                data = new StringContent(JsonConvert.SerializeObject(content), Encoding.UTF8, "application/json");
            if (method == HttpMethod.Get)
                result = await client.GetAsync(endpoint);
            if (method == HttpMethod.Put)
                result = await client.PutAsync(endpoint, data);
            if (method == HttpMethod.Delete)
                result = await client.DeleteAsync(endpoint);
            if (method == HttpMethod.Post)
                result = await client.PostAsync(endpoint, data);
            if (result != null)
            {
                if (result.IsSuccessStatusCode
                                   && result.StatusCode == System.Net.HttpStatusCode.OK)
                {
                    var json = result.Content.ReadAsStringAsync().Result;
                    returnResult = JsonConvert.DeserializeObject<T>(json);
                }
            }

问题应该出在哪里?我的 API 工作正常,并且已启用 CORS

【问题讨论】:

标签: xamarin asp.net-web-api xamarin.forms


【解决方案1】:

这是我如何在代码中使用 PostAsync 的示例,我认为错误“无内容”指的是您可能没有向服务器发送任何内容?我希望这个例子有帮助:

public async Task<bool> PostAppointmet(AppointmentEntity anAppointment)
{
    try{
        HttpClient client = new HttpClient();
        client.DefaultRequestHeaders.Add("Authorization", "Bearer " + App.apiToken);

        const string resourceUri = ApiBaseAddress + "/citas";

        string postBody = JsonConvert.SerializeObject(anAppointment);

        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

        HttpResponseMessage response = await client.PostAsync (resourceUri, new StringContent (postBody, Encoding.UTF8, "application/json"));

        if (response.IsSuccessStatusCode) {
            return true;
        }

        return false;
    }
    catch{
        return false;
    }
}

AppointmentEntity 是我的模型:

public class AppointmentEntity
{
    public int doctor { get; set; }
    public PatientEntity paciente { get; set; }
    public DateEntity cita { get; set; }...
}

【讨论】:

  • 感谢您重播我。我发现我正在传递数据,我可以在调试时看到它。如果有人帮助我解决我的解决方案,那对我来说会更好。再次感谢@Mario Galván 的回答
【解决方案2】:

尝试这样使用

var task = client.PostAsync(endpoint,content:data);

【讨论】:

  • 我的问题出在 API 编码不正确,感谢您重播我。如果有人再次收到此错误,请确保 API 数据发布工作正常。我们可以使用 fiddler 或者我们在 chrome 中使用 Postman 来验证数据是否正在发布。邮递员帮助我解决这个问题也很简单。
猜你喜欢
  • 1970-01-01
  • 2011-08-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-03
相关资源
最近更新 更多