14

I am attempting to post data from an Android application running in the Android Emulator on my local machine to a web application running under IIS Express also running on my local machine. Unfortunately, when I post to 10.0.2.2 from the emulator I receive a Host Not Found error message.

If I configure the web application to run under ASP.NET Dev Server (Cassini) instead of IIS Express the Android application is able to post with no problems. What configuration am I missing for IIS Express that is preventing it from working with a loopback from the Android emulator?

ahsteele
  • 25,470
  • 26
  • 131
  • 238

4 Answers4

19

Grant yourself permission to bind to network adapters other than localhost, and configure IIS express to bind to all adapters.

IIS Express will then accept connections from the Android emulator via 10.0.2.2. If you add a rule to the firewall, you can also expose IIS Express to your network, which decreases security but is useful for testing physical devices.

Step details: (they assume a port number of 5555 - use your actual port instead)

  1. Run this from a command prompt as Administrator:

    netsh http add urlacl url=http://*:5555/ user="NT AUTHORITY\INTERACTIVE"

  2. In %USERPROFILE%\Documents\IISExpress\config\applicationhost.config, replace your site's localhost binding with bindingInformation="*:5555:*". The result should look like this:

    <site name="..." id="...">
        <!-- application settings omitted for brevity -->
        <bindings>
            <binding protocol="http" bindingInformation="*:5555:*" />
        </bindings>
    </site>
Edward Brey
  • 35,877
  • 14
  • 173
  • 224
  • 8
    In Visual Studio 2015 and maybe newer versions, the `applicationhost.config` file is no longer in the `%USERPROFILE%` dir, but instead in the %PROJECTDIR%\.VS\config\ dir. – Loyalar May 25 '16 at 11:02
  • It works but I have to do the whole procedure every time (after every restart). Possible to avoid that somehow? – MaTTo Jun 08 '16 at 09:11
  • @Orochi The configuration changes should be permanent. I haven't encountered needing to re-make the changes after a restart. – Edward Brey Jun 08 '16 at 15:15
  • I get an error in VS: Invalid URI: The hostname could not be parsed. Therefore, I can't event start WS. Also, every time I have to change port number. Any idea, how to fix it? – MaTTo Jun 09 '16 at 13:47
  • Your steps do not "expose IIS Express to your network", as you don't even change a **firewall** rule. I also wrote a blog post to show how to do everything visually using Jexus Manager, https://blog.lextudio.com/how-to-let-android-emulator-access-iis-express-f6530a02b1d3 instead of remembering all the commands. – Lex Li Jul 15 '17 at 03:55
  • Normally, connection requests coming from the Android emulator would appear to be of local origin, so one would think that they *would* be able to reach something only listening on the loopback interface, unlike requests coming from a physical Android device sitting on the same network. – Chris Stratton Jul 23 '17 at 20:30
  • @ChrisStratton Per [Android Emulator Networking](https://developer.android.com/studio/run/emulator-networking.html), "Each instance of the emulator runs behind a virtual router/firewall service that isolates it from your development machine network interfaces and settings and from the internet." Thus, the emulator does not appear to be of local origin. – Edward Brey Jul 24 '17 at 00:06
  • @EdwardBrey it may be a type of router from the Android side, but to the host it's supposed to just be a local process sourcing this traffic, otherwise "10.0.2.2 Special alias to your host loopback interface " on that page would be a false statement. The traffic is either injected into the loopback interface, or it isn't. It should be, because the page continues " If you want to access services running on your development machine loopback interface (a.k.a. 127.0.0.1 on your machine), you should use the special address 10.0.2.2" – Chris Stratton Jul 24 '17 at 01:21
  • @ChrisStratton Good point. Guest VMs use the local loopback interface. However, they're still untrusted as if they were remote. The only node trusted by default for access to your website is the host OS. So being on the loopback interface isn't enough. You have to be 127.0.0.1 to get through with the default firewall rule. – Edward Brey Jul 24 '17 at 11:25
  • The point is that the traffic *is* from the host OS, as it's regenerated by a program running there performing socket operations. This is not a normal virtual machine network bridge - the emulated Android device doesn't have an IP address, and can't accept inbound connections. If you look at whatever the equivalent of netstat is, these should be local connections. It's a lot like if you use an SSH tunnel - the traffic *is* remote, but it *looks* local. – Chris Stratton Jul 24 '17 at 14:58
12

Add following line to IIs config file (ex c:\Users[YourName]\Documents\IISExpress\config\applicationhost.config ) Change the port 8085 if required..

<binding protocol="http" bindingInformation="*:8085:127.0.0.1" />

so your config file will end-up with something like this

<bindings>
<binding protocol="http" bindingInformation="*:4396:localhost" />     // the existing one
<binding protocol="http" bindingInformation="*:8085:127.0.0.1" />     // new line
</bindings>

now you can call your web service from remote by calling to port 8085

ex from android emu.
new HttpPost("http://10.0.2.2:8085");
kuma DK
  • 1,694
  • 16
  • 33
  • 2
    that is the best solution for me. Nevertheless I have had to provide my actual IP address ( from your example: 10.0.2.2) instead of local looop – eldi May 02 '15 at 07:58
  • I replace the old one (aka deleted the *{port}:localhost) and works! thank you – nicolocodev Sep 06 '15 at 02:49
  • 1
    I found this article helpful to solve my localhost access problems: http://www.damirscorner.com/blog/posts/20140113-ConnectingToLocalIisExpressServerFromWp8Emulator.html – ahaaman Oct 21 '15 at 07:57
  • For Genymotion emulator, need to add `` and run web service on the same ip address and port no. Then only my emulator is connecting to my iis express (port no can be replaced with desired one). – Madhu Jul 06 '17 at 05:36
7

By default, IIS Express only accepts connections from localhost. To enable connections from remote devices (and the emulator counts as such), use the instructions from here.

In short:

netsh http add urlacl url=http://[machinename]:[port]/ user=everyone
netsh http delete urlacl url=http://[machinename]:[port]/

Replace [machinename] and [port] with your computer name (or non-local IP) and port IIS Express runs on.

Also, see this question and this one.

Community
  • 1
  • 1
Fammy
  • 493
  • 3
  • 13
  • 1
    Android emulator is not one of the "remote devices", so your answer is in the wrong direction. – Lex Li Jul 15 '17 at 03:57
0

Here is my solution: I am using Visual Studio Express 2013 for Web and my RESTful web service is running on IIS express. When I tried to access my web service using an Android emulator in the same machine it gave me this invalid hostname error. As suggested by above answers I did add new bindings to my applicationhost.config file but still it didn't work. At last, I was able to fix this issue by running Visual Studio "as administrator".

ChaturaM
  • 1,377
  • 16
  • 29
  • About why your steps happened to work for you, the details can be found in https://blog.lextudio.com/how-to-let-android-emulator-access-iis-express-f6530a02b1d3 Though of course your explanation above is almost useless as you don't even figure out what exact steps make it work. Probably you remove the host header part and then run VS as admin, then IIS Express worker process has all permissions to activate the site binding. – Lex Li Jul 15 '17 at 04:01