Trying to access a locally-hosted ASP.net service from the Android emulator - not exposed on 127.0.0.1?

android, asp.net, iis, rest, visual-studio

Solution

The issue is caused by IIS Express binding by hostname, and the host name is set by Visual Studio as "localhost". Therefore "127.0.0.1" gives a "Error 400. bad reqest (invalid hostname)".

Solving this requires a few steps.

- Modify the applicationhost.config

This is the configuration file for IIS Express, and should be located in the root of your ASP.NET project. If this file can not easily be found, right click on the IIS express in the task bar, click on "Show All Applications", and the registered sites should be in a list. Click on the site you are trying to access. Two File paths should be listed at the bottom, "Path" and "Config". The config is the location to your applicationhost.config file.

The binding information would have the following format:

 <binding protocol="http" bindingInformation="*:17687:localhost" />

The colon separated values in the bindingInformation represent the IP address to listen to, the port, and the host name. You can remove the hostname to avoid IIS Express expecting "localhost" in the request header field:

 <binding protocol="http" bindingInformation="*:17687:" />

- Exit out of IIS Express

IIS Express needs to be restarted in order for this config change to take place. It can either be closed via right click on IIS in the task bar, and choose "Exit" or by choosing the specific site (most times there will only be 1, unless there are multiple web type projects - ASP.NET,WCT, etc.) and clicking "Stop" and then "Start". But Exiting out of it totally would be better as additional configuration might have to be done, and the PC might need a reboot anyway.

- Firewall

The port that the test site uses should be included in the firewall. In the case above, the port address would be 17687. Open the firewall settings and add a inbound rule to allow this TCP port. At that point, there might need to be a reboot. At the minimum, the project should be stopped and restarted to cause a restart of the IIS Express.

Problem

I'm running an ASP.net service running through Visual Studio. It's accessible via browser at (example API call) `http://localhost:9000/api/Maps/1`. When I visit `http://127.0.0.1:9000/api/Maps/1` via browser, I get a 400 error (Bad Request - Invalid Hostname). The Android emulator can successfully make calls to external sites (e.g. `http://httpbin.org/ip`). When I direct the app to pull JSON from `http://10.0.2.2:9000/api/Maps/1` (10.0.2.2 being what the web tells me directs the emulator to the host computer's localhost, the result is the HTML for (what I assume is) the same 400 error from 127.0.0.1 above. It seems when I run the web service from Visual Studio (2013, if that makes a difference), it's exposed properly on localhost, but not 127.0.0.1, and is therefore not accessible for testing. Can anyone help me to resolve this? Thanks!

Original source