PDA是android系统,调用金蝶云星空webapi实现仓库收发料,使用读取webapi有些功能无法实现,需要直接读写数据库,读取报错,如图:
用控制台程序测试正常读取
google搜索了一圈, 都需要使用ssl证书才能解决,终于找到不用证书的解决办法,参考地址
c# - "Trust anchor for certification path not found." in a .NET Maui Project trying to contact a local .NET WebApi - Stack Overflow
操作步骤:
在Platforms\Android下新建DangerousTrustProvider.cs文件,如下图
添加命名空间
using System;
using Java.Net;
using Java.Security;
using Java.Security.Cert;
using Javax.Net.Ssl;
添加代码
internal class DangerousTrustProvider : Provider
{
private const string TRUST_PROVIDER_ALG = "DangerousTrustAlgorithm";
private const string TRUST_PROVIDER_ID = "DangerousTrustProvider";
public DangerousTrustProvider() : base(TRUST_PROVIDER_ID, 1, string.Empty)
{
var key = "TrustManagerFactory." + DangerousTrustManagerFactory.GetAlgorithm();
var val = Java.Lang.Class.FromType(typeof(DangerousTrustManagerFactory)).Name;
Put(key, val);
}
public static void Register()
{
Provider registered = Security.GetProvider(TRUST_PROVIDER_ID);
if (null == registered)
{
Security.InsertProviderAt(new DangerousTrustProvider(), 1);
Security.SetProperty("ssl.TrustManagerFactory.algorithm", TRUST_PROVIDER_ALG);
}
}
public class DangerousTrustManager : X509ExtendedTrustManager
{
public override void CheckClientTrusted(X509Certificate[] chain, string authType, Socket socket) { }
public override void CheckClientTrusted(X509Certificate[] chain, string authType, SSLEngine engine) { }
public override void CheckClientTrusted(X509Certificate[] chain, string authType) { }
public override void CheckServerTrusted(X509Certificate[] chain, string authType, Socket socket) { }
public override void CheckServerTrusted(X509Certificate[] chain, string authType, SSLEngine engine) { }
public override void CheckServerTrusted(X509Certificate[] chain, string authType) { }
public override X509Certificate[] GetAcceptedIssuers() => Array.Empty<X509Certificate>();
}
public class DangerousTrustManagerFactory : TrustManagerFactorySpi
{
protected override void EngineInit(IManagerFactoryParameters mgrparams) { }
protected override void EngineInit(KeyStore keystore) { }
protected override ITrustManager[] EngineGetTrustManagers() => new ITrustManager[] { new DangerousTrustManager() };
public static string GetAlgorithm() => TRUST_PROVIDER_ALG;
}
}
复制上面的代码,粘贴替换,如图
完整代码如图
打开,MauiProgram.cs增加调用代码
#if ANDROID
Platforms.Android.DangerousTrustProvider.Register();
#endif
如图
再次用模拟器测试,读取成功