首页 > 解决方案 > 发送 JSON Put 方法的错误 404 - Xamarin.Forms

问题描述

我正在 Xamarin Forms 中开发一个移动应用程序,并且我的 Put 方法正在恢复 404 状态时遇到问题,但是如果我在 Postman 上提出请求,它运行良好......我看不到问题......

这是请求

        public async Task<UserHasGamification> AddPointsToUser(UserHasGamification userHasGamification)
        {
            HttpClient client = new HttpClient();
            client.BaseAddress = new Uri("http://192.168.2.202:8080/api/UserGamification");
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

            // Converte seu objeto para formato Json
            string json = JsonConvert.SerializeObject(userHasGamification);

            // Espera o resultado
            HttpResponseMessage response = await client.PutAsync("userHasGamification", new StringContent(json, Encoding.UTF8, "application/json"));

            if (response.IsSuccessStatusCode)
            {
                return userHasGamification;
            }
            return userHasGamification;
        }

这是我的

    public class UserHasGamification
    {
        public int UserID { get; set; }
        public string Level { get; set; }
        public string Points { get; set; }

    }

这是错误

{StatusCode: 404, ReasonPhrase: 'Not Found', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
{
  Cache-Control: no-cache
  Date: Sat, 29 Aug 2020 08:46:04 GMT
  Pragma: no-cache
  Server: Microsoft-IIS/10.0
  X-Android-Received-Millis: 1598690768707
  X-Android-Response-Source: NETWORK 404
  X-Android-Sent-Millis: 1598690768679
  X-AspNet-Version: 4.0.30319
  X-Powered-By: ASP.NET
  Content-Length: 122
  Content-Type: application/json; charset=utf-8
  Expires: -1
}}

get 方法工作正常……但 PUT 方法没有……我不太明白为什么。

有人可以帮帮我吗?

标签: jsonapixamarin.formsxamarin.androidrequest

解决方案


推荐阅读