Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 读取RSS源时发生XmlReader错误_C#_Rss_Xmlreader_Rss Reader - Fatal编程技术网

C# 读取RSS源时发生XmlReader错误

C# 读取RSS源时发生XmlReader错误,c#,rss,xmlreader,rss-reader,C#,Rss,Xmlreader,Rss Reader,我正在尝试使用以下代码从中读取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/po

我正在尝试使用以下代码从中读取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的不同计算机上尝试了这一点,但是错误仍然存在,所以这似乎不是问题所在。为了使跟踪更加困难,代码在运行时不会出错


我应该怎么解决这个问题呢?

您的站点需要设置
用户代理
标题

下面的代码应该可以工作

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)

哇!那就行了!!!你能解释一下为什么这个代码有效吗?我查阅了用户代理,但不确定我是否完全理解这一点。这件事困扰了我好几个星期。。。非常感谢你@克丽丝:我不知道该怎么解释(用我有限的英语)。只需删除
wc.Headers
行并重试。这是行不通的。。。我怎么找到的?可能是经验。如果我尝试用以下内容加载RSS xml,这会有什么不同:XDocument rsslink=XDocument.load(weblink)@ChrisL很可能
XDocument.Load
不会发布
用户代理
标题。这不是必须的,但有些网站可能需要它。()