Using XmlReader get Unauthorized WebException

 ̄綄美尐妖づ 提交于 2019-12-24 00:14:58

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!