【问题标题】:Xamarin Android error in (https, ssl, tls)Xamarin Android 错误(https、ssl、tls)
【发布时间】:2018-06-10 14:38:33
【问题描述】:

我尝试在“Microsoft Visual Studio 2017”Xamarin Android 应用程序中创建一个应用程序。

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

我的问题是:如何请求 https 调用?

基本代码:

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(一 发生或更多错误。)

在高级 Adnroid 属性中:

HttpClient 实现:Android

SSL/TLS 实施:原生 TLS 1.2+

【问题讨论】:

    标签: android ssl https


    【解决方案1】:

    也许它仍在帮助 OP 或其他人绊倒这个问题: 我想通过连接到我们黑莓服务器的三星 S7 (Android 8) 访问我们公司的 API,该 API 使用由受信任的第三方颁发的证书。设备本身安装了必要的证书,但我仍然得到信任失败异常,直到我像 OP 所说的那样切换 HTTPS 实现:

    在Android项目上右键->设置->Android-Options->高级

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

    然后我得到以下错误:

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

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

    在你的 Resources 目录中创建一个子目录 xml

    在里面,创建network_security_config.xml,内容如下

    <?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

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

    在我的情况下,它让整个事情在之后没有错误。

    【讨论】:

      猜你喜欢
      • 2018-10-04
      • 2016-03-19
      • 2023-04-01
      • 2016-02-07
      • 2019-03-18
      • 2012-09-22
      • 1970-01-01
      • 2014-05-02
      • 1970-01-01
      相关资源
      最近更新 更多