Xamarin Android错误(https,ssl,tls) [英] Xamarin Android error in (https, ssl, tls)

查看:141
本文介绍了Xamarin Android错误(https,ssl,tls)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在"Microsoft Visual Studio 2017" Xamarin Android应用中创建一个应用.

I try to create an app in "Microsoft Visual Studio 2017" Xamarin Android App.

如果我重新使用http而不是(https)可以正常工作

If i repelasd with http and not (https) it works fine

我的问题是:我该如何请求一个https呼叫?

My qustion is: How can i reguest a https call?

基本代码:

using Android.App;
using Android.Widget;
using Android.OS;
using Android.Support.V7.App;
using System.Net.Http;
using System;
using System.Text;
using System.Net;
using System.IO;

//System.Net.WebException: Error: TrustFailure (The authentication or decryption has failed.)

namespace httprequest
{
    [Activity(Label = "@string/app_name", Theme = "@style/AppTheme", MainLauncher = true)]
    public class MainActivity : AppCompatActivity
    {
        private static readonly HttpClient client = new HttpClient();
        Button btn1;

        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.activity_main);


            if (Build.Brand.Equals("GENERIC", StringComparison.InvariantCultureIgnoreCase))
            {
                ServicePointManager.ServerCertificateValidationCallback +=
                    (sender, cert, chain, sslPolicyErrors) => true;
            }


            btn1 = FindViewById<Button>(Resource.Id.httpbtn1);

            btn1.Click += delegate
            {
                using (var wb = new WebClient())
                {
                    var request = (HttpWebRequest)WebRequest.Create("https://???");

                    request.KeepAlive = false;
                    request.ProtocolVersion = HttpVersion.Version10;

                    var response = (HttpWebResponse)request.GetResponse();

                    var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();

                    Console.WriteLine(responseString);
                }
            };
        }
    }
}

这就是我得到的:

未处理的异常:System.Net.WebException:错误:TrustFailure(一个或更多错误发生.)

Unhandled Exception: System.Net.WebException: Error: TrustFailure (One or more errors occurred.)

在高级Adnroid属性中:

In Advanced Adnroid Properites:

HttpClient实现:Android

HttpClient implementation: Android

SSL/TLS实施:本机TLS 1.2 +

SSL/TLS implementation: Native TLS 1.2+

推荐答案

也许它仍在帮助OP或其他人在此问题上绊脚石:我想访问我们的公司API,该公司的API使用的是受信任的第三方通过连接到我们的Blackberry Server的Samsung S7(Android 8)颁发的证书.设备本身已安装了必要的证书,但是直到我像OP所说的那样切换HTTPS-Implementation之前,我仍然会遇到Trust Failure Exception异常:

Maybe it's still helping OP or someone else stumbling over this question: I wanted to access our company API that is using a certificate issued by a trusted Third Party via an Samsung S7 (Android 8) connected to our Blackberry Server. The device itself had the necessary certificate installed, but I still got the Trust Failure Exception until I switched the HTTPS-Implementation like OP said:

右键单击Android项目->设置-> Android选项->高级

Right click on the Android project -> Settings -> Android-Options -> Advanced

将HTTPClient实现更改为Android,将SSL/TLS实现更改为Native TLS 1.2 +

Change the HTTPClient implementation to Android and the SSL/TLS implementation to Native TLS 1.2+

然后我遇到以下错误:

java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.

因此,您似乎需要将Android指向自己的证书存储区.为此,请按照下列步骤操作:

So it seems you need to point Android to its own certificate stores. To do this follow these steps:

在资源"目录中创建子目录xml

Create a subdirectory xml in your Resources directory

在内部,使用以下内容创建network_security_config.xml

Inside, create network_security_config.xml with the following content

<?xml version="1.0" encoding="utf-8" ?>
 <network-security-config>
  <base-config>
   <trust-anchors>
    <!-- Trust preinstalled CAs -->
    <certificates src="system" />
    <!-- Additionally trust user added CAs -->
    <certificates src="user" />
  </trust-anchors>
 </base-config>
</network-security-config>

打开您的AndroidManifest.xml并将以下属性添加到application-Tag

Open your AndroidManifest.xml and add the following attribute to the application-Tag

<application [...] android:networkSecurityConfig="@xml/network_security_config">

以我为例,此后整个过程正常进行,没有错误.

In my case it got the whole thing working without errors afterwards.

这篇关于Xamarin Android错误(https,ssl,tls)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆