MAUI Android本地安全Web服务:找不到证书路径

发布于 2025-01-22 02:01:14 字数 1547 浏览 2 评论 0原文

我有一个例外:

“ /cross-platform/deploy-test/connect to-local-web-services#bypass-the-certificate-security-check“ rel =“ nofollow noreferrer”> https://learn.microsoft.com/en-en-en-us/ Xamarin/cross-platform/deploy-test/connect to to-local-web-services#bypass-the-certificate-security-check

将其添加到我的代码中:

    private static HttpClient Client;
    private ReservationModel[] Reservations;

    protected override async Task OnInitializedAsync()
    {
        HttpClientHandler insecureHandler = GetInsecureHandler();
        HttpClient Client = new HttpClient(insecureHandler);

        var response = await Client.PostAsJsonAsync("https://10.0.2.2:7284/reservations",
        "{}");

        if (response.IsSuccessStatusCode)
        {
        Reservations = await response.Content.ReadAsAsync<ReservationModel[]>();
        }       
    }

    public HttpClientHandler GetInsecureHandler()
    {
        HttpClientHandler handler = new HttpClientHandler();
        handler.ServerCertificateCustomValidationCallback = (message, cert, chain, errors) =>
        {
            if (cert.Issuer.Equals("CN=localhost"))
                return true;
            return errors == System.Net.Security.SslPolicyErrors.None;
        };
        return handler;
    }

但是仍然得到同样的例外,我想知道它是否想知道它是否可以使这种方法起作用,而无需启用HTTP清除文本流量

问候

I'm having this exception: "java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.

I tried to follow this: https://learn.microsoft.com/en-us/xamarin/cross-platform/deploy-test/connect-to-local-web-services#bypass-the-certificate-security-check

Added this to my code:

    private static HttpClient Client;
    private ReservationModel[] Reservations;

    protected override async Task OnInitializedAsync()
    {
        HttpClientHandler insecureHandler = GetInsecureHandler();
        HttpClient Client = new HttpClient(insecureHandler);

        var response = await Client.PostAsJsonAsync("https://10.0.2.2:7284/reservations",
        "{}");

        if (response.IsSuccessStatusCode)
        {
        Reservations = await response.Content.ReadAsAsync<ReservationModel[]>();
        }       
    }

    public HttpClientHandler GetInsecureHandler()
    {
        HttpClientHandler handler = new HttpClientHandler();
        handler.ServerCertificateCustomValidationCallback = (message, cert, chain, errors) =>
        {
            if (cert.Issuer.Equals("CN=localhost"))
                return true;
            return errors == System.Net.Security.SslPolicyErrors.None;
        };
        return handler;
    }

But still getting the same exception, I wonder if it is possible to make this approach work, without having the need to Enable HTTP clear-text traffic?

Updated

Exception (sorry for using image but was not able to place text):
enter image description here

Regards

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文