Search in sources :

Example 1 with HttpRequest

use of org.openecard.apache.http.HttpRequest 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);
    }
}
Also used : HttpRequest(org.openecard.apache.http.HttpRequest) BasicHttpRequest(org.openecard.apache.http.message.BasicHttpRequest) HttpRequestExecutor(org.openecard.apache.http.protocol.HttpRequestExecutor) HttpEntity(org.openecard.apache.http.HttpEntity) Header(org.openecard.apache.http.Header) BasicHttpContext(org.openecard.apache.http.protocol.BasicHttpContext) BasicHttpContext(org.openecard.apache.http.protocol.BasicHttpContext) HttpContext(org.openecard.apache.http.protocol.HttpContext) DefaultConnectionReuseStrategy(org.openecard.apache.http.impl.DefaultConnectionReuseStrategy) HttpResponse(org.openecard.apache.http.HttpResponse) BasicHttpRequest(org.openecard.apache.http.message.BasicHttpRequest)

Aggregations

Header (org.openecard.apache.http.Header)1 HttpEntity (org.openecard.apache.http.HttpEntity)1 HttpRequest (org.openecard.apache.http.HttpRequest)1 HttpResponse (org.openecard.apache.http.HttpResponse)1 DefaultConnectionReuseStrategy (org.openecard.apache.http.impl.DefaultConnectionReuseStrategy)1 BasicHttpRequest (org.openecard.apache.http.message.BasicHttpRequest)1 BasicHttpContext (org.openecard.apache.http.protocol.BasicHttpContext)1 HttpContext (org.openecard.apache.http.protocol.HttpContext)1 HttpRequestExecutor (org.openecard.apache.http.protocol.HttpRequestExecutor)1