Wednesday, 30 June 2010

ASP.NET RSS Toolkit With Proxy Server

Lots of ASP.NET developers took advantage of the free ASP.NET RSS toolkit to consume and provide RSS feeds. Thanks to dmitry in a matter of minutes, we will have a nice and robust toolkit which can be intergrated easily in our web application. However, After i finished my application and deploy it on one of our web servers
, Consuming RSS stopped working. The error message was "unable to connect to remote server". After investigating and contacting dmitry, i realized our proxy server was rejecting the request. I looked into the open source code provided and knew that the RSS was being loaded directly into an XmlDocument. All we needed is to send an HttpWebRequest, set the proxy configuration, read the response, and finally load the XmlDocument.

In DownloadManager Class, replace DownloadFeedDom by the below

//// look for disk cache first
CacheInfo dom = TryLoadFromDisk(url);

if (CacheReadable(dom))
{
return dom;
}

string ttlString = null;
HttpWebRequest webRequest = null;
string strResult = null;
try
{
// Create a new web request
webRequest = WebRequest.Create(url) as HttpWebRequest;
// Check if ProxyServerUrl in the appSettings is not empty
if (!string.IsNullOrEmpty(WebConfigurationManager.AppSettings["ProxyServerUrl"].ToString()))
{
// instantiate a new WebProxy class and send the ProxyServerUrl to the contructor parameter
System.Net.WebProxy proxy = new System.Net.WebProxy(WebConfigurationManager.AppSettings["ProxyServerUrl"].ToString(), true);
// Set the proxy credentials
proxy.Credentials = new NetworkCredential(WebConfigurationManager.AppSettings["PassThroughUser"].ToString(), WebConfigurationManager.AppSettings["PassThroughPwd"].ToString());
// Set the WebRequest proxy property by the WebProxy class already configured
webRequest.Proxy = proxy;
}
// Read the response
using (StreamReader sr = new StreamReader(webRequest.GetResponse().GetResponseStream()))
{
// Set strResult to the response
strResult = sr.ReadToEnd();
}
}
catch
{
}
// Load xml From the string
XmlDocument doc = new XmlDocument();
doc.LoadXml(strResult);

if (doc.SelectSingleNode("/rss/channel/ttl") != null)
{
ttlString = doc.SelectSingleNode("/rss/channel/ttl").Value;
}

//// compute expiry
int ttlMinutes = GetTtlFromString(ttlString, _defaultTtlMinutes);
DateTime utcExpiry = DateTime.UtcNow.AddMinutes(ttlMinutes);

//// save to disk
return TrySaveToDisk(doc, url, utcExpiry);

P.S: I would like to thank dmitry for his quick response eventhough i know that he would be busy.

Hope this helps,

No comments:

Post a Comment