【问题标题】:Xamarin App crashes calling HTTPSXamarin 应用程序崩溃调用 HTTPS
【发布时间】:2017-07-05 09:23:49
【问题描述】:

我创建了一个简单的 Xamarin.Forms 应用来使用 https://jsonplaceholder.typicode.com/ 检查 HTTPS。

但应用程序使用 HTTPS 崩溃。

 public RestService() {
      client = new HttpClient();
      client.MaxResponseContentBufferSize = 256000;
    }

public async Task<List<Item>> RefreshDataAsync() {
  string url = "http://jsonplaceholder.typicode.com/posts";
  // Crash using this URL
  //string url = "https://jsonplaceholder.typicode.com/posts";
  var uri = new Uri(string.Format(url, string.Empty));
  List<Item> Items = new List<Item>();

  var content = await client.GetStringAsync(uri);
    Items = JsonConvert.DeserializeObject<List<Item>>(content);

  return Items;
}
}

是网站或 httpClient 有问题还是我做错了什么?

那是例外

{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.
  at Mono.Security.Protocol.Tls.RecordProtocol.EndReceiveRecord (System.IAsyncResult asyncResult) [0x00037] in <d2bf9ddce2b945f79db1e7c4354bafea>:0 
  at Mono.Security.Protocol.Tls.SslClientStream.SafeEndReceiveRecord (System.IAsyncResult ar, System.Boolean ignoreEmpty) [0x00000] in <d2bf9ddce2b945f79db1e7c4354bafea>:0 
  at Mono.Security.Protocol.Tls.SslClientStream.NegotiateAsyncWorker (System.IAsyncResult result) [0x00071] in <d2bf9ddce2b945f79db1e7c4354bafea>:0 
   --- End of inner exception stack trace ---
  at Mono.Security.Protocol.Tls.SslClientStream.EndNegotiateHandshake (System.IAsyncResult result) [0x00032] in <d2bf9ddce2b945f79db1e7c4354bafea>:0 
  at Mono.Security.Protocol.Tls.SslStreamBase.AsyncHandshakeCallback (System.IAsyncResult asyncResult) [0x0000c] in <d2bf9ddce2b945f79db1e7c4354bafea>:0 
   --- End of inner exception stack trace ---
  at Mono.Security.Protocol.Tls.SslStreamBase.EndRead (System.IAsyncResult asyncResult) [0x0004b] in <d2bf9ddce2b945f79db1e7c4354bafea>:0 
  at Mono.Net.Security.Private.LegacySslStream.EndAuthenticateAsClient (System.IAsyncResult asyncResult) [0x0000e] in <a547bd0d78184f26ab08d022f013c1e1>:0 
  at Mono.Net.Security.Private.LegacySslStream.AuthenticateAsClient (System.String targetHost, System.Security.Cryptography.X509Certificates.X509CertificateCollection clientCertificates, System.Security.Authentication.SslProtocols enabledSslProtocols, System.Boolean checkCertificateRevocation) [0x0000e] in <a547bd0d78184f26ab08d022f013c1e1>:0 
  at Mono.Net.Security.MonoTlsStream.CreateStream (System.Byte[] buffer) [0x0007b] in <a547bd0d78184f26ab08d022f013c1e1>:0 
  at System.Net.WebConnection.CreateStream (System.Net.HttpWebRequest request) [0x00073] in <a547bd0d78184f26ab08d022f013c1e1>:0 
   --- End of inner exception stack trace ---
  at System.Net.HttpWebRequest.EndGetResponse (System.IAsyncResult asyncResult) [0x00058] in <a547bd0d78184f26ab08d022f013c1e1>:0 
  at System.Threading.Tasks.TaskFactory`1[TResult].FromAsyncCoreLogic (System.IAsyncResult iar, System.Func`2[T,TResult] endFunction, System.Action`1[T] endAction, System.Threading.Tasks.Task`1[TResult] promise, System.Boolean requiresSynchronization) [0x0000f] in <3fd174ff54b146228c505f23cf75ce71>:0 
--- End of stack trace from previous location where exception was thrown ---
  at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in <3fd174ff54b146228c505f23cf75ce71>:0 
  at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) [0x0003e] in <3fd174ff54b146228c505f23cf75ce71>:0 
  at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) [0x00028] in <3fd174ff54b146228c505f23cf75ce71>:0 
  at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) [0x00008] in <3fd174ff54b146228c505f23cf75ce71>:0 
  at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1+ConfiguredTaskAwaiter[TResult].GetResult () [0x00000] in <3fd174ff54b146228c505f23cf75ce71>:0 
  at System.Net.Http.HttpClientHandler+<SendAsync>d__63.MoveNext () [0x003e6] in <b696532a7c264e5e866cb15a1b40a4a4>:0 
   --- End of inner exception stack trace ---
  at System.Net.Http.HttpClientHandler+<SendAsync>d__63.MoveNext () [0x00449] in <b696532a7c264e5e866cb15a1b40a4a4>:0 
--- End of stack trace from previous location where exception was thrown ---
  at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in <3fd174ff54b146228c505f23cf75ce71>:0 
  at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) [0x0003e] in <3fd174ff54b146228c505f23cf75ce71>:0 
  at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) [0x00028] in <3fd174ff54b146228c505f23cf75ce71>:0 
  at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) [0x00008] in <3fd174ff54b146228c505f23cf75ce71>:0 
  at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1+ConfiguredTaskAwaiter[TResult].GetResult () [0x00000] in <3fd174ff54b146228c505f23cf75ce71>:0 
  at System.Net.Http.HttpClient+<SendAsyncWorker>d__49.MoveNext () [0x000ca] in <b696532a7c264e5e866cb15a1b40a4a4>:0 
--- End of stack trace from previous location where exception was thrown ---
  at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in <3fd174ff54b146228c505f23cf75ce71>:0 
  at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) [0x0003e] in <3fd174ff54b146228c505f23cf75ce71>:0 
  at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) [0x00028] in <3fd174ff54b146228c505f23cf75ce71>:0 
  at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) [0x00008] in <3fd174ff54b146228c505f23cf75ce71>:0 
  at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1+ConfiguredTaskAwaiter[TResult].GetResult () [0x00000] in <3fd174ff54b146228c505f23cf75ce71>:0 
  at System.Net.Http.HttpClient+<GetStringAsync>d__55.MoveNext () [0x0007d] in <b696532a7c264e5e866cb15a1b40a4a4>:0 
--- End of stack trace from previous location where exception was thrown ---
  at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in <3fd174ff54b146228c505f23cf75ce71>:0 
  at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) [0x0003e] in <3fd174ff54b146228c505f23cf75ce71>:0 
  at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) [0x00028] in <3fd174ff54b146228c505f23cf75ce71>:0 
  at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) [0x00008] in <3fd174ff54b146228c505f23cf75ce71>:0 
  at System.Runtime.CompilerServices.TaskAwaiter`1[TResult].GetResult () [0x00000] in <3fd174ff54b146228c505f23cf75ce71>:0 
  at getWeather.RestService+<RefreshDataAsync>d__2.MoveNext () [0x0006e] in C:\Users\afelde\documents\visual studio 2017\Projects\getWeather\getWeather\getWeather\RestService.cs:30 }

【问题讨论】:

  • 它给出了什么错误?堆栈跟踪?
  • 使用调试器。
  • 我记得读过 TLS 1.2 和单声道存在问题。如果您认为这是您的问题的原因,请试一试:ModernHttpClient。这只是包装了 Android 或 IOS 特定的 http 客户端。如果您使用的是 Android,您还可以检查项目中的设置,在 Android 选项、高级、HttpClient 实现中...
  • 在这种情况下,此应用程序仅由 Anroid 部署和测试,HttpClient 实现也设置为默认值和 SSL/TLS。应该设置什么?

标签: c# .net xamarin httpclient


【解决方案1】:

Xamarin.Forum 上的 Post 也可以在不安装任何 Nuget 包的情况下解决问题:

正如@SvetlomirKalitsov 所描述的,下面的两个设置负责 问题:

HttpClient 实现 -> AndroidClientHandler SSL/TLS implementation -> Native TLS 1这是我犯错误的地方:那里 是两个非常相似的选项: Native TLS 1.2+ 这是正确的 一,默认(本机 TLS 1.2+)。这第二个(默认)将 失败。确保选择非默认的;换句话说,使 确保强制使用该选项,而不是依赖默认值。

一点背景信息:XA_HTTP_CLIENT_HANDLER_TYPE 环境 Xamarin.Android 上的变量

感谢@SvetlomirKalitsov

【讨论】:

  • 我没看到那个,抱歉!
  • 不过是你的帖子给了我一个提示 ;-)
【解决方案2】:

我发现了一篇关于您的问题的帖子 (https://forums.xamarin.com/discussion/10405/the-authentication-or-decryption-has-failed-in-the-web-request)

在 cmets 中,您会发现有人建议使用该软件包 modernhttpclient

使用这个包的方法: HttpClient 客户端 = new HttpClient(new NativeMessageHandler());

NativeMessagehandler 将解决证书问题(因为你使用 https 它给出了身份验证错误,这将解决它)

对于其余部分,您可以像以前一样使用客户端。

祝你好运

【讨论】:

  • 我不敢相信它有效!但是有没有其他使用HTTPS的方法
  • @feldeOne 通常这使用 HTTPS?我可能错了,但这个插件将帮助您获得使用 HTTPS 所需的认证
猜你喜欢
  • 2015-12-05
  • 2019-10-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-15
  • 2020-01-04
相关资源
最近更新 更多