0

I'm working on a simple Xamarin forms client application which accessing .Net WebApi Service. I tested the endpoint using postman it worked perfectly and returned data, but when I using Xamarin app to get data I'm getting an error

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

I mentioned the method which I'm using to get data from rest service

public static async Task<dynamic> getDataFromService(string pQueryString)
{
    HttpClient client = new HttpClient();
    var response = await client.GetAsync(pQueryString);

    dynamic data = null;
    if (response != null)
    {
        string json = response.Content.ReadAsStringAsync().Result;
        data = JsonConvert.DeserializeObject(json);
    }

    return data;
}
3
  • What is the InnerException / Stacktrace? Commented Mar 19, 2018 at 6:58
  • 1
    The actual query string could be helpful, too, if at any rate possible. Commented Mar 19, 2018 at 7:03
  • localhost:53269/GetUsers Commented Mar 19, 2018 at 8:45

1 Answer 1

2

When using 'localhost' from within a emulator or on a real device, you are accessing the emulator/device and not the development machine where you are probably running the api service. you need to enter a valid url to access the machine where the api is running.

if you tried running postman in the emulator or on a real device you would likely get the same error.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.