我想通过GET请求向服务器端请求来自不同服务器的映像。如果请求服务器响应状态代码200一切正常。 但我经常将FOUND 302重定向作为状态代码。在这种情况下,我不需要 response.body 。
这是我用来从facebook检索用户图片的代码。由于facebook进行重定向,我得到302作为状态代码,而不是response.body。
def uri = new URI("http://graph.facebook.com/1000345345345/picture?width=200&height=200")
def requestFactory = new SimpleClientHttpRequestFactory()
def request = requestFactory.createRequest(uri, HttpMethod.GET)
try {
def response = request.execute()
def statusCode = response.statusCode
if (statusCode == HttpStatus.FOUND) {
log.debug "302"
// how do I get the response body?
}
if (statusCode == HttpStatus.OK) {
return response.body.bytes
}
else if(statusCode == HttpStatus.FORBIDDEN) {
throw new IllegalAccessError(response.statusCode.toString())
}
} catch(ex) {
log.error "Exception while querying $url", ex
}
return null
如何在302上获得正确的响应正文?
答案 0 :(得分:1)
你可以用它。你的方法是:
def queryUrl(url, ...) {}
您可以使用:
if (statusCode == HttpStatus.FOUND) {
def newUrl = response.connection.responses.getHeaders().Location[0]
log.debug "302 redirect to ${newUrl}"
return queryUrl(newUrl, "")
}
这将请求重定向的网址。