【问题标题】:Xamarin async Http ClientXamarin 异步 Http 客户端
【发布时间】:2017-12-15 17:22:30
【问题描述】:

我在我的 Xamarin 项目中遇到了一个问题:当我尝试通过 HttpClient.GetStringAsncy 从网页获取 JsonString 时,我的应用程序崩溃了。这是错误的 StackTrace:

在 System.Net.Http.HttpClientHandler+d__63.MoveNext () [0x00479] 在:0 --- 从先前抛出异常的位置结束堆栈跟踪 --- 在 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] 在:0 在 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task 任务) [0x0003e] in :0 在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task 任务) [0x00028] in :0 在 System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task 任务) [0x00008] in :0 在 System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1+ConfiguredTaskAwaiter[TResult].GetResult () [0x00000] in :0 在 System.Net.Http.HttpClient+d__49.MoveNext () [0x000ca] in :0 --- 从先前抛出异常的位置结束堆栈跟踪 --- 在 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] 在:0 在 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task 任务) [0x0003e] in :0 在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task 任务) [0x00028] in :0 在 System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task 任务) [0x00008] in :0 在 System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1+ConfiguredTaskAwaiter[TResult].GetResult () [0x00000] in :0 在 System.Net.Http.HttpClient+d__54.MoveNext () [0x0007d] in :0 --- 从先前抛出异常的位置结束堆栈跟踪 --- 在 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] 在:0 在 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task 任务) [0x0003e] in :0 在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task 任务) [0x00028] in :0 在 System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task 任务) [0x00008] in :0 在 System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1+ConfiguredTaskAwaiter[TResult].GetResult () [0x00000] in :0 在 E:\Visual Studio Projects\MRKrauchthal\MRKrauchthal\MRKrauchthal\Pages\GameDetailPage.xaml.cs:99 中的 MRKrauchthal.Pages.GameDetailPage+d__13.MoveNext () [0x00057]

e.InnerException

{System.Net.WebException:错误:SecureChannelFailure(身份验证或解密失败。)---> System.IO.IOException:身份验证或解密失败。 ---> System.IO.IOException:认证或解密失败。 ---> Mono.Security.Protocol.Tls.TlsException:认证或解密失败。 在 Mono.Security.Protocol.Tls.RecordProtocol.EndReceiveRecord (System.IAsyncResult asyncResult) [0x00037] in :0 在 Mono.Security.Protocol.Tls.SslClientStream.SafeEndReceiveRecord (System.IAsyncResult ar, System.Boolean ignoreEmpty) [0x00000] in :0 在 Mono.Security.Protocol.Tls.SslClientStream.NegotiateAsyncWorker (System.IAsyncResult 结果) [0x00071] in :0 --- 内部异常堆栈跟踪结束 --- 在 Mono.Security.Protocol.Tls.SslClientStream.EndNegotiateHandshake (System.IAsyncResult 结果) [0x00032] in :0 在 Mono.Security.Protocol.Tls.SslStreamBase.AsyncHandshakeCallback (System.IAsyncResult asyncResult) [0x0000c] in :0 --- 内部异常堆栈跟踪结束 --- 在 Mono.Security.Protocol.Tls.SslStreamBase.EndRead (System.IAsyncResult asyncResult) [0x0004b] in :0 在 Mono.Net.Security.Private.LegacySslStream.EndAuthenticateAsClient (System.IAsyncResult asyncResult) [0x0000e] in :0 在 Mono.Net.Security.Private.LegacySslStream.AuthenticateAsClient (System.String targetHost, System.Security.Cryptography.X509Certificates.X509CertificateCollection clientCertificates, System.Security.Authentication.SslProtocols enabledSslProtocols, System.Boolean checkCertificateRevocation) [0x0000e] 在:0 在 Mono.Net.Security.MonoTlsStream.CreateStream (System.Byte[] 缓冲区) [0x0007b] in :0 在 System.Net.WebConnection.CreateStream (System.Net.HttpWebRequest 请求)[0x00073] 在:0 --- 内部异常堆栈跟踪结束 --- 在 System.Net.HttpWebRequest.EndGetResponse (System.IAsyncResult asyncResult) [0x00058] 在:0 在 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] 承诺,System.Boolean requiresSynchronization) [0x0000f] in :0 --- 从先前抛出异常的位置结束堆栈跟踪 --- 在 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] 在:0 在 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task 任务) [0x0003e] in :0 在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task 任务) [0x00028] in :0 在 System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task 任务) [0x00008] in :0 在 System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1+ConfiguredTaskAwaiter[TResult].GetResult () [0x00000] in :0 在 System.Net.Http.HttpClientHandler+d__63.MoveNext () [0x0041e] in :0 }

代码如下所示:

private async Task SetImage(int id, string teamName, Xamarin.Forms.Image image)
    {
        try
        {
            HttpClient httpClient = new HttpClient();
            obj = JsonConvert.DeserializeObject<JsonImage>(await httpClient.GetStringAsync("www.linkToImage.com").ConfigureAwait(false));
        }
        catch (Exception e) {
            test.Text = e.StackTrace;
        }

    }

我希望有人可以帮助我:D

最好的问候

【问题讨论】:

  • 您正在打印堆栈跟踪但不是错误消息。请添加该信息,因为它可能是解决问题的关键
  • 如果您的GET 返回错误会怎样?在尝试反序列化之前,您不会检查结果是否成功。仅仅因为你可以写在一行上,并不意味着你应该
  • 我用 e.message 得到的错误信息是:发送请求时发生错误
  • 当我自己尝试链接时,通过将链接复制粘贴到浏览器中,我会得到完整的 json 响应
  • 抛出的异常很可能是AggregateException。它的属性InnerExceptions 包含抛出的真正异常。可以分享一下吗?

标签: c# android asynchronous xamarin httpclient


【解决方案1】:

Xamarin.Android 中的默认 SSL/TLS 实现存在一些问题。你可以去 Android Project settings->Android Options->Advanced->SSL/TLS implementation 并更改为 Native TLS 1.2+

【讨论】:

    猜你喜欢
    • 2017-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-31
    • 2019-01-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多