C#不会加载某些XML,但工程在浏览器 [英] C# Won't Load A Certain XML, But Works in Browser

查看:122
本文介绍了C#不会加载某些XML,但工程在浏览器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新(ER)到C#,但我有Java和VB.NET的背景下,这样的跳跃很容易。这个周末,我开始了新的小型项目,C#和从interwebs公众XML饲料。但我有加载XML的一个问题。这里是我的代码:

I'm new(er) to C#, but I have a background with Java and VB.NET, so jumping in was easy. This weekend I started a new mini-project with C# and a public XML feed from the interwebs. But I'm having a problem loading the XML. Here's my code:

string url = ... ;
...
XmlDocument xmlDoc = new XmlDocument();
...         
try{                
    xmlDoc.Load(url);
}catch(Exception e){
    Console.WriteLine(e);
}

当我尝试加载XML,它抛出一个异常:

When I attempt to load the XML, it throws an exception:

http://i.stack.imgur.com /Xo2Ra.png
(新手不能附加图片,抱歉)

http://i.stack.imgur.com/Xo2Ra.png (Newbies can't attach pictures, sorry)

我是不是在所有的时候我的代码并没有感到惊讶工作。我被搞清楚的问题是启动了标准的故障排除过程。我满心指望我的代码是错误的。为了验证这一理论,我在网上找到了一个随机XML饲料和它复制到我的代码。令我吃惊的是,它装载就好了。现在我怀疑转移到目标XML。它工作正常,在Chrome和FireFox(载荷0.734秒),并不需要(向公众开放)任何凭据,并且有效/孔形成。

I wasn't at all surprised when my code didn't work. I started the standard troubleshooting process by figuring out where the problem was. I fully expected my code to be faulty. To test this theory, I found a random XML feed on the web and copied it into my code. To my surprise, it loaded just fine. Now my suspicion shifted to the target XML. It works fine in Chrome and FireFox (loads in .734 seconds), does not require any credentials (open to public), and is valid/well formed.

这时我想起,我已经在几个月前写一个JavaScript使用此相同的饲料。我发射了起来,并发现它也是完美的工作。

Then I remembered a JavaScript that I had written a few months ago that uses this same feed. I fired that up, and found it to also be working perfectly.

我在这里的损失,因为我的两个代码和XML似乎是罚款。有谁知道这可怎么解决吗?我是否需要使用HttpWebRequest和传递到XmlDocument的(我不知道如何做到这一点)?是否有更多的方式来解决此?

I'm at a loss here because both my code and XML seem to be fine. Does anyone know how this can be fixed? Do I need to use a HttpWebRequest and pass to the XmlDocument (I don't know how to do this)? Are there any more ways to troubleshoot this?

推荐答案

我在我的评论指出,XMLDocument.load方法相比是farely原始从浏览器完全成熟的请求。当您使用的Proxy-或数据包示踪剂像小提琴手,你会发现,例如IE9提出要求,包括特定的头文件:

As i indicated in my comment, XmlDocument.Load is farely primitive compared to a full blown request from a browser. When you use a proxy- or packet tracer like Fiddler, you will find that for example IE9 makes a request including specific headers:

GET
http://stats.us.playstation。 COM / WARHAWK / XmlFeedAction.action启动= 1&安培;年底= 1
HTTP / 1.1接受:text / html的,是application / xhtml + xml的 /
接受语言:恩美的User-Agent:Mozilla的/ 5.0(兼容; MSIE 9.0;
的Windows NT 6.1;三叉戟/ 5.0)接受编码:gzip,紧缩
连接:保持活动主持人: stats.us.playstation.com饼干:
JSESSIONID = HLygTblTG13HhXqqw80jw9Wdhw0q03dxcQLp04fD3Q5yChYvPGn6 -882698034;
SONYCOOKIE1 = 543467712.20480.0000

GET http://stats.us.playstation.com/warhawk/XmlFeedAction.action?start=1&end=1 HTTP/1.1 Accept: text/html, application/xhtml+xml, / Accept-Language: en-US User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0) Accept-Encoding: gzip, deflate Connection: Keep-Alive Host: stats.us.playstation.com Cookie: JSESSIONID=HLygTblTG13HhXqqw80jw9Wdhw0q03dxcQLp04fD3Q5yChYvPGn6!-882698034; SONYCOOKIE1=543467712.20480.0000

现在的网络服务器的行为受到在请求中指定的标头。在这种情况下,接受和用户代理发挥作用。

Now the webserver's behavior is subjected to the headers specified in a request. In this case, the Accept and user-agent play a role. I can succesfully load the xml content in a XmlDocument by including some fake headers like the following:

        string url = "http://stats.us.playstation.com/warhawk/XmlFeedAction.action?start=1&end=1";

        WebClient client = new WebClient();
        client.Headers["User-Agent"] = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/14.0.835.202 Safari/535.1";
        client.Headers["Accept"] = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
        string data = client.DownloadString(url);

        XmlDocument doc = new XmlDocument();
        doc.LoadXml(data);

这篇关于C#不会加载某些XML,但工程在浏览器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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