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

5

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?

Pratincole answered 31/5, 2011 at 19:47 Comment(0)
D
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>
Dunleavy answered 23/9, 2013 at 12:8 Comment(10)
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.Bicephalous
It works but I have to do the whole procedure every time (after every restart). Possible to avoid that somehow?Interpose
@Orochi The configuration changes should be permanent. I haven't encountered needing to re-make the changes after a restart.Dunleavy
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?Interpose
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.Ivyiwis
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.Frankish
@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.Dunleavy
@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"Frankish
@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.Dunleavy
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.Frankish
P
11

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");
Pelf answered 18/10, 2014 at 6:57 Comment(4)
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 looopSugden
I replace the old one (aka deleted the *{port}:localhost) and works! thank youEjection
I found this article helpful to solve my localhost access problems: damirscorner.com/blog/posts/…Dwayne
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).Optimism
L
8

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.

Lucylud answered 31/5, 2011 at 20:37 Comment(1)
Android emulator is not one of the "remote devices", so your answer is in the wrong direction.Ivyiwis
S
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".

Strenta answered 10/12, 2014 at 1:6 Comment(1)
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.Ivyiwis
W
0

You need to add a new binding for your PC name or change a binding to *:<port>:* or :<port>: and then allow that port using Windows Firewall with Advanced Security.

File to look into is <solution folder>\.vs\<project name>\config\applicationhost.config around line 167

Wesla answered 5/12, 2021 at 12:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.