One or more errors occurred. (Unable to parse TLS packet header) C# Xamarin Android Project Visual Studio

When I using http url for API server I receive error:

    Message "One or more errors occurred. (Cleartext HTTP traffic to 194.141.118.43 not permitted)" 

When I put https on the url for API server I receive error:

    Message "One or more errors occurred. (Unable to parse TLS packet header)"  

My RestServiceData class look like:

    using System;
using System.Diagnostics;
using System.Net.Http;
using System.Threading.Tasks;
using MaritsaTundzhaForecast.Models;
using Newtonsoft.Json;

namespace MaritsaTundzhaForecast.Services
{
    public class RestServiceData
    {
        HttpClient _client1;

        HttpClient _client2;

        public RestServiceData()
        {
            _client1 = new HttpClient();

            _client2 = new HttpClient();
        }

        public WaterBindingData GetWaterDataForecast(string query, string query2)
        {
            WaterDataJson waterData = new WaterDataJson();
            WaterStationsJson waterStations = new WaterStationsJson();

            WaterBindingData result = new WaterBindingData();
            try
            {

                var task = Task.Run(() => _client1.GetAsync(query));
                task.Wait();
                var response = task.Result;

                var task2 = Task.Run(() => _client2.GetAsync(query2));
                task2.Wait();
                var response2 = task2.Result;


                if (response.IsSuccessStatusCode && response2.IsSuccessStatusCode)
                {
                    var content = response.Content.ReadAsStringAsync().Result;
                    var content2 = response2.Content.ReadAsStringAsync().Result;

                    var json = content2.Replace(""ardaforecast":[[", ""ardaforecast":[ {"items": [")
                                                 .Replace("}],{"fieldCount"", "}],"details":{"fieldCount"")
                                                 .Replace("}]}", "}}]}");
                    waterData = JsonConvert.DeserializeObject<WaterDataJson>(content2);


                    waterStations = JsonConvert.DeserializeObject<WaterStationsJson>(content);

                    result.WaterData = waterData;
                    result.WaterStation = waterStations;
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine("ttERROR {0}", ex.Message);
            }

            return result;
        }
    }
}

This is my Constants class:

    using System;
using System.IO;

namespace MaritsaTundzhaForecast
{
    public static class Constants
    {
        public static string EndPoint = "https://194.141.118.43:3001/";
    }
}

What Can I do to fix this error ?

Answer

  1. If your website does not have any certificate configuration, change https to http.

  2. Set to allow http requests.

    Add in your xxx.Android->Properties->AndroidManifest.xml:android:usesCleartextTraffic="true".

  3. If the above method does not work,Create an xml folder under your Resources folder and add the network_security_config.xml file. Add code:

    <?xml version="1.0" encoding="utf-8"?>
    <network-security-config>
        <base-config cleartextTrafficPermitted="true"/>
    </network-security-config> 
    

    Add in your xxx.Android->Properties->AndroidManifest.xml:

    android:networkSecurityConfig="@xml/network_security_config"
    
Source: stackoverflow
The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .