简体   繁体   中英

Web API request error in xamarin forms

I am trying to develop an mobile application using asp.net web api and xamarin forms. Getting errors like this

System.Net.Http.HttpRequestException: An error occurred while sending the request

I developed all the application Mobile, Web in a same solution. Then Created web api with individual login using visual studio 2017. This is my source code for RegisterBinding model

RegisterBinding.cs

public class RegisterBindingModel
{
    public string Email { get; set; }

    public string Password { get; set; }

    public string ConfirmPassword { get; set; }
}

Then i created viewmodel for xamarin binding RegisterViewModel.cs

    class RegisterViewModel
    {
    ApiServices _apiserv = new ApiServices(); 
    public string Email { get; set; }
    public string Password { get; set; }
    public string ConfirmPassword { get; set; }
    public string Message { get; set; }

    public ICommand RegisterCommand {
        get
        {
            return new Command(async() =>
            {
                var isSucess = await _apiserv.RegisterAsync(Email, Password, ConfirmPassword);
                if (isSucess)
                {
                    Message = "Sucessfully Registered!";
                }
                else
                {
                    Message = "Try again";
                }
            });
        }
    }
}

Also i create apiservice to connect mobile app and web api service

ApiService.cs

public async Task<bool> RegisterAsync(string email, string password, string confirmPassword)
        {
            try
            {
                var client = new HttpClient();

                var model = new RegisterBindingModel
                {
                    Email = email,
                    Password = password,
                    ConfirmPassword = confirmPassword
                };

                var json = JsonConvert.SerializeObject(model);

                HttpContent _content = new StringContent(json);

                _content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");


                var response = await client.PostAsync("http://localhost:54996/api/Account/Register", _content);
                return response.IsSuccessStatusCode;

            }
            catch (System.Exception)
            {

                throw;
            }


        }

Finally my view for xamarin xaml is

RegisterPage.cs

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="CIDSCONMOB.Views.RegisterPage"
            xmlns:vm="clr-namespace:CIDSCONMOB.ViewModels">

    <ContentPage.BindingContext>
        <vm:RegisterViewModel/>
    </ContentPage.BindingContext>

    <StackLayout Orientation="Vertical">
        <Entry Text="{Binding Email}"
               Placeholder="Email"/>
        <Entry Text="{Binding Password}"
               Placeholder="Password"
               IsPassword="True"/>
        <Entry Text="{Binding ConfirmPassword}"
               Placeholder="Confirm Password"
               IsPassword="True"/>
        <Button Command="{Binding RegisterCommand}"
                 Text="Sign Up"/>
        <Label Text="{Binding Message}"/>
    </StackLayout>

</ContentPage>  

This is the Error when i click the register button

HTTP Exception

As i am very new to xamarin development please help me to find solution for this issue

The problem is most likely in the address you're using http://localhost:54996/api/Account/Register . When running on a simulator or actual device, think of it as an external machine. Thus, localhost will point to that simulator/device.

Retrieve the address of the machine where your server application is running on, make sure it is connected to the same network as your simulator/device, enter that as an address and try again.

The address will most likely start with 192.168.xx or 10.xxx .

Also, there is probably more detail in the InnerException , better check that first.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM