0

So, I have an API running in Visual Studio 2019, and I can make a request by typing in a url in the browser, like

https://192.168.1.192:44327/api/pickslips

But when I try to make such a call from code, like for instance:

HttpClient httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

var responseMessage = await httpClient.GetAsync(@"https://192.168.1.192:44327/api/pickslips");

var jsonResult = await responseMessage.Content.ReadAsStringAsync();
var slips = JsonConvert.DeserializeObject<IEnumerable<PickSlip>>(jsonResult);
return slips.ToList();

The application stops and exits at await GetAsync, without an error, and doesn't get to anything beyond it. However, the code works when I try to make a call to other APIs like some of the test ones you can find on the internet.

By the way, one of the reasons why it's not running on localhost is because I need it for Xamarin.

So how can I make this work?

  • 1
    is your server setup to accept requests from remote clients? Have you tested that the server is reachable from the device/simulator browser? – Jason Dec 02 '20 at 19:22
  • Are you able to trace through the client code and determine where exactly it is exiting and a configuration that is or isn't throwing an exception? – Grax32 Dec 02 '20 at 19:23
  • Try to use `https://localhost:44327/api/pickslips` or `http://localhost:44327/api/pickslips`. Do you get an exception? Please post exception details – NineBerry Dec 02 '20 at 19:23
  • @Jason I didn't set it up for that, how do I do that? – K. Arkadius Wowczuk Dec 02 '20 at 19:25
  • I don't know what webserver you're using, but google " enable remote requests" – Jason Dec 02 '20 at 19:28

2 Answers2

0

The application is unable to reach the IP address specific, throw an HTTP error, and the app crashes. To troubleshoot that you want to start with try/catch and read the error details. Most likely you are hitting one of two issues:

  1. Your device/sim is unable to reach the given IP Address
  2. You are trying to access your local HTTP server which doesn't allow remote connections

In the first case, you want to verify that your device on the same network and can reach the IP address specified, in the case of sim you want to make it can access the network interface. Similar issues were answered here (for Android).

In the second case, you want to make sure your local HTTP server configured to allow remote connections. The fact you can access it from a browser is great, try to access it from a browser on another computer in the same network. A similar issue was answered here.

Mando
  • 11,414
  • 17
  • 86
  • 167
0

If it's an emulator I don't think every emulator accesses the host through it's router's IP address, i.e. you should check to see for the emulator in question if it is in a sort of a VPN with your host machine and accesses it through an IP that's in a different class (usually 10.x.x.x if I remember) but it shouldn't work from the browser (on an emulator or device in question) so if that's the case, could you provide more info like is it an android or iOS, are you certainly accessing it through httpS and not http as you need to enable http on those recent mobile OS versions though the app's manifest?

Also if remote requests i.e. from other devices or emulator even are being blocked and just working locally then for a quick check try disabling your firewall, make sure it's not the problem, and enable it back on.

Hope something helps.

GI1
  • 116
  • 8