Android Emulator loopback to IIS Express does not work, but does work with Cassini

12,425

Solution 1

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>

Solution 2

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");

Solution 3

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.

Share:
12,425
ahsteele
Author by

ahsteele

Professional success influenced by practical academics has shaped my outlook on software development. I have never stopped learning and am always interested in refining my development processes. For me it is not just the programming, all aspects of software development excite me.

Updated on June 15, 2022

Comments

  • ahsteele
    ahsteele almost 2 years

    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?

  • eldi
    eldi about 9 years
    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
  • nicolocodev
    nicolocodev over 8 years
    I replace the old one (aka deleted the *{port}:localhost) and works! thank you
  • ahaaman
    ahaaman over 8 years
    I found this article helpful to solve my localhost access problems: damirscorner.com/blog/posts/…
  • Loyalar
    Loyalar almost 8 years
    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.
  • MaTTo
    MaTTo almost 8 years
    It works but I have to do the whole procedure every time (after every restart). Possible to avoid that somehow?
  • Edward Brey
    Edward Brey almost 8 years
    @Orochi The configuration changes should be permanent. I haven't encountered needing to re-make the changes after a restart.
  • MaTTo
    MaTTo almost 8 years
    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?
  • Madhu
    Madhu almost 7 years
    For Genymotion emulator, need to add <binding protocol="http" bindingInformation="*:8085:192.168.56.1" /> 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).
  • Lex Li
    Lex Li almost 7 years
    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, blog.lextudio.com/… instead of remembering all the commands.
  • Lex Li
    Lex Li almost 7 years
    Android emulator is not one of the "remote devices", so your answer is in the wrong direction.
  • Lex Li
    Lex Li almost 7 years
    About why your steps happened to work for you, the details can be found in blog.lextudio.com/… 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.
  • Chris Stratton
    Chris Stratton almost 7 years
    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.
  • Edward Brey
    Edward Brey almost 7 years
    @ChrisStratton Per Android Emulator Networking, "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.
  • Chris Stratton
    Chris Stratton almost 7 years
    @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"
  • Edward Brey
    Edward Brey almost 7 years
    @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.
  • Chris Stratton
    Chris Stratton almost 7 years
    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.