C#Rss app中的内部服务器错误500

时间:2014-04-03 04:57:58

标签: c# rss rss-reader

我正在开发一个RSS阅读器,当我将请求发送到服务器并获得响应时,我然后在xml文档中加载响应。当文档加载时,它会抛出异常,即阻止我的应用程序编译和运行。

抛出异常的文本:远程服务器返回错误:(500)内部服务器错误。这个例外是每个人都在踢我的头。任何可以解释为什么会发生这种情况以及如何处理它。

我称之为Rss阅读器mehtod:

private static string[,] rssData;
rssData = Rss_read("http://bbc.com/news");

Rss阅读器方法的鳕鱼是:

      private static String[,] Rss_read(string connection)
  {
     WebRequest feedRqst = WebRequest.Create(connection); 
     WebResponse feedRspns = feedRqst.GetResponse(); 

     Stream rssStream = feedRspns.GetResponseStream(); // Returning the feed stream;
     XmlDocument rssxmlDoc = new XmlDocument();




     rssxmlDoc.Load(rssStream);   ///statement which return exception;                     
     XmlNodeList rssItme = rssxmlDoc.SelectNodes("rss/ chanel/item");

     string[,] feedData  = new string[40, 3]; 

     for (int i = 0; i < rssItme.Count; i++)
     {
        XmlNode rssNod;
        rssNod = rssItme.Item(i).SelectSingleNode("title"); // title of feed
        if (rssNod != null)
        {
           feedData[i, 0] = rssNod.InnerText;
        }
        else
        {
           feedData[i, 0] = "";
        }
        rssNod = rssItme.Item(i).SelectSingleNode("descryption"); // decryption of feed;
        if (rssNod != null)
        {
           feedData[i, 1] = rssNod.InnerText; 

        }
        else
        {
           feedData[i, 1] = "";

        }
        rssNod = rssItme.Item(i).SelectSingleNode("link"); // link to the specific title in the feed;
        if (rssNod != null)
        {
           feedData[i, 2] = rssNod.InnerText;


        }
        else
        {
           feedData[i, 2] = "";

        }
     } // End of for loop;

     return feedData;

  } // End of rss_feed method;

2 个答案:

答案 0 :(得分:1)

尝试使用WebClient,更容易使用:

private static String[,] Rss_read(string connection)
{
    string[,] feedData = new string[40, 3];
    WebClient client = new WebClient();
    XmlDocument rssxmlDoc = new XmlDocument();
    string downloadString = client.DownloadString(connection);
    rssxmlDoc.LoadXml(downloadString);   ///statement which return exception;    
    XmlNodeList rssItme = rssxmlDoc.SelectNodes("rss/ chanel/item");

    for (int i = 0; i < rssItme.Count; i++)
    {
        // Your logic here

    }
    return feedData;
}

至于为什么你得到500错误,我的猜测是你使用的XmlDocument.Load()方法没有Web客户端的全部功能,所以它无法处理cookie和301/302重定向目标网址非常好。见下文:

enter image description here

答案 1 :(得分:0)

解决方案1: 您请求的网址http://bbc.com/news未返回XML。因此,通过XmlDocument.Load读取它会导致问题。请使用StreamReader来处理您的案件。像

这样的东西
    private static string[,] Rss_read(string connection)
    {
        WebRequest feedRqst = WebRequest.Create(connection);
        WebResponse feedRspns = feedRqst.GetResponse();

        Stream rssStream = feedRspns.GetResponseStream(); // Returning the feed stream;

        StreamReader sr = new StreamReader(rssStream);

        while (!sr.EndOfStream)
        {
            //Do some logic
        }
    }

解决方案2 我注意到http://bbc.com/news的Rss Feed位于此网址http://feeds.bbci.co.uk/news/rss.xml

将此URL更新为Rss_read方法的参数,这将起作用。喜欢这个

rssData = Rss_read("http://feeds.bbci.co.uk/news/rss.xml");