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

Time:11-29

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("\t\tERROR {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 ?

CodePudding user response:

  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"
    

CodePudding user response:

If you should want to use the http request, you should add android:usesCleartextTraffic="true" to the application tag in the AndroidManifest.xml which only used in API level 23 and higher(This is enough for test). You should use the https request online, you need a domain name and https will use 443 port in your server. Usually, you need Nginx or Apache to listen 443 and connect your self server.

  • Related