最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

android - Connection Fail when attempting to access Localhost from Maui App - Stack Overflow

programmeradmin2浏览0评论

Gotta say, this has me stumped. When calling a web service (ASP.NET) from its production URL (;param2=y), everything works fine.

When attempting to access the same url from its local IIS server address from Postman (http://localhost:portnum/myurl?param1=x&param2=y) everything also triggers properly.

BUT, when trying the same PostAsync call from within my Maui app, I get a connection failure (and a debug point set on the IIS server instance never triggers).

Things I've tried:

  • Changing localhost to my particular computer IP (10.0.1.151)

  • Adding a network_security_config.xml file

     <?xml version="1.0" encoding="utf-8"?>
     <network-security-config>
       <domain-config cleartextTrafficPermitted="true">
         <domain includeSubdomains="true">10.0.0.2</domain>
         <domain includeSubdomains="true">10.0.1.151</domain>
         <domain includeSubdomains="true">localhost</domain>
       </domain-config>
     </network-security-config>
    
  • With an associated reference in my AndroidManifest.xml file:

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="; android:installLocation="auto">
        <application android:allowBackup="false"   android:networkSecurityConfig="@xml/network_security_config" android:supportsRtl="true" android:label="MyApp" android:icon="@mipmap/myApp_app_icon"></application>
    </manifest>
    

All of this succeeded only in changing my "Connection Error" into a "Connection Error (task cancelled) as the task timed out (without ever hitting my debug point in IIS).

My firewall is set so that all internal traffic on all ports is routed without interference (which is why Postman can connect without issue, as can other Visual Studio forms apps, etc.).

I am unable to connect to either via either the emulator or with a physical device.

What am I missing?

(Trying to connect over https experiences the same timeout/task cancelled problem)

Gotta say, this has me stumped. When calling a web service (ASP.NET) from its production URL (https://myhost.com/appname/myurl?param1=x&param2=y), everything works fine.

When attempting to access the same url from its local IIS server address from Postman (http://localhost:portnum/myurl?param1=x&param2=y) everything also triggers properly.

BUT, when trying the same PostAsync call from within my Maui app, I get a connection failure (and a debug point set on the IIS server instance never triggers).

Things I've tried:

  • Changing localhost to my particular computer IP (10.0.1.151)

  • Adding a network_security_config.xml file

     <?xml version="1.0" encoding="utf-8"?>
     <network-security-config>
       <domain-config cleartextTrafficPermitted="true">
         <domain includeSubdomains="true">10.0.0.2</domain>
         <domain includeSubdomains="true">10.0.1.151</domain>
         <domain includeSubdomains="true">localhost</domain>
       </domain-config>
     </network-security-config>
    
  • With an associated reference in my AndroidManifest.xml file:

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android" android:installLocation="auto">
        <application android:allowBackup="false"   android:networkSecurityConfig="@xml/network_security_config" android:supportsRtl="true" android:label="MyApp" android:icon="@mipmap/myApp_app_icon"></application>
    </manifest>
    

All of this succeeded only in changing my "Connection Error" into a "Connection Error (task cancelled) as the task timed out (without ever hitting my debug point in IIS).

My firewall is set so that all internal traffic on all ports is routed without interference (which is why Postman can connect without issue, as can other Visual Studio forms apps, etc.).

I am unable to connect to either via either the emulator or with a physical device.

What am I missing?

(Trying to connect over https experiences the same timeout/task cancelled problem)

Share Improve this question asked Jan 19 at 16:44 pbickfordpbickford 892 silver badges16 bronze badges 5
  • To access localhost from Android emulator try 10.0.2.2 address – Rafael Commented Jan 19 at 16:51
  • localhost tells Android to connect to itself, not to the host computer the emulator is running on. Additionally, you need to be sure IIS is setup to allow connections from remote clients – Jason Commented Jan 19 at 20:42
  • Trying the emulator with 10.0.2.2 (had typo above in routing sample) resulted in a 400- bad request. No joy attempting to the hardware testing device using any method. @Jason, can you clarify what additional setup might be necessary in IIS? As previous, Postman is able to trigger the correct IIS code with a post to localhost:port/params. Trying with just the IP:port/params results in the same 400-bad request. Is there some needed mapping of the hostname that must be done? – pbickford Commented Jan 19 at 21:10
  • google.com/… – Jason Commented Jan 19 at 22:24
  • What does your CORS policy look like? – D3vtr0n Commented Jan 20 at 6:53
Add a comment  | 

2 Answers 2

Reset to default 0

As already pointed out; connecting to localhost will never work since that will look for the server on the device you're running on. That could work for Windows or macOS, but not for Android or iOS.

Official documentation on how to achieve this and things to watch out for can be found here.

An easier alternative might be to use Visual Studio Dev Tunnels, I have a video on there here. With Dev Tunnels you basically create a tunnel from your machine to the outside world, exposing your server to the public. That makes it much easier to connect your device from anywhere, including your own machine.

An older solution and alternative to Dev Tunnels is ngrok.

Thanks to all for your help!

The solution I ultimately went with was to use iisexpress-proxy to set my IIS Express's local port to one which was (temporarily) remotely accessible. This is the same general solution as Dev Tunnels and ngrok, which likely also would have worked--I just arrived at a working solution with this solution first.

The particulars for me required:

  • installing node.js (https://nodejs.org/) - Run the Windows installer
  • Make sure the installed NodeJs folder is in my Paths System Variable: C:\Program Files\nodejs\
  • Install iisexpress-proxy as a global npm module with npm install -g iisexpress-proxy
  • Activate the proxy by opening a command line and typing iisexpress-proxy localPort to proxyPort (e.g. iisexpress-proxy 54449 to 3000 to map my IIS Express local development port 54449 to 3000)
  • Have my Android app access the testing url at LocalHostIP:3000/myurl?param1=x&param2=y

Only real downside to this approach I see is the need to remember to create the proxy every time I test this way (likely create a one-click batch file to do the iisexpress-proxy command). This seems like a mild hassle, but I'll admit to being slightly nervous about any alternatives which would automatically create a link to my dev machine which might be visible to the outside world. Please advise if I'm being overly nervous about this, or if you have a better development path for such work. In any case, thanks again for all your help!

发布评论

评论列表(0)

  1. 暂无评论