【问题标题】:Xamarin HTTPClient: Error when accessing specific web serviceXamarin HTTPClient:访问特定 Web 服务时出错
【发布时间】:2017-10-09 04:06:27
【问题描述】:

我正在尝试访问返回 json (https://api.cartolafc.globo.com/mercado/destaques) 的特定 URL。

在我的 Xamarin C# 类中,我这样编码:

public async void DownloadDataAsync()
    {
        string baseUrl = "https://api.cartolafc.globo.com";
        string address = "/mercado/destaques";

        try
        {
            var httpClient = new HttpClient();



            httpClient.BaseAddress = new Uri(baseUrl);

            var resp = await httpClient.GetAsync(address);

            if (resp.IsSuccessStatusCode)
            {
                var respStr = await resp.Content.ReadAsStringAsync();

                var listaAtletas = JsonConvert.DeserializeObject<List<Atleta>>(respStr);

            }
        }
        catch (Exception ex)
        {
            Debug.WriteLine(ex.Message);

        }

    }

我总是收到以下异常:

System.Net.WebException: 获取响应流时出错 (ReadDone2): ReceiveFailure ---> System.Exception: 在 System.Net.WebConnection.HandleError (System.Net.WebExceptionStatus st, System.Exception e, System.String其中)[0x0003a] 在 :0 在 System.Net.WebConnection.ReadDone (System.IAsyncResult 结果) [0x0006e] 在 :0 在 System.Runtime.Remoting.Messaging.AsyncResult.Invoke (System.Runtime.Remoting.Messaging.AsyncResult) [0x00000] 在 :0 在 System.Runtime.Remoting.Messaging.AsyncResult.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem () [0x00000] 在 :0 在 System.Threading.ThreadPoolWorkQueue.Dispatch () [0x00096] 在 :0 在 System.Threading._ThreadPoolWaitCallback.PerformWaitCallback () [0x00000] 在 :0 在 System.Net.WebConnection.HandleError (System.Net.WebExceptionStatus st, System.Exception e, System.String where) [0x0003a] 在 /Users/builder/data/lanes/4468/f913a78a/source/mono/mcs/class /System/System.Net/WebConnection.cs:447 --- 内部异常堆栈跟踪结束 --- 在 System.Net.HttpWebRequest.EndGetResponse (System.IAsyncResult asyncResult) [0x0005e] 在 /Users/builder/data/lanes/4468/f913a78a/source/mono/mcs/class/System/System.Net/HttpWebRequest.cs:1029 在 System.Threading.Tasks.TaskFactory1[TResult].FromAsyncCoreLogic (System.IAsyncResult iar, System.Func2[T,TResult] endFunction, System.Action1[T] endAction, System.Threading.Tasks.Task1[TResult] promise, System.Boolean requiresSynchronization) [0x00014] in /Users/builder/data/lanes/4468 /f913a78a/source/mono/mcs/class/referencesource/mscorlib/system/threading/Tasks/FutureFactory.cs:550 --- 从先前抛出异常的位置结束堆栈跟踪 --- 在 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] 在 /Users/builder/data/lanes/4468/f913a78a/source/mono/mcs/class/referencesource/mscorlib/system/runtime/exceptionservices/exceptionservicescommon.cs :143 在 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task 任务) [0x00047] 在 /Users/builder/data/lanes/4468/f913a78a/source/mono/mcs/class/referencesource/mscorlib/system /runtime/compilerservices/TaskAwaiter.cs:187 在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task 任务) [0x0002e] 在 /Users/builder/data/lanes/4468/f913a78a/source/mono/mcs/class/referencesource/mscorlib/system /runtime/compilerservices/TaskAwaiter.cs:156 在 System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task 任务) [0x0000b] 在 /Users/builder/data/lanes/4468/f913a78a/source/mono/mcs/class/referencesource/mscorlib/system /runtime/compilerservices/TaskAwaiter.cs:128 在 System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1+ConfiguredTaskAwaiter[TResult].GetResult () [0x00000] 在 /Users/builder/data/lanes/4468/f913a78a/source/mono/mcs/class/referencesource/mscorlib/system/运行时/compilerservices/TaskAwaiter.cs:535 在 System.Net.Http.HttpClientHandler+c__async0.MoveNext () [0x003ce] 在 /Users/builder/data/lanes/4468/f913a78a/source/mono/mcs/class/System.Net.Http/System.Net.Http /HttpClientHandler.cs:379 --- 内部异常堆栈跟踪结束 --- 在 System.Net.Http.HttpClientHandler+c__async0.MoveNext () [0x0047a] 在 /Users/builder/data/lanes/4468/f913a78a/source/mono/mcs/class/System.Net.Http/System.Net.Http /HttpClientHandler.cs:383 --- 从先前抛出异常的位置结束堆栈跟踪 --- 在 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] 在 /Users/builder/data/lanes/4468/f913a78a/source/mono/mcs/class/referencesource/mscorlib/system/runtime/exceptionservices/exceptionservicescommon.cs :143 …

当我使用其他 URL 时,例如:https://coffeemaker.herokuapp.com/foursquare.json?q[near]=Lima,%20PE&q[query]=Ceviche,请求成功完成。

此特定 URL 无法正常工作的原因可能是什么?

【问题讨论】:

  • 在邮递员中打开它,运行良好。创建 WPF 项目只是为了看看,得到了这个讨厌。 URL 有点奇怪,要么是 SSL 级别,要么是无效的证书。现在没有时间,将深入挖掘,但认为您会对“提示”感兴趣。抛出异常:mscorlib.dll 中的“System.Net.Http.HttpRequestException” System.Net.Http.HttpRequestException:发送请求时出错。 ---> System.Net.WebException:请求被中止:无法创建 SSL/TLS 安全通道。

标签: c# android json api xamarin


【解决方案1】:

你的堆栈跟踪很模糊,可能是this blog here 与您的问题相似。只需确保在调试模式下您的 Web 服务 url 在GetAsync() 调用时是正确的。 您也可能想尝试ModernHttpClientRefit 而不是使用普通的HttpClient。

【讨论】:

    【解决方案2】:

    我在发布版本上遇到了同样的问题(调试工作正常)。检查我的 droid 项目的“Internet”权限就可以了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-02-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-26
      • 2018-01-24
      • 1970-01-01
      相关资源
      最近更新 更多