.Net MAUI Android can't talk to API localhost

Viewed 1709

I've downloaded the latest sample MAUI here: https://github.com/microsoft/dotnet-podcasts and copied the exact way they make requests but I can't get passed an 'unexpected end of stream error':

System.Net.WebException: unexpected end of stream on com.android.okhttp.Address@4c288121
 ---> Java.IO.IOException: unexpected end of stream on com.android.okhttp.Address@4c288121

I'm initialising the HttpClient in the MauiProgram.cs via the AddHttpClient factory method like so (note the different IP address for Andorid):

public static string Base = DeviceInfo.Platform == DevicePlatform.Android ? "http://10.0.2.2" : "https://localhost";
public static string APIUrl = $"{Base}:7096/api/";

builder.Services.AddHttpClient<DishClient>(client => { client.BaseAddress = new Uri(APIUrl); });

I've included the following android:networkSecurityConfig in the AndroidManifest.xml (which allows http traffic):

<network-security-config>
  <base-config cleartextTrafficPermitted="true">
        <trust-anchors>
            <certificates src="system" />
        </trust-anchors>
    </base-config>
</network-security-config>

The solution in Xamarin it seems is to use AndroidClientHandler but this is in a deprecated library that I can't use in MAUI, and there's no reference to it in the linked solution that works.

Things I've tried:

using Https: I've followed the guide here: https://docs.microsoft.com/en-us/xamarin/cross-platform/deploy-test/connect-to-local-web-services#bypass-the-certificate-security-check but I still get certificate errors:

   System.Net.WebException: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.
   ---> Javax.Net.Ssl.SSLHandshakeException: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.

I've also tried this

handler.ServerCertificateCustomValidationCallback = HttpClientHandler.DangerousAcceptAnyServerCertificateValidator; 

but get an error

System.PlatformNotSupportedException: Operation is not supported on this platform

It's been 4 days non-stop and i've gone through every conceivable setting in the working solution and can't find any differences. Any ideas?

1 Answers

@Jimmy in your code, you're changing the URL from "https://localhost" to "http://10.0.2.2" when you're running on android. In both cases, you're calling the API at port 7096: public static string APIUrl = $"{Base}:7096/api/";. In the case of android, you're using the HTTP protocol, and in all other cases, you're using HTTPS.

I assume that your server listens to HTTPS only on port 7096 and not to HTTP. Maybe this causes your issue.

Another problem could be that your service is only bound to localhost but not to your IP address (you can look this up in the launchsettings.json of your API).

Another issue can be that your firewall doesn't allow incoming traffic on port 7096. You should check that, too, because you're not crossing machine borders when you're running on Windows. However, you have a cross-machine communication when running on the Android emulator.

Related