C# WebRequest/Response - Error 500 - Catch Error

751 Views Asked by At

I have following problem: My tool checks every 6min a RSS-Feed. This works perfectly but the server on which the RSS-Feed is hosted, is sometimes not available for some seconds. The "check"-part runs 24/7. So sometimes it throws an error: 500 - internal server error and therefore my tool sometimes stops working.

How can I catch this? (I don't need to recheck if it failed. Just need to catch that error so that the tool continues running)

This is the code, I use:

    // RSS WORKARONUD
    String[,] rssData = null;

    private String[,] getRssData(String channel)
    {
        System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls12;
        System.Net.WebRequest myRequest = System.Net.WebRequest.Create(channel);
        System.Net.WebResponse myResponse = myRequest.GetResponse();


        System.IO.Stream rssStream = myResponse.GetResponseStream();
        System.Xml.XmlDocument rssDoc = new System.Xml.XmlDocument();

        rssDoc.Load(rssStream);

        System.Xml.XmlNodeList rssItems = rssDoc.SelectNodes("rss /channel/item");

        //Matrix, 100 rows , 3 colums
        String[,] tempRssData = new String[100, 3];
1

There are 1 best solutions below

2
Prateek Shrivastava On BEST ANSWER
System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls12;
        System.Net.WebRequest myRequest = System.Net.WebRequest.Create(channel);
        System.Net.WebResponse myResponse = myRequest.GetResponse();

try
{
        System.IO.Stream rssStream = myResponse.GetResponseStream();
        System.Xml.XmlDocument rssDoc = new System.Xml.XmlDocument();

        rssDoc.Load(rssStream);

        System.Xml.XmlNodeList rssItems = rssDoc.SelectNodes("rss /channel/item");
  //Matrix, 100 rows , 3 colums
        String[,] tempRssData = new String[100, 3];
}
Catch(Exception ex)
{
}