【问题标题】:How can I debug this webclient exception?如何调试此 Web 客户端异常?
【发布时间】:2013-10-01 10:13:27
【问题描述】:

我正在尝试使用以下代码为 Android 应用提取一个简单的 RSS 提要:

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] in :0 --- 堆栈跟踪结束 以前抛出异常的位置 --- at
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
[0x00000] 在 :0 在
System.Runtime.CompilerServices.TaskAwaiter`1[System.String].GetResult () [0x00000] in :0 at
Spotlight.EpisodePoller+c
_async1.MoveNext ()
[0x000e2] 在
/Users/Jannie/Workshop/SpotlightEnglish/Elements/EpisodePoller.cs:52
[聚焦] Web 异常:执行 WebClient 时发生错误
请求。

我不在代理或防火墙后面,可以通过测试设备浏览提要 url,并在我的应用清单中指定了 Internet 访问权限。

任何关于如何开始调试的指针将不胜感激。

【问题讨论】:

  • 什么是ex.Message?你试过WebClient.DownloadStringTaskAsync()吗?
  • ex.Message: An error occurred performing a WebClient request.
  • 耶!! DownloadStringTaskAsync() 完美运行。

标签: c# debugging xamarin.android webclient


【解决方案1】:

以下带有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
    }
}

另外,您的代码似乎有点错误。为什么要上传东西到服务器而不是下载

【讨论】:

  • 好方法,谢谢!我正在尝试不同的 webclient 方法,这些方法可以让我轻松管理参数,但我只会将它们附加到 uri 字符串。
猜你喜欢
  • 2013-09-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多