【发布时间】:2011-09-20 16:27:21
【问题描述】:
_rssSource = @"http://feeds.bbci.co.uk/news/world/rss.xml";
XmlReader reader = XmlReader.Create(_rssSource);
我有上面的代码,它在 GetResponse() 处引发以下错误:
System.Net.WebException was unhandled by user code
Message=The remote server returned an error: (403) Forbidden.
Source=System
StackTrace:
at System.Net.HttpWebRequest.GetResponse()
at System.Xml.XmlDownloadManager.GetNonFileStream(Uri uri, ICredentials credentials, IWebProxy proxy, RequestCachePolicy cachePolicy)
at System.Xml.XmlDownloadManager.GetStream(Uri uri, ICredentials credentials, IWebProxy proxy, RequestCachePolicy cachePolicy)
at System.Xml.XmlUrlResolver.GetEntity(Uri absoluteUri, String role, Type ofObjectToReturn)
at System.Xml.XmlReaderSettings.CreateReader(String inputUri, XmlParserContext inputContext)
at System.Xml.XmlReader.Create(String inputUri, XmlReaderSettings settings, XmlParserContext inputContext)
at System.Xml.XmlReader.Create(String inputUri)
我试图跟踪通过提琴手请求的内容,但除了对包含代码的页面的请求之外,没有显示任何请求。该链接通过 ie/visual studio 工作正常。
我不确定它是否与代理服务器/IIS/互联网设置有关。
知道发生了什么吗?
【问题讨论】:
-
您的代码示例甚至没有显示 GetResponse()。如果没有相关代码,很难看出哪里出了问题。
-
你认为
XmlReader.Create是在制造 403 错误吗?有一个真正的 403 错误从网络返回,你只是因为某种原因看不到它。尝试使用纯WebRequest或WebClient获取内容,就像实验一样。 -
代码对我有用。我刚刚声明了
string _rssSource;并输入了您的代码。我可以读取文件。 -
@David Stratton:
XmlReader.Create(string)将执行请求并在参数是 URL 时自动获取响应。 msdn.microsoft.com/en-us/library/w8k674bf.aspx -
GetResponse() 发生在 XmlReader.Create() 中,您可以从堆栈跟踪中看到。我在家里也可以正常工作,这就是为什么我认为这一定是某种设置问题。