I am developing an RSS reader, when i sent the request to the sever and get the response, I then load the response in xml document. When the document is loading it throws exception, that is stopping my app from being compiling and running.

the text of the exception thrown: The remote server returned an error: (500) Internal Server Error. it is kicking my head every with this exception. Any can explain why this is happening and how to handle it.

from where I call the Rss reader mehtod:

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

the cod of Rss reader method is:

      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;
有帮助吗?

解决方案

Try WebClient instead, much easier to use:

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;
}

As to WHY you're getting a 500 error, my guess is that the XmlDocument.Load() method you are using does not have the full capabilities of a web client, so it cannot handle cookies and 301/302 redirections from the target URL very well. See below:

enter image description here

其他提示

Solution 1: Your requested url http://bbc.com/news is not returning XML. So reading it through XmlDocument.Load is causing issue. please use StreamReader to handle your case. Something like

    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
        }
    }

Solution 2 I noticed the Rss Feed for http://bbc.com/news is in this URL http://feeds.bbci.co.uk/news/rss.xml

Update this URL as parameter to Rss_read method, and that will work. Like this

rssData = Rss_read("http://feeds.bbci.co.uk/news/rss.xml");
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top