小编Mon*_*ica的帖子

xamarin.android 添加客户端证书

我正在尝试向 Xamarin.Android 中的 Web api 发送请求。该 API 需要客户端证书。我遵循了这个问题中的建议:xamarin.ios httpclient clientcertificate not work with https,但我收到“方法未实现”异常。有人可以帮忙吗?

这是我的代码:

    string result = await CallApi(new System.Uri("myurl"));

    protected async Task<string> CallApi(Uri url)
    {
        try
        {
            AndroidClientHandler clientHandler = new AndroidClientHandler();
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Ssl3;

            using (var mmstream = new MemoryStream())
            {
                Application.Context.Assets.Open("mycert.pfx").CopyTo(mmstream);
                byte[] b = mmstream.ToArray();

                X509Certificate2 cert = new X509Certificate2(b, "password", X509KeyStorageFlags.DefaultKeySet);
                clientHandler.ClientCertificates.Add(cert);
            }

            ServicePointManager.ServerCertificateValidationCallback += new RemoteCertificateValidationCallback((sender, certificate, chain, policyErrors) => { return true; });

            HttpClient client …
Run Code Online (Sandbox Code Playgroud)

android xamarin

2
推荐指数
1
解决办法
6796
查看次数

标签 统计

android ×1

xamarin ×1