1

我有以下代码:

final remote = new HTTPBuilder("http://example.com")
final text = remote.request(GET) { req ->
    uri.path = "/path"
    requestContentType = TEXT
    uri.query = ['name': 'value']
    response.success = { resp, text ->
        text
    }
    response.failure = { resp ->
        println "Rest failure (${req.getURI()}): ${resp.status}: ${resp.statusLine.reasonPhrase}"
    }
}

它在抛出:

java.io.IOException: Not in GZIP format
    at java.util.zip.GZIPInputStream.readHeader(GZIPInputStream.java:143)
    at java.util.zip.GZIPInputStream.<init>(GZIPInputStream.java:58)
    at java.util.zip.GZIPInputStream.<init>(GZIPInputStream.java:67)
    at groovyx.net.http.GZIPEncoding$GZIPDecompressingEntity.getContent(GZIPEncoding.java:73)
    at org.apache.http.util.EntityUtils.consume(EntityUtils.java:83)
    at org.apache.http.conn.BasicManagedEntity.ensureConsumed(BasicManagedEntity.java:99)
    at org.apache.http.conn.BasicManagedEntity.consumeContent(BasicManagedEntity.java:112)
    at groovyx.net.http.HTTPBuilder.doRequest(HTTPBuilder.java:515)
    at groovyx.net.http.HTTPBuilder.doRequest(HTTPBuilder.java:441)
    at groovyx.net.http.HTTPBuilder.request(HTTPBuilder.java:373)
    at groovyx.net.http.HTTPBuilder$request.call(Unknown Source)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:42)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:108)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:120)

如何解决这个问题?

4

1 回答 1

0

必须设置内容编码:

final text = remote.request(GET, TEXT) { req ->
    uri.path = "/path"
    uri.query = ['name': 'value']
    response.success = { resp, text ->
        text
    }
    response.failure = { resp ->
        println "Rest failure (${req.getURI()}): ${resp.status}: ${resp.statusLine.reasonPhrase}"
    }
}
于 2013-09-17T17:10:27.627 回答