0

We are developing an iOS shopping cart application in c# and Visual Studio 2017 for Xamarin. We are using rest web services, Here I could not call web services. when I call web service I am getting null response with an error[ConnectFailure (Connection refused)]. My question is How to get a value from localhost URL like [http://localhost:56207/api/Users/Raju/Password@123]. When I enter this URL in the browser I am getting true or false depending upon user and password validation.I request you to help me to resolve this issue.I paste the code in below:

 public class RestInterfaceImp : IRestLogin
    {

    HttpClient client;
    private const string WebServiceUrl = "http://localhost:56207/api/Users/Raju/Password@123";


    public async Task<List<User>> RefreshDataAsync()
    {

        try
        {
            var httpClient = new HttpClient();

            var resp = await httpClient.GetAsync(WebServiceUrl);


            if (resp.IsSuccessStatusCode)
            {
                var respStr = await resp.Content.ReadAsStringAsync();

               var listaAtletas = JsonConvert.DeserializeObject<List<User>>(respStr);

            }

        }
        catch (HttpRequestException e)
        {
            Debug.WriteLine(e.InnerException.Message);

        }
        return null;
    }

}
VahidShir
  • 2,066
  • 2
  • 17
  • 27
P.Thiyagu
  • 917
  • 2
  • 7
  • 15
  • You can get result in browser(from computer I believe) because your computer browser can reach that localhost URL, but how iOS app from either simulator or a real device is supposed to know and reach a URL in your computer? – VahidShir Feb 06 '18 at 16:58
  • According to this [link](https://stackoverflow.com/a/6077929/5941852): The iOS Simulator uses the host machine network so you should be able to just use localhost or your machines IP address, whichever IP your web service is listening on. – VahidShir Feb 06 '18 at 17:11
  • That said, will you get any result when entering that URL in iOS simulator(say Safari)? – VahidShir Feb 06 '18 at 17:15
  • I've never had success connecting to an api by just using localhost: but others are saying it's possible. – GBreen12 Feb 06 '18 at 17:26
  • Thank you so much, it's working now – P.Thiyagu Feb 10 '18 at 14:45

1 Answers1

0

Whenever we need to expose a local api to a simulator like you are doing, we use ngrok:

https://github.com/inconshreveable/ngrok

For some reason their website is down right now so it's possible they are no longer a thing but here is the url:

https://ngrok.com/

GBreen12
  • 1,832
  • 2
  • 20
  • 38