PostAsync Problem

Hello,
I am trying to post to a backend server, data from mobile cross app with the HttpClient.PostAsync(string requestUri, HttpContent content); but all the time gives up unhandled exception. Please i would like a help.

Posts

  • voidstreamvoidstream FRMember ✭✭✭

    @tom1985 said:
    Hello,
    I am trying to post to a backend server, data from mobile cross app with the HttpClient.PostAsync(string requestUri, HttpContent content); but all the time gives up unhandled exception. Please i would like a help.

    Hello, you should drop code, we can't help you like this...

  • tom1985tom1985 GRMember

    This is the client side:

    private async void NextButton_Clicked(object sender, EventArgs e)
    {
    var client = new HttpClient();
    var content = new FormUrlEncodedContent(new[]
    {
    new KeyValuePair<string, string>("phone_num", PhoneNumberEntry.Text)
    });
    var result = await client.PostAsync("backend url", content);

            if (result.StatusCode == System.Net.HttpStatusCode.OK)
            {
                var definition = new { Success = true };
                var response = JsonConvert.DeserializeAnonymousType(result.Content.ReadAsStringAsync().Result, definition);
    
                if (response.Success)
                {
                    await Navigation.PushAsync(new TokenPage(client));
                }
                else
                {
                    await DisplayAlert("Phone Number problem", "You entered an invalid phone number, try again.", "OK");
                }
            }
            else
            {
                await DisplayAlert("Server Problem", "Did not receive successful response from backend.", "OK");
            }
        }
    

    and this is the

    backend code:

        [HttpPost]
        public ActionResult Validate(string phone_num)
        {
    
                bool validate=InsertIntodb(phone_num);
    
                if (validate >0)
                {
                    Session["PhoneNumber"] = phone_num;
                    Session["Token"] = SendSmsToken();
                    return Json(new { success = true });
                }
                else
                {
                    return Json(new { success = false });
                }
    
        }
    
Sign In or Register to comment.