【问题标题】:XmlReader.Create() The remote server returned an error: (403) ForbiddenXmlReader.Create() 远程服务器返回错误:(403) Forbidden
【发布时间】: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 错误从网络返回,你只是因为某种原因看不到它。尝试使用纯 WebRequestWebClient 获取内容,就像实验一样。
  • 代码对我有用。我刚刚声明了string _rssSource; 并输入了您的代码。我可以读取文件。
  • @David Stratton:XmlReader.Create(string) 将执行请求并在参数是 URL 时自动获取响应。 msdn.microsoft.com/en-us/library/w8k674bf.aspx
  • GetResponse() 发生在 XmlReader.Create() 中,您可以从堆栈跟踪中看到。我在家里也可以正常工作,这就是为什么我认为这一定是某种设置问题。

标签: c# asp.net


【解决方案1】:

尝试使用与您的代码在同一台机器上运行的浏览器访问相同的提要,以消除易于检测的问题,例如所需的凭据或网络限制。如果您的浏览器可以很好地读取提要,那么您可能需要调整请求属性。

由 XmlReader.Create() 提供的默认 .NET 用户代理可能被 RSS 服务明确禁止。一个简单的测试方法是在 WebClient 对象中显式设置您的用户代理字符串,然后在您的 URL 上调用 OpenRead() 方法:

WebClient webClient = new WebClient();
webClient.Headers.Add("user-agent", "MyRSSReader/1.0");

XmlReader reader = XmlReader.Create(webClient.OpenRead(_rssSource));

如果您发明的用户代理字符串不起作用,您可以尝试欺骗 well known Feed 阅读器或浏览器的字符串。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-13
    相关资源
    最近更新 更多