问题
When trying to consume an RDF feed from craigslist, I'm running into a (401) Unauthorized WebException. I'm able to read the two commented out URLs directly below it with no issues. If I'm able to directly navigate to the craigslist URL using Internet Explorer with no problem, why does it fail when trying to load the data using an XmlReader?
http://portland.craigslist.org/search/sss?query=mac&srchType=A&format=rss
static void Main(string[] args)
{
XmlReader reader = XmlReader.Create("http://portland.craigslist.org/search/sss?query=mac&srchType=A&format=rss");
//XmlReader reader = XmlReader.Create("http://wdfw.wa.gov/news/newsrss.php");
//XmlReader reader = XmlReader.Create("http://rss.slashdot.org/Slashdot/slashdot");
Rss10FeedFormatter rf = new Rss10FeedFormatter();
rf.ReadFrom(reader);
Console.ReadLine();
}
}
回答1:
Use XmlResolver
XmlUrlResolver resolver = new XmlUrlResolver();
resolver.Credentials = System.Net.CredentialCache.DefaultCredentials;
XmlReaderSettings settings = new XmlReaderSettings();
settings.XmlResolver = resolver;
// create a reader and populate the document
XmlReader reader = XmlReader.Create(rssFeedUrl, settings); //
doc = new XmlDocument();
doc.Load(reader);
来源:https://stackoverflow.com/questions/12826208/using-xmlreader-get-unauthorized-webexception