出现一个或多个错误.(无法解析TLS数据包标头)C#Xamarin Android Project Visual Stu



当我为API服务器使用http url时,我收到错误:

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

当我把https放在API服务器的url上时,我收到错误:

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

我的RestServiceData类看起来像:

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;
}
}
}

这是我的Constants类:

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

我能做些什么来修复这个错误?

  1. 如果您的网站没有任何证书配置,请将https更改为http

  2. 设置为允许http请求。

    添加您的xxx。安卓系统->属性->AndroidManifest.xml:android:usesCleartextTraffic="true"

  3. 如果以上方法不起作用,请在Resources文件夹,并添加network_security_config.xml文件。添加代码:

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

    添加您的xxx。安卓系统->属性->AndroidManifest.xml:

    android:networkSecurityConfig="@xml/network_security_config"
    

如果您想要使用http请求,您应该将android:usesCleartextTraffic="true"添加到AndroidManifest.xml中的应用程序标签中,该标签仅在API 23级及更高级别中使用(这足以进行测试(。你应该在线使用https请求,你需要一个域名,https将在你的服务器中使用443端口。通常,您需要Nginx或Apache来侦听443并连接自己的服务器。

相关内容

最新更新