使用XmlReader获取未经授权的WebException [英] Using XmlReader get Unauthorized WebException

查看:110
本文介绍了使用XmlReader获取未经授权的WebException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当尝试使用craigslist中的RDF提要时,我遇到了(401)未经授权的WebException.我可以在其正下方读取两个注释掉的URL,而不会出现任何问题.如果我可以使用Internet Explorer毫无问题地直接导航到craigslist URL,为什么在尝试使用XmlReader加载数据时却失败了?

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

推荐答案

使用XmlResolver

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

这篇关于使用XmlReader获取未经授权的WebException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆