-1

If I do this like I would in a WinForms app (yes I know this is not a secure way to send passwords, I will fix it later):

 var req = WebRequest.CreateHttp($"http://localhost:58112/api/login?systemID=1091&login={login}&password={password}");
 var resp = (HttpWebResponse)req.GetResponse();

I get a timeout error, presumably because "localhost" refers to the Android virtual machine I'm running Xamarin on. So how do I refer to the actual local machine from the Android app? I was thinking maybe the hostname of my PC, but then it would vary depending on who is running the code so that would need to be another input field on the login page...

ekolis
  • 6,270
  • 12
  • 50
  • 101
  • 1
    use the IP address of the local server – Jason Jul 08 '19 at 21:38
  • Wouldn't that have the same problem as using the hostname, namely that it would have to change depending on who is running the code? – ekolis Jul 09 '19 at 03:45
  • 1
    Yes. But this is going to be true of any app that does not always talk to the same backend. Even if your users do setup their own server, it is not guaranteed that they will always use `localhost`, so in any event you will need to allow them to specify a server address – Jason Jul 09 '19 at 11:50
  • 1
    What you are looking for is the host IP **10.0.2.2**. The question has already been answered here (note that the IP only works in the Android emulator). https://stackoverflow.com/questions/5806220/how-to-connect-to-my-http-localhost-web-server-from-android-emulator-in-eclips – Thomas Glaser Jul 10 '19 at 06:52
  • That sounds useful; this is just a proof of concept app so it won't be running anywhere but an emulator for the time being! Once I create the final app it can be pointed to an actual server... – ekolis Jul 10 '19 at 13:22

1 Answers1

1

As you've discovered, localhost / 127.0.0.1 means "this host" and is only useful for talking to the same host that your app resides on. If you want to talk to another local machine from a separate Android device this won't help you.

If I'm understanding you correctly you're trying to do an HTTP Get request to a different server on the local network. I'm not sure there is a 'magic bullet' way of discovering services locally though many protocols have been developed to try to overcome this over the years including many based on DNS, UDP, multicast, Bonjour and previously discovery for SOAP services etc.

Many threads exist around best ways to discover services locally (including from from Android specifically) which may help you...

https://softwareengineering.stackexchange.com/questions/244728/best-strategy-to-discover-a-web-service-in-a-local-network

Device discovery in local network

I've not used them but a search turned up these (Simple Service Discovery Protocol, ZeroConf)...

https://github.com/Yortw/RSSDP

https://github.com/onovotny/Zeroconf

The simplest method could be to allow the user to provide a hint at runtime.. eg, ask for the IP of the server in a simple dialog on first run or in settings / preferences / config file etc. I've often used that for apps in order to speed up development with a view to introducing auto discovery later. YMMV of-course.

HTH

ps. Please go easy on me other StackOverflow'ers, this is my first ever reply :-)

markc
  • 11
  • 2