use of org.openecard.apache.http.message.BasicHttpRequest in project open-ecard by ecsec.
the class StreamHttpClientConnectionTest method consumeEntity.
private void consumeEntity(StreamHttpClientConnection conn, String hostName, int numIt) throws IOException, HttpException {
HttpContext ctx = new BasicHttpContext();
HttpRequestExecutor httpexecutor = new HttpRequestExecutor();
HttpResponse response = null;
DefaultConnectionReuseStrategy reuse = new DefaultConnectionReuseStrategy();
int i = 0;
while (i == 0 || (i < numIt && reuse.keepAlive(response, ctx))) {
i++;
// send request and receive response
HttpRequest request = new BasicHttpRequest("GET", "/");
HttpRequestHelper.setDefaultHeader(request, hostName);
response = httpexecutor.execute(request, conn, ctx);
conn.receiveResponseEntity(response);
HttpEntity entity = response.getEntity();
assertNotNull(entity);
// consume entity
byte[] content = FileUtils.toByteArray(entity.getContent());
// read header and check if content size is correct
Header lengthHeader = response.getFirstHeader("Content-Length");
long length = Long.parseLong(lengthHeader.getValue());
assertNotNull(lengthHeader);
assertEquals(entity.getContentLength(), length);
assertEquals(content.length, length);
// consume everything from the entity and close stream
EntityUtils.consume(entity);
}
}
Aggregations