RSS Feed reading using ASP.NET with C#.Net|RSS Feed reading with proxy set up

We can read RSS feeds using ASP.NET with C# code. In the below example, if you click on button all rss feed data will read and display on the webpage. In the corporate network we are unable to read RSS feeds with out using proxy. Please give your proxy name in the below line. Basically, this proxy programming required in the corporate networks.  That's it entire code will work.


WebProxy proxyObject = new WebProxy("http://proxyName:8080/", true);






Output page:




.aspx Code
<html>
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <table cellpadding="0" cellspacing="0">
            <tr>
                <td>
                    <asp:Table ID="table1" runat="server">
                    </asp:Table>
                </td>
            </tr>
        </table>
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />

    </div>
    </form>
</body>
</html>


C# Code behind. 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.Web.UI.HtmlControls;
using System.Xml;
using System.IO;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        //int proxyValue = 8080;

        WebProxy proxyObject = new WebProxy("http://proxyName:8080/", true);
        proxyObject.Credentials = CredentialCache.DefaultCredentials;



        WebRequest MyRssRequest = WebRequest.Create("http://www.csharp-dotnet-interview-questions.blogspot.com/feeds/posts/default?alt=rss");
        MyRssRequest.Proxy = proxyObject;
        WebResponse MyRssResponse = MyRssRequest.GetResponse();
        Stream MyRssStream = MyRssResponse.GetResponseStream();
        //Here we are loading previously created xml document. 
        XmlDocument MyRssDocument = new XmlDocument();
        MyRssDocument.Load(MyRssStream);
        XmlNodeList MyRssList = MyRssDocument.SelectNodes("rss/channel/item");

        string sTitle = "";
        string sLink = "";
        string sDescription = "";
        // Iterate/Loop through RSS Feed items
        for (int i = 0; i < MyRssList.Count; i++)
        {
            XmlNode MyRssDetail;
            MyRssDetail = MyRssList.Item(i).SelectSingleNode("title");
            if (MyRssDetail != null)
                sTitle = MyRssDetail.InnerText;
            else
                sTitle = "";
            MyRssDetail = MyRssList.Item(i).SelectSingleNode("link");
            if (MyRssDetail != null)
                sLink = MyRssDetail.InnerText;
            else
                sLink = "";
            MyRssDetail = MyRssList.Item(i).SelectSingleNode("description");
            if (MyRssDetail != null)
                sDescription = MyRssDetail.InnerText;
            else
            {
                sDescription = "";
            }

            TableCell block = new TableCell();
            block.Text = "<span style='font-weight:bold'><a href='" + sLink + "' target='new'>" + sTitle + "</a></span>";
            TableRow row = new TableRow();
            row.Cells.Add(block);
            //table1.Rows.Add(row);

            TableCell block_description = new TableCell();
            block_description.Text = "<p align='justify'>" + sDescription + "</p>";
            //TableRow row2 = new TableRow();
            //row2.Cells.Add(block_description);
            row.Cells.Add(block_description);

            table1.Rows.Add(row);
        }
    }
}


Share this post :

Post a Comment

Please give your valuable feedback on this post. You can submit any ASP.NET article here. We will post that article in this website by your name.

 
Support : Ranga Rajesh Kumar
Copyright © 2012. ASP.NET Examples - All Rights Reserved
Site Designed by Ranga Rajesh Kumar