如何使用 body form-data 获取 Xamarin C# url 中的数据

大家好,我正在尝试通过使用 php Web 服务登录来从 url 下载数据,但我不知道该怎么做。如果我通过邮递员发出 POST 请求,在 BODY -> form-data 中插入电子邮件和密码,它会给出以下内容:


{“用户”:[{“电子邮件”:“email@test.it”,“昵称”:“昵称”,“图像”:“http://localhost/MyWebService/images/test_img.png “}]}


所以我在cs中创建了一个类,如下所示:


public class Users

    {

        [JsonProperty("users", NullValueHandling = NullValueHandling.Ignore)]

        public User[] UsersUsers { get; set; }

    }


    public class User

    {

        [JsonProperty("email", NullValueHandling = NullValueHandling.Ignore)]

        public string email { get; set; }


        [JsonProperty("nickname", NullValueHandling = NullValueHandling.Ignore)]

        public string nickname { get; set; }


        [JsonProperty("password", NullValueHandling = NullValueHandling.Ignore)]

        public string password { get; set; }


        [JsonProperty("image", NullValueHandling = NullValueHandling.Ignore)]

        public Uri image { get; set; }

    }

相反,在 botton 函数中,我尝试编写此代码来联系服务并进行调用:


async private void login(Object sender, EventArgs e)

        {

            string email = lbl_email.Text;

            string password = lbl_password.Text;

            string url = "http://192.168.178.77/TestLoginURL/api/login.php";



            var formContent = new FormUrlEncodedContent(new[]

            {

              new KeyValuePair<string, string>("email", email),

              new KeyValuePair<string, string>("password", password),

            });


            string contentType = "application/json";

            JObject json = new JObject

            {

                { "email", email},

                { "password", password }

            };

            HttpClient client = new HttpClient();

            var response = await client.PostAsync(url, new StringContent(json.ToString(), Encoding.UTF8, contentType));

            var data = await response.Content.ReadAsStringAsync();

            var users = JsonConvert.DeserializeObject<Users>(data);


        }

显然它被压垮了,给我的错误是这样的:Newtonsoft.Json.JsonReaderException 解析值时遇到意外字符:<。路径 '',第 0 行,位置 0。


那么,有人可以帮助我吗?我哪里错了?谢谢


MYYA
浏览 121回答 1
1回答

冉冉说

这样解决:async private void login(Object sender, EventArgs e){&nbsp; &nbsp; string email = lbl_email.Text;&nbsp; &nbsp; string password = lbl_password.Text;&nbsp; &nbsp; string url = "http://192.168.178.77/TestLoginURL/api/login.php";&nbsp; &nbsp; Users users = new Users();&nbsp; &nbsp; FormUrlEncodedContent formContent = new FormUrlEncodedContent(new[]&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; new KeyValuePair<string, string>("email", email),&nbsp; &nbsp; &nbsp; new KeyValuePair<string, string>("password", password),&nbsp; &nbsp; });&nbsp; &nbsp; HttpClient client = new HttpClient();&nbsp; &nbsp; client.DefaultRequestHeaders.Add("Accept", "application/json");&nbsp; &nbsp; try&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; CancellationTokenSource cts = new CancellationTokenSource();&nbsp; &nbsp; &nbsp; &nbsp; var responseMessage = client.PostAsync(url, formContent).Result;&nbsp; &nbsp; &nbsp; &nbsp; var data = await responseMessage.Content.ReadAsStringAsync();&nbsp; &nbsp; &nbsp; &nbsp; users = JsonConvert.DeserializeObject<Users>(data);&nbsp; &nbsp; &nbsp; &nbsp; lbl_nickname.Text = users.UsersUsers[0].nickname;&nbsp; &nbsp; }&nbsp; &nbsp; catch (Exception ex)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; ex.Message.ToString();&nbsp; &nbsp; &nbsp; &nbsp; throw;&nbsp; &nbsp; }}我将参数传递给 url 的方式实际上是错误的,因此它无法识别我发送的电子邮件和密码,从而导致错误。
打开App,查看更多内容
随时随地看视频慕课网APP