PDA

View Full Version : Feed Viewer using asp.net



techno_adi
9 Oct 2006, 2:59 AM
Hi,
I have tried to create a asp.net verison of feed.php and it doesnt seem to work.

My ashx file:




Next line enclosed within <> tags
%@ WebHandler Language="C#" Class="Feed" %
using System;
using System.Web;
using System.Xml;

public class Feed : IHttpHandler {

public void ProcessRequest (HttpContext context) {
try
{
string url = context.Request["feed"];
if (!string.IsNullOrEmpty(url) && url.StartsWith("http"))
{
context.Response.ContentType = "text/xml";
XmlTextReader xtr = new XmlTextReader(url);
context.Response.Write(xtr.ReadInnerXml());
xtr.Close();
}
else
context.Response.Write("Feed Url Error in " + url);
}
catch (Exception ex)
{
context.Response.Write("exp " + ex.Message);
}
}

public bool IsReusable {
get {
return false;
}
}

}


the error on accessing
http://localhost/yui//Feed.ashx?feed=http%3A%2F%2Ffeeds.feedburner.com%2Fajaxian


The XML page cannot be displayed
Cannot view XML input using XSL style sheet. Please correct the error and then click the Refresh button, or try again later.


--------------------------------------------------------------------------------

A name was started with an invalid character. Error processing resource 'http://localhost/yui/Feed.ashx?feed=http%3A%2F%...

<WebHandler>
-^



Following is the error in the first line of the ashx file in VS2005. This line is auto-generated and how does this have error?

ASP.NET runtime error: Catastrophic failure (Exception from HRESULT: 0x8000FFFF (E_UNEXPECTED))

Any suggestions please??
Thanks

rodiniz
9 Oct 2006, 6:38 AM
You cannot use XmlTextReader to read an rss feed.
You need to use a component like
rss.net
http://www.rssdotnet.com/
I can give a sample on using this component , my sample is not generating xml like you want
but I think you will have some starting point.
My e-mail is at my website.

techno_adi
9 Oct 2006, 6:52 AM
Hi,
I don't get one point i.e. why do I need a component to simply read a rss(xml) file and send it as a xml response over XHR.
Please help me on this. I need to complete on this topic ASAP.
Thanks

rodiniz
9 Oct 2006, 7:53 AM
You could use the System.net.Weclient class


WebClient cli = new WebClient();
byte[] data = cli.DownloadData(Url);
string texto = System.Text.Encoding.UTF8.GetString(data);
context.Response.Write(texto);

But will get all the posts from the rss feed... and that can take a lot of time.
But it will work. With the component you can download only the latest 10 feeds (for example)
wich will much faster.

jack.slocum
9 Oct 2006, 8:00 AM
This is completely untested and I just typed it into this window, but it should work.



string url = context.Request["feed"];
if (!string.IsNullOrEmpty(url) && url.StartsWith("http"))
{
context.Response.ContentType = "text/xml";
HttpWebRequest http = WebRequest.create(url);
HttpWebResponse response = http.GetResponse();
StreamReader s = new StreamReader(response.GetResponseStream());
string result = s.ReadToEnd();
context.Response.Write(result);
}
else
context.Response.Write("Feed Url Error in " + url);

techno_adi
9 Oct 2006, 8:55 PM
Hi Jack,
The commented lines were flagging error cannot convert webrequest/response object to httpwebrequest/response( rightly so), so i changed them but still no result..
There still exists some error


WebRequest http = HttpWebRequest.Create(url);
//HttpWebRequest http = WebRequest.Create(url);
//HttpWebResponse response = http.GetResponse();
WebResponse response = http.GetResponse();
StreamReader s = new StreamReader(response.GetResponseStream());
string result = s.ReadToEnd();
context.Response.Write(result);

and this version also


HttpWebRequest http = WebRequest.Create(url) as HttpWebRequest ;
using (HttpWebResponse response = http.GetResponse() as HttpWebResponse)
{
StreamReader s = new StreamReader(response.GetResponseStream());
string result = s.ReadToEnd();
context.Response.Write(result);
}

Response received in both cases:
exceptionUnable to connect to the remote server:::(inner exception) System.Net.Sockets.SocketException: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress) at System.Net.Sockets.Socket.InternalConnect(EndPoint remoteEP) at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Int32 timeout, Exception& exception)

Hi rodinz,
I tried your version too, the response received is same? :cry:

Is this some thing related to Proxy servers?

Angelgab82
26 Apr 2009, 12:45 PM
I had the same problem with requesting it on windows server for some security reasons...

I did a simple solution in FeedGrid.js i requested the page directly.

this.store = new Ext.data.Store({
proxy: new Ext.data.HttpProxy({
url: 'feed-proxy.aspx'
}),

and in the .net i created my RSS with Request.Form("Feed") with the conditions.

you can create whatever you wish in this condition inside your page with if else or case condition.

and in the RSS

feeds.addFeed({
url:'news',
text: 'view all news'
}, true);