Search code examples
javascriptandroidcordovagoogle-chrome-devtoolsadb

App unable to make requests to localhost though port forwarding is working


I'm trying to debug my Android app built with cordova over USB.

My device is showing up in adb and chrome://inspect and I've port forwarded the device to connect to localhost:9000. I have confirmed that the port forwarding is working, since I can access localhost:9000 from Chrome on the device.

However when the app tries tp make a request to localhost:9000, I get a 404 (Not Found) error. I can copy the same address the app is trying to access and it opens correctly in Chrome on the device and also through the address bar of the webview inspector in Chrome devtools on my dev machine.

Why is my app unable to connect to localhost when Chrome can?

EDIT: The closest similar issue I could find was Android device communicate with local API server However the solution suggested, adding a CSP, didn't resolve my issue.

Some things I've tried to no avail:

  • Added a CSP
  • Updated the whitelist plugin
  • Added <allow-intent href="*" /> and <allow-navigation href="*" />
  • Updated the CLI version
  • Connecting with IP instead of localhost

I probably forgot something, I've been working on and off on this for a while now. Any and all suggestions welcome. Thanks!


Solution

  • I managed to resolve this by changing the bindings of the local server I was trying to connect to with the client.

    My server is using .NET and IIS Express for development, so I modified the applicationhost.config file from

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

    to

    <binding protocol="http" bindingInformation=":9000:" />
    

    as per Binding IIS Express to an IP Address. This allows me to connect to the server using 127.0.0.1:9000 or my machine's IP instead of just localhost:9000. I then modified my port forwarding in chrome://inspect to MYLOCALIP:9000 instead of localhost:9000 and configured the app to make requests to 127.0.0.1:9000 instead of localhost:9000.