5

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://learn.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?

5
  • Hi @Jimmy ,did you test it in a simulator or on a real device? And you can first recheck if you can access the website in your browser on your test device. May 23, 2022 at 7:07
  • @JessieZhang-MSFT using the default android emulator. It works fine in my MVC web app and also works with the Windows Machine debug option. It's just Android that doesn't work (I can't test ios).
    – Jimmy
    May 23, 2022 at 10:10
  • Maybe your issue is related to this stackoverflow.com/questions/6760585/…
    – FlaGon
    May 24, 2022 at 6:43
  • 1
    Use ngrok.com for that.
    – Xserge
    May 24, 2022 at 6:58
  • I find no way to debug the target for Windows also, thanks @Xserge ngrok worked for me
    – vCillusion
    Nov 5, 2022 at 3:45

1 Answer 1

3

@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.

3
  • Thanks Andre! Can't believe I didn't think of that! In the WebApi launchSettings.json the http url is configured to a different port. I updated the port of the android url accordingly and am no longer getting the unexpected end of stream error... though now I get the certificate error mentioned, even on http, weird.
    – Jimmy
    May 26, 2022 at 1:36
  • I still haven't gotten passed certificate issues though and have resigned to connecting to my live API when I'm testing on android...
    – Jimmy
    Jul 6, 2022 at 3:57
  • I used this answer to fix (bypass) the certificate issue: stackoverflow.com/a/71196389/1662619
    – Jimmy
    Jul 18, 2022 at 9:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.