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.

httppost fails trying to connect to localhost.

I have the following class in my pcl project:

public static class ServerRequest
static HttpClient client = new HttpClient();

    public static async Task<string> PostDataAsync(string url, string json)
            var result = await client.PostAsync(url, new StringContent(
            var x = await result.Content.ReadAsStringAsync();
            return x;
        catch (Exception e)
            return null;

I make the call using a command from my ModelView:

async Task Login()
IsBusyLogin = true;
var lu = new LoginUserDto();
lu.EmailAddress = "[email protected]";
lu.Password = "12345";

        var body = JsonConvert.SerializeObject(lu);

        var result = await ServerRequest.PostDataAsync("my local host url", body);

        IsBusyLogin = false;
        await Application.Current.MainPage.DisplayAlert("Save", "Save it", "Cancel");

I get this error message when trapping the PostDataAsync: System.Net.WebExceptionStatus.ConnectFailure. How do I get this to work using local host or must I deploy the api to a website?

Best Answer

  • JohnBairdJohnBaird USMember ✭✭
    Accepted Answer

    Well I guess I've struggled long enough with this and some fairy came buy and sprinkled dust on my project. IT IS FINALLY WORKING.....


Sign In or Register to comment.