【发布时间】:2010-08-04 18:35:25
【问题描述】:
我有一个 Silverlight 应用程序,它使用 WebClient 类与 REST Web 服务通信。它在 IE 8 和 Chrome 5 中运行良好,但在 Firefox 3 中 Web 服务调用失败。
我已经缩小了问题范围:Firefox 3 正在更改我的 HTTP 请求的 Accept 标头。这是我的简化代码:
// Use the ClientHttp stack.
WebRequest.RegisterPrefix("http://", WebRequestCreator.ClientHttp);
// Call the web service.
var webClient = new WebClient();
webClient.Headers["Accept"] = "application/json";
webClient.DownloadStringAsync(someUrl);
使用 Fiddler 调查管道上的数据,请求的标头被替换:
GET /1/36497f32-1acd-4f4e-a946-622b3f20dfa5/Content/GetAllTextContentsForUser/0 HTTP/1.1
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Host: localhost:88
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.8) Gecko/20100722 Firefox/3.6.8
请注意第二行“Accept”——它已被替换为 text/html、xml 和其他格式。不是我要找的——我绝对需要 JSON 回来。
有没有办法阻止 Firefox 修改我的 Accept 标头?
【问题讨论】:
-
OK -- 所有浏览器都替换接受字符串。 IE 和 Chrome 将其替换为“/”,而 Mozilla 则使用上面列出的时髦字符串:developer.yahoo.com/dotnet/silverlight/2.0/requestheaders.html
-
我希望我的 Web 服务调用接受 JSON,而不是 XML,该死。救命!
标签: silverlight http-headers webclient