dotnet / maui

.NET MAUI is the .NET Multi-platform App UI, a framework for building native device applications spanning mobile, tablet, and desktop.
https://dot.net/maui
MIT License
22.21k stars 1.75k forks source link

MAUI NET8.0 Android HTTPClient isn't working as intend, SendAsync() and GetAsync() send nothing. But works well on Windows #23993

Closed warrenZY closed 3 months ago

warrenZY commented 3 months ago

Description

I tried to kinds of writing but non of them work, while connecting to my ZTE device and debug.

image image

I need to send Get with customized cookie.

I tried:

    if (DeviceInfo.Current.Platform == DevicePlatform.Android)
    {

if ANDROID

        //HttpClientHandler handler = new HttpClientHandler();
        var Msghandler = new Xamarin.Android.Net.AndroidMessageHandler() { UseCookies = false };
        httpClient = new HttpClient(new Xamarin.Android.Net.AndroidMessageHandler());

endif

    }
    else
    {
        var httpClientHandler = new HttpClientHandler() { UseCookies = false };
        httpClient = new HttpClient(httpClientHandler);

    }
    //var httpRequestMessage = new HttpRequestMessage(HttpMethod.Get, uri);

var httpClientHandler = new HttpClientHandler() { UseCookies=false }; //httpClientHandler.ServerCertificateCustomValidationCallback = HttpClientHandler.DangerousAcceptAnyServerCertificateValidator; //httpClientHandler.AllowAutoRedirect = false; //var httpClient= new HttpClient(httpClientHandler); if (DeviceInfo.Current.Platform == DevicePlatform.Android) { var httpClient = new HttpClient(new Xamarin.Android.Net.AndroidMessageHandler()); } else { var httpClient = new HttpClient(httpClientHandler); }

var httpRequestMessage =new HttpRequestMessage(HttpMethod.Get, uri); httpRequestMessage.Headers.Add("Cookie", $"mobile_enterprise={mobileNumber}"); var ret= await httpClient.SendAsync(httpRequestMessage); ret.EnsureSuccessStatusCode(); return await ret.Content.ReadAsStringAsync();

Steps to Reproduce

No response

Link to public reproduction project repository

No response

Version with bug

8.0.71 SR7.1

Is this a regression from previous behavior?

Yes, this used to work in .NET MAUI

Last version that worked well

Unknown/Other

Affected platforms

Android

Affected platform versions

Android 13

Did you find any workaround?

No response

Relevant log output

Non Error log output in this situation
github-actions[bot] commented 3 months ago

Hi I'm an AI powered bot that finds similar issues based off the issue title.

Please view the issues below to see if they solve your problem, and if the issue describes your problem please consider closing this one and thumbs upping the other issue to help us prioritize it. Thank you!

Closed similar issues:

Note: You can give me feedback by thumbs upping or thumbs downing this comment.

warrenZY commented 3 months ago

The code just work right well in windows as image shows, it correctly sends the GET and get Response from server. but just sending noting well running on Android image In Android, when I press the send button, nothing happend. Debug shows that it just jumping back forth between Line 33,41,46 and never runs to Line 47 image

warrenZY commented 3 months ago

image This is what i captured from the network traffic(I open a hotspot and connect my phone through it), unlike the windows its just show nothing. The only request show that's come from my web browser well testing connectivity and ensure there's nternet Access.

warrenZY commented 3 months ago

I thought I find the real reason, Android system restricted clear text traffic causing this http Get Request cannot being Send. image Fix it by adding android:usesCleartextTraffic="true" at manifest

jonmdev commented 3 months ago

I thought I find the real reason, Android system restricted clear text traffic causing this http Get Request cannot being Send. image Fix it by adding android:usesCleartextTraffic="true" at manifest

Android intends you to only use https. If you try http url you get this problem. Change to https and it works.

warrenZY commented 3 months ago

Android intends you to only use https. If you try http url you get this problem. Change to https and it works.

That's right, but I did own that server. And the server only supports Http, instead of Https. What a mess, I'm not sure if there's any way else can solve this problem, Hope there would be an easier way to One Click Config. LOL!

sviluppomania commented 3 months ago

Hi, currently from Xamarin to .NET 8 MAUI these NuGet assemblies/packages are not compatible, what are the solutions?

Xamarin.Android.Google.BillingClient Xamarin.GooglePlayServices.Ads Xamarin.GooglePlayServices.Location Xamarin.Google.UserMessagingPlatform Xamarin.AndroidX.Browser Xamarin.AndroidX.Core Xamarin.AndroidX.Lifecycle.LiveData Xamarin.AndroidX.MediaRouter Xamarin.AndroidX.Palette Xamarin.AndroidX.RecyclerView Xamarin.GooglePlayServices.SafetyNet AdsJumbo

SkiaSharp.Views.Forms

Xamarin.Android.Support.Collections Xamarin.Android.Support.CustomTabs Xamarin.Android.Support.Interpolator Xamarin.Essentials Xamarin.Forms Xamarin.Android.Support.Design Xamarin.Android.Support.v7.AppCompat Xamarin.Android.Support.v4 Xamarin.Android.Support.v7.CardView Xamarin.Android.Support.v7.MediaRouter Xamarin.Essentials Xamarin.Forms Microsoft.NETCore.UniversalWindowsPlatform

Thanks so much to anyone who will help! 🙂

jfversluis commented 3 months ago

@sviluppomania please do not randomly ask questions in other issues.

@warrenZY unfortunately there is no other way. Both Android and iOS enforce using https unless you opt-out of it with what you just did. This is not a .NET MAUI thing, but rather something on the platform. Glad you figured it out!