2017-05-10 64 views
2

我試圖訪問返回json的特定URL(https://api.cartolafc.globo.com/mercado/destaques)。Xamarin HTTPClient:訪問特定Web服務時出錯

在我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):接收失敗---> System.Exception:在System.Net上的System.Net.WebConnection.HandleError(System.Net.WebExceptionStatus st,System.Exception e,System.String其中)[0x0003a] < 8dac28ffb2cb41cba0b572038da86c99>:0 。 WebConnection.ReadDone(System.IAsyncResult結果)[ 0x0006e]在< 8dac28ffb2cb41cba0b572038da86c99>:0 在System.Runtime.Remoting.Messaging.AsyncResult.Invoke(System.Runtime.Remoting.Messaging.AsyncResult)[0x00000]在< 8c304e4006094a46a7950338a3b3cb5d>:0 在System.Runtime.Remoting.Messaging .AsyncResult.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()[0x00000]在< 8c304e4006094a46a7950338a3b3cb5d>:0 在System.Threading.ThreadPoolWorkQueue.Dispatch()[0x00096]在< 8c304e4006094a46a7950338a3b3cb5d>:0 在System.Threading._ThreadPoolWaitCallback.PerformWaitCallback ()[0x00000] in < 8c304e4006094a46a7950338a3b3cb5d>:0 at System.Net.WebConnection.HandleError(System.Net.WebExceptionStatus st,System.Exception e,System.String where)[0x0003a] in/Us ers/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 at System.Threading .TasksFactory 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)在/ Users/builder/data/lanes/4468/f913a78a/source/mono/mcs中的[0x00014] /class/referencesource/mscorlib/system/threading/Tasks/FutureFactory.cs:550 ---從以前的位置拋出異常的堆棧跟蹤結束--- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()[ 0x0000c] in/Use rs/builder/data/lanes/4468/f913a78a/source/mono/mcs/class/referencesource/mscorlib/system/runtime/exceptionservices/exceptionservicescommon.cs:143 at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(System.Threading .Tasks.Task任務)在System的/Users/builder/data/lanes/4468/f913a78a/source/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:187, 處的[0x00047]。 Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(System.Threading.Tasks.Task任務)/ Users/builder/data/lanes/4468/f913a78a/source/mono/mcs/class/referencesource/mscorlib/system/runtime中的[0x0002e] compilerservices/TaskAwaiter.cs:156 at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(System.Threading.Tasks.Task task)[0x0000b] in/Users/builder/data/lanes/4468/f913a78a/source/mono/mcs /類/ referencesource/mscorlib程序/系統/運行/ compilerservices/TAS kAwaiter.cs:128 at/Users/builder/data/lanes/4468/f913a78a/source/mono/mcs/class /目錄下System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1 + ConfiguredTaskAwaiter [TResult] .GetResult()[0x00000]在/ Users/builder/data/lanes/4468/f913a78a/source/mono中的System.Net.Http.HttpClientHandler + c__async0.MoveNext()[0x003ce]處的referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:535 /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 --- End來自以前位置的堆棧跟蹤,拋出異常---在/ Users/builder/data/lanes/4468/f913a78a/source/mono/mcs/class中System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()[0x0000c]處的 /referencesource/mscorlib/system/runtime/exceptionservices/exceptionservicescommon.cs:143 ...

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

什麼可能是這個特定的網址不能正常工作的原因?

+0

在郵遞員打開它,運行良好。 創建WPF項目只是爲了看看,得到了這個nastiness。網址有些奇怪,無論是SSL級別還是無效的證書。現在沒時間了,會多挖掘一下,但認爲你會對「提示」感興趣。 拋出的異常:mscorlib.dll中的'System.Net.Http.HttpRequestException' System.Net.Http.HttpRequestException:發送請求時發生錯誤。 ---> System.Net.WebException:請求被中止:無法創建SSL/TLS安全通道。 –

回答

0

您的堆棧跟蹤非常模糊,或許this blog here 與您的問題類似。只需確保在調試模式下,您的網絡服務網址在GetAsync()呼叫中正確。 你也可能想嘗試ModernHttpClientRefit而不是使用普通的HttpClient。

0

我在發佈版本上遇到同樣的問題(調試工作正常)。檢查我的機器人項目的「互聯網」權限做了訣竅。

how to fix you error