Xamarin App崩溃调用HTTPS

时间:2017-07-05 09:23:50

标签: c# .net xamarin httpclient

我创建了一个简单的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 }

2 个答案:

答案 0 :(得分:4)

Xamarin.Forum上的Post也解决了问题而没有安装任何Nuget包:

  

正如@SvetlomirKalitsov所描述的,下面的两个设置可以解决   问题:

     

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

     

一些背景信息:XA_HTTP_CLIENT_HANDLER_TYPE环境   关于Xamarin.Android的变量

     

谢谢@SvetlomirKalitsov

答案 1 :(得分:1)

我发现了一篇关于您的问题的帖子(here

在评论中,您会发现有人建议使用套餐https://forums.xamarin.com/discussion/10405/the-authentication-or-decryption-has-failed-in-the-web-request

使用此程序包的方法: HttpClient client = new HttpClient(new NativeMessageHandler());

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

对于其他人,您可以使用客户端,因为您已经在使用它。

祝你好运