Download RSS feed in C#

Download an RSS feed using WebClient in C#...
static void Method1()
{
 WebClient wc = new WebClient();
 wc.Proxy = GetWebProxy();

 string feed = wc.DownloadString("rss url");

 XmlDocument doc = new XmlDocument();

 doc.LoadXml(feed);
}
Download an RSS feed using WebRequest in C#...
static void Method2()
{
 HttpWebRequest request = WebRequest.Create("rss url") as HttpWebRequest;
 //set the user agent so it looks like IE to not raise suspicion
 request.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)";
 request.Method = "GET";

 request.Proxy = GetWebProxy();

 string pagedata;
 //get the response from the server
 HttpWebResponse response = (HttpWebResponse)request.GetResponse();
 using (Stream stream = response.GetResponseStream())
 {
     using (StreamReader reader = new StreamReader(stream))
     {
         pagedata = reader.ReadToEnd();
     }
 }
 response.Close();

 XmlDocument doc = new XmlDocument();

 doc.LoadXml(pagedata);
}
Shared code to create a proxy if you're behind a firewall...
static WebProxy GetWebProxy()
{
 return new WebProxy("proxy server:proxy port", true, new string[0], CredentialCache.DefaultNetworkCredentials);
}

0 comments: