处理“分块”使用 HTTPBuilder/Groovy 编码响应

发布于 2024-10-03 09:45:37 字数 484 浏览 9 评论 0 原文

我刚开始使用 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

草莓味的萝莉 2024-10-10 09:45:38

当我需要与 IIS 服务器交互时,我遇到了同样的问题。返回的 XML 在 Web 服务器返回的实际 XML 前面有一个虚假字符。我像这样解决了这个问题:

StringReader reader = builder.get( path: 'rcserver/systeminfo.xml', contentType: ContentType.TEXT )
def text = reader.getText()
def xml = new XmlSlurper().parseText(text.substring(1));

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:

StringReader reader = builder.get( path: 'rcserver/systeminfo.xml', contentType: ContentType.TEXT )
def text = reader.getText()
def xml = new XmlSlurper().parseText(text.substring(1));
長街聽風 2024-10-10 09:45:38

HTTPBuilder 类有一个 setContentEncoding() 方法允许您指定响应的内容类型。

也许是这样的:

http.contentEncoding = ContentEncoding.Type.GZIP
http.request( Method.GET, XML)

希望这有帮助。

The HTTPBuilder class has a setContentEncoding() method that allows you to specify the response's content-type.

Maybe something like:

http.contentEncoding = ContentEncoding.Type.GZIP
http.request( Method.GET, XML)

Hope this helps.

追我者格杀勿论 2024-10-10 09:45:38

我也遇到了这个问题,并且通过 https 连接 IIS 服务器。这是 Wim Deblauwe 对 POST 请求的回答的一点补充。您必须在请求中发送与您期望的响应中不同的类型。

发送一个 POST,其中 XML 作为请求类型,TEXT 作为响应类型。然后,将文本响应解析为 XML。这对我有用。

在 Groovy 中:

def reader = http.request(Method.POST, ContentType.TEXT){
    uri.path = "myPath.api"
    send ContentType.XML, postBodyXml
}
def text = reader.getText()
def resultxml = new XmlSlurper().parseText(text.substring(1));

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:

def reader = http.request(Method.POST, ContentType.TEXT){
    uri.path = "myPath.api"
    send ContentType.XML, postBodyXml
}
def text = reader.getText()
def resultxml = new XmlSlurper().parseText(text.substring(1));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文