我正在尝试使用 Apache HtppClient 执行 HTTP GET 并读取响应。我目前的努力看起来像这样
def listAlertsUrl = "http://example.com/whatever"
HttpGet listAlertsRequest = new HttpGet(listAlertsUrl)
HttpResponse response = httpClient.execute(listAlertsRequest)
HttpEntity entity = response.entity
EntityUtils.consume(entity)
// newReader() is a method that Groovy adds to InputStream
Reader jsonResponse = entity.content.newReader()
try {
// do stuff with the Reader
} finally {
jsonResponse.close()
}
但是,当我尝试使用时Reader
出现错误:
原因:java.io.IOException:尝试从关闭的流中读取。
我正在努力寻找如何使用 HttpClient v.4 的示例,因为我的 Google 搜索只返回具有完全不同 API 的旧版本的示例。