【问题标题】:WebException in Xamarin, using HttpClientXamarin 中的 WebException,使用 HttpClient
【发布时间】: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


【解决方案1】:

试试这个: 打开 Android 项目属性 -> Android 选项 -> 高级

HttpClient implementation 设置从 Default 切换到 Android

似乎默认的 Mono 客户端处理程序无法用于 HTTPS。

【讨论】:

  • 会提到将 HttpClient 实现保留为默认值,并将 TLS 更新为 1.2+ 对我有用
  • 最佳解决方案。我不知道为什么需要做这些事情。这个标准是一个还是一个黑客?
  • 为我工作。我将 HttpClient implementation 更改为 Android。 SSL/TLS 实现到默认。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多