2013-10-01 25 views
1

我想拉一個簡單的RSS提要的Android應用程序,此代碼:如何調試此webclient異常?

public async Task<IList<Episode>> PullEpisodesAsync(int page) 
    { 
     string xmlFeed = ""; 

     try 
     { 
      using (WebClient wc = new WebClient()) 
      { 
       var uri = new Uri(Const.FeedEndpoint); // http://spotlightenglish.com/feeds/appfeed/ 
       xmlFeed = await wc.UploadStringTaskAsync(uri, "GET", ""); 
      } 

     } 
     catch (System.Net.WebException wex) 
     { 
      Console.WriteLine("WebExceptionStatus: {0}", wex.StackTrace); 
     } 

     IList<Episode> episodes = ParseFeed(xmlFeed); 

     return episodes; 
    } 

我得到一個運行時異常,不給我很多去:

WebExceptionStatus:在
System.Net.WebClient + C_ async15.MoveNext()
[0x00000]在:0 ---棧跟蹤的結尾從 先前位置,其中引發異常---在
System.Run time.ExceptionServices.ExceptionDispatchInfo.Throw()
[0x00000]中:0在
System.Runtime.CompilerServices.TaskAwaiter`1 [System.String] .GetResult ()[0x00000]中:0在
聚光燈。 EpisodePoller + C
_async1.MoveNext()
[0x000e2]在
/Users/Jannie/Workshop/SpotlightEnglish/Elements/EpisodePoller.cs:52
[聚光燈]的Web例外:出錯執行Web客戶端
請求。

我不是在代理或防火牆後面,可以通過測試設備瀏覽訂閱源網址,並在我的應用清單中指定Internet訪問。

任何關於如何開始調試的指針將不勝感激。

+1

什麼是'ex.Message'?你有沒有試過['WebClient.DownloadStringTaskAsync()'](http://msdn.microsoft.com/en-us/library/hh194294.aspx)? – CodeCaster

+0

ex.Message:'執行WebClient請求時發生錯誤.' –

+1

Yay !! 'DownloadStringTaskAsync()'完美地工作。 –

回答

2

下與HttpClient作品對我來說代碼:

using (var client = new HttpClient()) 
{ 
    var url = "http://spotlightenglish.com/feeds/appfeed/"; 
    client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("text/xml")); 
    var msg = await client.GetAsync(url); 
    if (msg.IsSuccessStatusCode) 
    { 
     var xml = await msg.Content.ReadAsStringAsync(); 
     // do whatever with xml from here 
    } 
} 

而且你的代碼似乎有點不對勁。你爲什麼上傳東西到服務器而不是正在下載

+0

好的方法,謝謝!我正在嘗試不同的webclient方法,使我能夠輕鬆管理參數,但是我只是將它們附加到uri字符串中。 –