XmlReader读取RSS Feed时出错

时间:2014-06-27 20:36:47

标签: c# rss xmlreader rss-reader

我正在尝试使用以下代码从http://backend.deviantart.com/rss.xml?q=gallery:duster132/23316533&type=deviation读取RSS源:

        //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");
        }

......我收到错误:

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

起初我以为deviantart可能会阻止我的IP,所以我尝试使用不同IP的不同计算机,但错误仍然存​​在,所以似乎不是问题。为了使事情更难以跟踪,代码在http://fightpunch.blogspot.com/feeds/posts/default处没有错误。

我应该尝试解决这个问题?

2 个答案:

答案 0 :(得分:1)

您的网站需要设置User-Agent标题

下面的代码应该有用..

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));

答案 1 :(得分:1)

在我的情况下,SSL已被禁用且仅允许TLS。我改为使用HTTPWebRequest代替。请注意,我使用的是.NET 4.0,并且没有TLS1.2作为选项,所以我硬编码了值(3072):

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)