Using XmlReader get Unauthorized WebException

2019-07-20 17:31发布

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();
    }
}

enter image description here

Why unauthorized?

1条回答
你好瞎i
2楼-- · 2019-07-20 17:52

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);
查看更多
登录 后发表回答