XmlReader在阅读RSS提要时出错

本文关键字:出错 RSS XmlReader | 更新日期: 2023-09-27 18:10:40

我正在尝试使用以下代码从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上工作时没有错误。

我该怎么解决这个问题?

XmlReader在阅读RSS提要时出错

您的站点要求设置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));

在我的情况下,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)