Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: "Bad Request-Invalid Hostname" When accessing localhost from emulators

I am not able to use localhost with xamarin.android application. I have executed Web project & Service project(not mobile app) on Chrome Browser.

This is emulators executed project

The displayed port number I tried browsing in Android Studio emulators & Genymotion emulators by appending 10.0.2.2: & 10.0.3.2: respectively, I got result Bad Request-Invalid Hostname. Even I have tried Service project port number, both not working.

Web Project: ASP.NET project using Visual Studio 2017 & Having service project in the same solution.

like image 956
R15 Avatar asked Mar 14 '18 05:03

R15


2 Answers

Open your solution folder. You might have several projects inside 1 folder, you don't need project's folder but the root one. Inside you'd have .vs/config/applicationhost.config file, open it.

find <sites> inside you'll have sites that you are running, might have several, if you switch startup projects in one solution. The name will be corresponding to one displayed by IIS in taskbar when you right-click it's icon. The tag could be, for example, <site name="ServerApp" id="2">. Now you need to add a line to bind external connections to your machine ip, for example

      <site name="ServerApp" id="2">
...
        <bindings>
...
          <binding protocol="http" bindingInformation="*:60424:127.0.0.1" />
        </bindings>
      </site>

Set appropriate port number and protocol, what matters here is just map them to 127.0.0.1, not "localhost" then they will be available for android emulator via 10.0.2.2:60424 (the port number 60424 is just from the example above, use yours)

like image 145
Nick Kovalsky Avatar answered Oct 26 '22 07:10

Nick Kovalsky


You can use our free extension Conveyor https://marketplace.visualstudio.com/items?itemName=vs-publisher-1448185.ConveyorbyKeyoti

If the IP that Conveyor gives you doesn't work, just substitute it with 10.0.2.2. Using Conveyor solves the problem of needing to make IIS Express bind to other hostnames. It also has some other features too.

like image 44
Jim W says reinstate Monica Avatar answered Oct 26 '22 07:10

Jim W says reinstate Monica