1

I am trying to read an RSS feed from http://backend.deviantart.com/rss.xml?q=gallery:duster132/23316533&type=deviation with the following code:

        //Different RSS Links
        string deviant_rsslink = @"http://backend.deviantart.com/rss.xml?q=gallery:duster132/23316533&type=deviation";
        string blogspot_rsslink = @"http://fightpunch.blogspot.com/feeds/posts/default";

        //Reading the links
        XmlReader reader = XmlReader.Create(deviant_rsslink); //LINE WHERE ERROR OCCURS
        SyndicationFeed feed = SyndicationFeed.Load(reader);
        reader.Close();
        foreach (SyndicationItem item in feed.Items)
        {
            String subject = item.Title.Text;
            Console.WriteLine("Subjext is: " + subject + "\n");
        }

...and I get the error:

"The underlying connection was closed: The connection was closed unexpectedly."

At first I thought deviantart might be blocking my IP so I tried this from different computers with differing IPs however the error persists, so it seems that is not the issue. To make things more difficult to track, the code works with no error at http://fightpunch.blogspot.com/feeds/posts/default.

What should I try to fix this?

2 Answers 2

2

You site requires User-Agent header be set

Below code should work..

string rss = null;
using (var wc = new Webclient())
{
    wc.Headers["User-Agent"] = "SO/1.0";
    rss = wc.DownloadString(deviant_rsslink);
}
XmlReader reader = XmlReader.Create(new StringReader(rss));
Sign up to request clarification or add additional context in comments.

4 Comments

Wow... that works!!! Can you explain why this code works? I looked up user-agent, but not sure I fully understand this. I've been stumped by this for weeks... thank you so so much!!!
@ChrisL I don't know how I can explain it (with my limited english). Just remove the wc.Headers line and try. it will not work... How I found it? maybe experience.
How would this be different, if I tried to load the RSS xml with the following: XDocument rsslink = XDocument.Load(weblink);
@ChrisL Most probably XDocument.Load doesn't post User-Agent header. It is not a must, but some sites may require it. (w3.org/Protocols/HTTP/HTRQ_Headers.html#user-agent)
1

In my case SSL was disabled and TLS was only allowed. I changed to using a HTTPWebRequest instead. Please note that I am using .NET 4.0 and do not have TLS1.2 as option so I hardcoded the value (3072) for that:

Dim doc As New XmlDocument()
Dim req As HttpWebRequest = DirectCast(WebRequest.Create(FeedAddress), HttpWebRequest)
ServicePointManager.SecurityProtocol = DirectCast(3072, SecurityProtocolType)
req.Method = "GET"
Dim myHttpWebResponse As HttpWebResponse = DirectCast(req.GetResponse(), HttpWebResponse)

Dim response As WebResponse = req.GetResponse
Dim streamResponse As Stream = response.GetResponseStream
doc.Load(streamResponse)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.