亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何使用 body form-data 獲取 Xamarin C# url 中的數據

如何使用 body form-data 獲取 Xamarin C# url 中的數據

C#
MYYA 2023-08-20 14:22:35
大家好,我正在嘗試通過使用 php Web 服務登錄來從 url 下載數據,但我不知道該怎么做。如果我通過郵遞員發出 POST 請求,在 BODY -> form-data 中插入電子郵件和密碼,它會給出以下內容:{“用戶”:[{“電子郵件”:“[email protected]”,“昵稱”:“昵稱”,“圖像”:“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。那么,有人可以幫助我嗎?我哪里錯了?謝謝
查看完整描述

1 回答

?
冉冉說

TA貢獻1877條經驗 獲得超1個贊

這樣解決:


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";


    Users users = new Users();


    FormUrlEncodedContent formContent = new FormUrlEncodedContent(new[]

    {

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

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

    });


    HttpClient client = new HttpClient();

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


    try

    {

        CancellationTokenSource cts = new CancellationTokenSource();

        var responseMessage = client.PostAsync(url, formContent).Result;

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

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

        lbl_nickname.Text = users.UsersUsers[0].nickname;


    }

    catch (Exception ex)

    {

        ex.Message.ToString();

        throw;

    }

}

我將參數傳遞給 url 的方式實際上是錯誤的,因此它無法識別我發送的電子郵件和密碼,從而導致錯誤。


查看完整回答
反對 回復 2023-08-20
  • 1 回答
  • 0 關注
  • 147 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號