我刚开始使用 groovy,并开始使用它来测试一些 REST 服务。由于“序言中不允许内容”,我在解析来自我们服务的 XML 响应时遇到问题。经过一段时间的搜索,我发现一篇文章说开头可能有一个字节顺序标记。为了弥补这一点,我按照他们的方法在第一个 << 之前修剪了字符。然后解析响应。虽然这有效,但我还被告知问题是响应返回为“传输编码:分块”。
使用 HTTPBuilder,有没有办法在不修剪字符的情况下处理分块响应?
如果我尝试:
def http = new HTTPBuilder('url')
http.request( Method.valueOf("GET"), XML )
我收到“序言消息中不允许内容”。但是:
http.request( Method.valueOf("GET"), TEXT )
可以工作,但需要修剪文本,直到第一个 在将响应发送到 XmlParser 之前。
I'm new to using groovy and have started to use it to test some REST services. I'm having an issue parsing my XML response from our service due to 'Content not allowed in prolog.' After awhile searching I came across a post saying there might be a Byte Order Marker at the beginning. To compensate I followed their approach to trim the characters before the first < and then parse the response. While this works, I was also told the issue is that the response is coming back as 'Transfer-Encoding: chunked'.
Using HTTPBuilder, is there a way to handle chunked responses without trimming characters off?
If I try:
def http = new HTTPBuilder('url')
http.request( Method.valueOf("GET"), XML )
I get the 'Content not allowed in prolog message. However:
http.request( Method.valueOf("GET"), TEXT )
Works, but requires trimming the text until the first < before sending the response to XmlParser.
发布评论
评论(3)
当我需要与 IIS 服务器交互时,我遇到了同样的问题。返回的 XML 在 Web 服务器返回的实际 XML 前面有一个虚假字符。我像这样解决了这个问题:
I had the same issue when I needed to interact with an IIS server. The XML returned had a bogus character in front of the actual XML returned by the web server. I worked around it like this:
HTTPBuilder 类有一个 setContentEncoding() 方法允许您指定响应的内容类型。
也许是这样的:
希望这有帮助。
The HTTPBuilder class has a setContentEncoding() method that allows you to specify the response's content-type.
Maybe something like:
Hope this helps.
我也遇到了这个问题,并且通过 https 连接 IIS 服务器。这是 Wim Deblauwe 对 POST 请求的回答的一点补充。您必须在请求中发送与您期望的响应中不同的类型。
发送一个 POST,其中 XML 作为请求类型,TEXT 作为响应类型。然后,将文本响应解析为 XML。这对我有用。
在 Groovy 中:
I was having this problem as well hitting an IIS server over https. Here is a little addition to Wim Deblauwe's answer for a POST request. You have to send a different type in the request than you expect in the response.
Send a POST with XML as the request type and TEXT as the response type. Then, parse the text response into XML. This worked for me.
In Groovy: