Use HttpClient in Maui

scixing 21 Reputation points
2021-12-10T01:38:50.83+00:00

When I use HttpClient in MauiApp. It works well in the Emulator, but not on the mobile phone, Why is that?(android) its a bug(?

 private void OnCounterClicked(object sender, EventArgs e)
        {
//            HttpClient httpClent = CreateHttpClient();
//#if __ANDROID__
//                        httpClent = new HttpClient(new Xamarin.Android.Net.AndroidMessageHandler());
//#endif

            HttpClient httpClent = new();
            var res = httpClent.GetAsync($"https://xxxx.xxx/Image/Pochama/pocha-{count}.jpg");
            //PokeImg.Source = ImageSource.FromStream(() => new MemoryStream(res.Content.ReadAsByteArrayAsync().Result));

            //File.WriteAllBytes($"{count}.png", res.Content.ReadAsByteArrayAsync().Result);
            //var stream1 = new MemoryStream(imageAsBytes);
            //PokeImg.Source = $"pocha_{count % 88}.png";
            count++;
            CounterLabel.Text = $"Current count: {count}";

            SemanticScreenReader.Announce(CounterLabel.Text);
        }
.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
2,903 questions
{count} votes

4 answers

Sort by: Most helpful
  1. Ken Tucker 5,846 Reputation points
    2021-12-10T01:47:57+00:00

    In the android projects properties make sure you select TLS 1.2

    156376-image.png


  2. Stefan Kelchtermans 6 Reputation points
    2022-11-10T14:45:28.61+00:00

    Use in code
    System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

    That dit it for me.


  3. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

  4. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more