远程服务器返回错误:(403)禁止

问题描述:

代码如下:



string rssft =" http://www.ft.com/rss/home/us&quot ;;

var docft = XDocument.Load(rssft);




上述代码docft中的
包含空值并发生错误远程服务器返回错误:(403)禁止。



请帮帮我



提前致谢

the code is given below

string rssft = "http://www.ft.com/rss/home/us";
var docft = XDocument.Load(rssft);


in the above code docft contains null value and the error occurred The remote server returned an error: (403) Forbidden.

pls help me

Thanks in advance







URL中没有重载XDocument.Load: https:// msdn .microsoft.com / zh-cn / library / system.xml.linq.xdocument.load(v = vs.110).aspx [ ^ ]



你应该这样做:



Hi,


There is no overload with URL in XDocument.Load : https://msdn.microsoft.com/en-us/library/system.xml.linq.xdocument.load(v=vs.110).aspx[^]

you should do :

var m_URL = "http://www.ft.com/rss/home/us"; // Set your URL
string xmlStr;
using(var wc = new WebClient())
{
wc.Headers.Add("user-agent", "Mozilla/5.0 (Windows; Windows NT 5.1; rv:1.9.2.4) Gecko/20100611 Firefox/3.6.4");
    xmlStr = wc.DownloadString(m_URL);
}
var xmlDoc = new XmlDocument();
xmlDoc.LoadXml(xmlStr);