【发布时间】:2017-01-25 12:01:50
【问题描述】:
我正在学习如何在 C# 中使用 xamarin,并且我有一个 android 应用程序可以向外部 API 发出 Web 请求,但我收到以下错误;
System.Net.Http.HttpRequestException: An error occurred while sending the
request ---> System.Net.WebException: Error: SecureChannelFailure (The
authentication or decryption has failed.) ---> System.IO.IOException: The
authentication or decryption has failed. ---> System.IO.IOException: The
authentication or decryption has failed. --->
Mono.Security.Protocol.Tls.TlsException: The authentication or decryption has failed.
我的代码如下;
public async Task<List<T>> GetList<T>(string url)
{
HttpClient client = new HttpClient();
var response = await client.GetAsync(new Uri(url));
response.EnsureSuccessStatusCode();
var content = await response.Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject<List<T>>(content);
}
android中的MainClass调用核心类,核心类调用这个服务类。
当 API 确实不使用 HTTPS 时,HttpClient 请求工作,但每当我使用启用了 HTTPS 的 API 时都会抛出此错误。
我还使用可移植类库来存储代码,因此无法使用 HttpWebRequest 进行 Web 请求。
我也试过了;
ServicePointManager.ServerCertificateValidationCallback += (o, certificate, chain, errors) => true;
和;
System.Security.Cryptography.AesCryptoServiceProvider b = new System.Security.Cryptography.AesCryptoServiceProvider();
在我的应用程序 OnCreate 类 (android) 中。
我还将 HttpClient 实现更改为 AndroidClientHandler,同时将 SSL/TLS 实现更改为 1.1。以及在模拟器和设备上尝试此操作。
有人有什么建议吗?
【问题讨论】:
-
你能解决这个问题吗?我遇到了同样的问题,修改 Android 项目对我有用。在 Android Options > Advanced 下,我将 HttpClient implementation 设置为 Android,将 SSL/TLS implementation 设置为 Native TLS 1.2+
-
我也得到了同样的上述错误@Yanbin Hu 很好的解决方案,除了将 HTTP 客户端实现从 Default 更改为 Android native 之外,还将 SSL/TLS 实现从 Default 更改为 - Native TLS 1.2+ - 然后它对我有用。
标签: c# xamarin mono dotnet-httpclient