Forum Xamarin.Forms


The Xamarin Forums have officially moved to the new Microsoft Q&A experience. Microsoft Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

To create new threads and ask questions head over to Microsoft Q&A for .NET and get involved today.

"System.Net.WebException: 'Failed to connect to localhost/'" I got those error,

sjinsjin Member ✭✭

I am new for Xamarin. form,

I try to use Api, and integrate with my Xamarin form app, but cannot connect to my Xamarin App with Api.

but using Postman, directly input data to my Api working perfectly.

I got this message from Xamarin form.
"System.Net.WebException: 'Failed to connect to localhost/'"

My code below:

public class ApiServices
    public async Task<bool> RegisterAsync(string phone, string password, string confirmPassword)
        var client = new HttpClient();

        var model = new RegisterBindingModel
            phone = phone,
            password = password,
            passwordconfirm = confirmPassword
        var json = JsonConvert.SerializeObject(model);

        HttpContent content = new StringContent(json);

        content.Headers.ContentType = new MediaTypeHeaderValue("application/json"); 

        var response = await client.PostAsync("http://localhost:44391/api/login/", content);

        return response.IsSuccessStatusCode;

That is my code for connecting Api.

I don't know why i got coming from.

Thank you. guys.

Best Answer


Sign In or Register to comment.