Forum Xamarin.Forms
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions 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)
    {
        try
        {
            var result = await client.PostAsync(url, new StringContent(
                json,
                Encoding.UTF8,
                "application/json"));
            var x = await result.Content.ReadAsStringAsync();
            System.Diagnostics.Debug.WriteLine(x);
            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.....

Answers

Sign In or Register to comment.