问题描述
以下下载RSS文件的代码在我正在整理的应用程序中运行了好几周:
公共类RssFeedReader
{
public IEnumerable< post> ;; ReadFeed(string url)
{
var rssFeed = XDocument.Load(url);
var posts =来自rssFeed.Descendants(item)中的项目
选择新帖子
{
Title = item.Element(title).Value,
描述= item.Element(描述).Value,
// PublishedDate = item.Element(.Value};
返回帖子;
}
}
url在我正在使用的测试用例中是
http://www.news.com.au/national/rss
我得到的回复是:
异常抛出:System.dll中的System.Net.WebException
和
附加信息:远程服务器返回错误:(403 )禁止。
System.Net.WebException occurre d
HResult = -2146233079
消息=远程服务器返回错误:(403)禁止。
来源=系统
StackTrace:at System.Net.HttpWebRequest.GetResponse()
InnerException:
如上所述,这一切都运作得非常顺利直到3天前,然后停止工作没有明显的原因..我已经到处寻找答案,并尝试了各种事情,如直接加载到XDocument等 - 但它仍然给出相同或类似的错误。
我拿了那段代码片段,没有对它做任何改动,写了一个调用它的小程序。我使用完全相同的网址 - 它工作正常!
那么主程序中的配置是否正确?
非常感谢任何帮助!
我尝试过:
除此之外,我把这个块放在'var rssFeed'前面并运行它:
WebClient webClient = new WebClient();
webClient.Headers.Add(user-agent,MyRSSReader / 1.0;);
XmlReader readers = XmlReader.Create(webClient.OpenRead (url));
var xmlDocument = new XmlDocument();
xmlDocument.Load(readers);
并且尝试运行xmlDocument.Load(读者)时出现完全相同的403错误。
我在CodeProject上读到了关于403错误的一些建议,并尝试使用https而不仅仅是http: - 它因为不同(但可以理解)原因而失败:
异常抛出:System.dll中的System.Security.Authentication.AuthenticationException
附加信息:根据验证,远程证书无效过程。
The following code to download an RSS file was working perfectly for weeks in an application I'm putting together:
public class RssFeedReader
{
public IEnumerable<post>; ReadFeed(string url)
{
var rssFeed = XDocument.Load(url);
var posts = from item in rssFeed.Descendants(item)
select new Post
{
Title = item.Element(title).Value,
Description = item.Element(description).Value,
// PublishedDate = item.Element(.Value};
return posts;
}
}
url in the test case I'm using is
http://www.news.com.au/national/rss
The response I'm getting is:
"Exception thrown: System.Net.WebException in System.dll "
and
"Additional information: The remote server returned an error: (403) Forbidden."
System.Net.WebException occurred
HResult=-2146233079
Message=The remote server returned an error: (403) Forbidden.
Source=System
StackTrace: at System.Net.HttpWebRequest.GetResponse()
InnerException:
As mentioned this was all working so smoothly up until 3 days ago then stopped working for no apparent reason.. I've looked all over the place for an answer and tried various things like loading directly into an XDocument etc - but it still gives the same or similar error.
I took that code snippet, made no changes to it and wrote a small program b that calls it. I used exactly the same url - and it works fine!
So could it be something is configured incorrectly in the main program?
Any help very much appreciated!
What I have tried:
Amongst other things, I put this block in front of 'var rssFeed' and ran it:
WebClient webClient = new WebClient();
webClient.Headers.Add( user-agent , MyRSSReader/1.0 ;);
XmlReader readers = XmlReader.Create(webClient.OpenRead(url));
var xmlDocument = new XmlDocument();
xmlDocument.Load(readers);
and got exactly the same 403 error as it tried to run "xmlDocument.Load(readers); "
I read some suggestions on CodeProject about the 403 error and tried using https instead of just http: - it failed for a diffeet (but understandable) reason:
Exception thrown: System.Security.Authentication.AuthenticationException in System.dll
Additional information: The remote certificate is invalid according to the validation procedure.
推荐答案
static XDocument LoadRss(string url)
{
var request = (HttpWebRequest)WebRequest.Create(url);
request.UserAgent = "(Anything other than an empty string)";
using (var response = request.GetResponse())
using (var stream = response.GetResponseStream())
{
return XDocument.Load(stream);
}
}
public IEnumerable<Post> ReadFeed(string url)
{
var rssFeed = LoadRss(url);
...
这篇关于403将URL加载到xdocument时出现禁止错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!