问题
I try to create an app in "Microsoft Visual Studio 2017" Xamarin Android App.
If i repelasd with http and not (https) it works fine
My qustion is: How can i reguest a https call?
Basic code:
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);
}
};
}
}
}
Here is what i get:
Unhandled Exception: System.Net.WebException: Error: TrustFailure (One or more errors occurred.)
In Advanced Adnroid Properites:
HttpClient implementation: Android
SSL/TLS implementation: Native TLS 1.2+
回答1:
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:
Right click on the Android project -> Settings -> Android-Options -> Advanced
Change the HTTPClient implementation to Android and the SSL/TLS implementation to Native TLS 1.2+
Then I got the following error:
java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.
So it seems you need to point Android to its own certificate stores. To do this follow these steps:
Create a subdirectory xml in your Resources directory
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>
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.
来源:https://stackoverflow.com/questions/50784801/xamarin-android-error-in-https-ssl-tls