Xamarin 中的 WebException,使用 HttpClient
WebException in Xamarin, using HttpClient
我正在学习如何将 xamarin 与 C# 一起使用,我有一个 android 应用程序可以向外部 API 发出网络请求,但我收到以下错误;
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调用核心class,核心class调用此服务class。
当 API 不 使用 HTTPS 时,HttpClient 请求工作,但每当我使用启用了 HTTPS 的 API 时抛出此错误。
我还使用便携式 Class 库来存储代码,因此无法使用 HttpWebRequest 进行网络请求。
我也试过了;
ServicePointManager.ServerCertificateValidationCallback += (o, certificate, chain, errors) => true;
和;
System.Security.Cryptography.AesCryptoServiceProvider b = new System.Security.Cryptography.AesCryptoServiceProvider();
在我的应用程序 OnCreate class (android).
我还将 HttpClient 实现更改为 AndroidClientHandler,同时将 SSL/TLS 实现更改为 1.1。在模拟器和设备上尝试这个。
有人有什么建议吗?
试试这个:
打开 Android 项目 属性 -> Android 选项 -> 高级
将 HttpClient 实现 设置从 默认 切换到 Android
HTTPS 的默认 Mono 客户端处理程序似乎已损坏。
我正在学习如何将 xamarin 与 C# 一起使用,我有一个 android 应用程序可以向外部 API 发出网络请求,但我收到以下错误;
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调用核心class,核心class调用此服务class。
当 API 不 使用 HTTPS 时,HttpClient 请求工作,但每当我使用启用了 HTTPS 的 API 时抛出此错误。
我还使用便携式 Class 库来存储代码,因此无法使用 HttpWebRequest 进行网络请求。
我也试过了;
ServicePointManager.ServerCertificateValidationCallback += (o, certificate, chain, errors) => true;
和;
System.Security.Cryptography.AesCryptoServiceProvider b = new System.Security.Cryptography.AesCryptoServiceProvider();
在我的应用程序 OnCreate class (android).
我还将 HttpClient 实现更改为 AndroidClientHandler,同时将 SSL/TLS 实现更改为 1.1。在模拟器和设备上尝试这个。
有人有什么建议吗?
试试这个: 打开 Android 项目 属性 -> Android 选项 -> 高级
将 HttpClient 实现 设置从 默认 切换到 Android
HTTPS 的默认 Mono 客户端处理程序似乎已损坏。