当浏览器中的相同 GET 请求显示 XML 响应时,为什么 HTTPClient 响应会生成 JSON 流?
我第一次使用 HttpClient 向 Web 服务(由 WADL 定义)发出请求,如果我在浏览器中执行 Get 请求,它会使用 XML 进行响应,则我想将响应解编为有意义的对象。但是当我显示代码的响应时,它似乎是 JSON。我是不是少了一步?或者有没有办法可以转换?
I am using HttpClient for the first time to make a request to a web service (defined by WADL), I'd like to unmarshall the response into a meaningful object, in the browser if I perform the Get request it response with XML. But when I display the response from the code it seems to be JSON. Am I missing a step? or is there a way this can be converted?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
浏览器和代码是否有可能发送不同的
Accept:
标头? http://www.w3.org/Protocols/rfc2616/rfc2616 -sec14.html#sec14.1您可以尝试从代码中发送“application/xml,text/xml”的 Accept 标头,看看是否会发生变化?另一件要做的事情是使用 Firebug 等工具检查浏览器发送的标头,因为如果 URL 相同,则几乎可以肯定是正在发送的标头之一改变了行为。
Is it possible that the browser and the code are sending different
Accept:
headers? http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.1Can you try sending an Accept header of "application/xml, text/xml", say, from the code and see if that changes things? Another thing to do is to check the headers the browser is sending using a tool like Firebug, for example, because if the URL is the same, it's almost certainly one of the headers being sent that's changing the behaviour.
我猜测网络服务会根据用户代理或请求的其他不同内容返回不同的响应。检查所有参数是否相同。还要检查 HttpClient 是否执行 GET 而不是 POST。除非您的 Web 浏览器和 HttpClient 请求之间的参数或标头不同,否则响应中不应有任何差异。
I'm guessing the webservice is returning a different response depending on the user-agent or something else different about the request. Check all teh parameters are identical. Also check the HttpClient is doing a GET not a POST. There should be no difference in the response unless parameters or headers differ between your web-browser and your HttpClient request.