Example usage for org.apache.http.util EntityUtils consume

List of usage examples for org.apache.http.util EntityUtils consume

Introduction

In this page you can find the example usage for org.apache.http.util EntityUtils consume.

Prototype

public static void consume(HttpEntity httpEntity) throws IOException 

Source Link

Usage

From source file:NginxIT.java

@Test(expected = IllegalArgumentException.class)
public void testName() throws Exception {
    String baseUrl = System.getProperty("cache.base.url");
    HttpGet get = new HttpGet(baseUrl);
    CloseableHttpClient httpClient = HttpClients.createDefault();

    try (CloseableHttpResponse response = httpClient.execute(get)) {
        HttpEntity entity = response.getEntity();
        EntityUtils.consume(entity);
    }/*from   w w  w  .j av  a 2  s.  com*/
}

From source file:org.apache.marmotta.platform.core.services.http.response.StatusCodeResponseHandler.java

@Override
public Integer handleResponse(HttpResponse response) throws ClientProtocolException, IOException {
    try {//from   w  ww  . j  a va 2s  . c  om
        return response.getStatusLine().getStatusCode();
    } finally {
        EntityUtils.consume(response.getEntity());
    }
}

From source file:gov.nasa.ensemble.common.http.HttpUtils.java

public static void consume(HttpRequestBase request, HttpResponse response) throws IOException {
    if (response != null) {
        try {/*w  w  w. j  av a 2s  .co  m*/
            EntityUtils.consume(response.getEntity());
        } catch (IOException e) {
            if (request != null) {
                throw new IOException("releasing entity for " + request.getMethod(), e);
            } else {
                throw new IOException("releasing entity", e);
            }
        }
    }
}

From source file:neembuu.release1.httpclient.utils.NHttpClientUtils.java

/**
 * Get the content type of a url.//from   ww  w  .  j a v  a 2 s .c  o m
 * @param url
 * @return the content type.
 */
public static String getContentType(String url, DefaultHttpClient httpClient) {
    try {
        //DefaultHttpClient httpClient = NHttpClient.getInstance();
        HttpGet httpGet = new HttpGet(url);
        HttpResponse response = httpClient.execute(httpGet);
        String contentType = response.getEntity().getContentType().getValue();
        System.out.println("Content Type: " + contentType);
        EntityUtils.consume(response.getEntity());
        return contentType;
    } catch (IOException ex) {
        ex.printStackTrace();
    }
    return null;
}

From source file:myexamples.MyExamples.java

public static void getExample() throws IOException {
    HttpGet httpGet = new HttpGet("http://localhost:9200/gb/tweet/9");
    CloseableHttpResponse response1 = httpclient.execute(httpGet);
    // The underlying HTTP connection is still held by the response object
    // to allow the response content to be streamed directly from the network socket.
    // In order to ensure correct deallocation of system resources
    // the user MUST call CloseableHttpResponse#close() from a finally clause.
    // Please note that if response content is not fully consumed the underlying
    // connection cannot be safely re-used and will be shut down and discarded
    // by the connection manager.
    try {//from ww w .  java2  s  .c om
        System.out.println(response1.getStatusLine());
        HttpEntity entity1 = response1.getEntity();
        // do something useful with the response body
        // and ensure it is fully consumed
        if (entity1 != null) {
            long len = entity1.getContentLength();
            if (len != -1 && len < 2048) {
                System.out.println(EntityUtils.toString(entity1));
            } else {
                System.out.println("entity length=" + len);
            }
        }

        EntityUtils.consume(entity1);
    } finally {
        response1.close();
    }
}

From source file:com.net.plus.common.http.handler.ByteArrayResponseHandler.java

/**
  * Returns the response body as a byte array if the response was successful (a
  * 2xx status code). If no response body exists, this returns null. If the
  * response was unsuccessful (>= 300 status code), throws an
  * {@link HttpResponseException}.//  w  w  w  . j  a va2 s.co  m
  */
public byte[] handleResponse(HttpResponse response) throws ClientProtocolException, IOException {
    StatusLine statusLine = response.getStatusLine();
    HttpEntity entity = response.getEntity();
    if (statusLine.getStatusCode() >= 300) {
        EntityUtils.consume(entity);
        throw new HttpResponseException(statusLine.getStatusCode(), statusLine.getReasonPhrase());
    }
    byte[] result = null;
    long len = entity.getContentLength();
    if (len != -1 && len < 4096) {
        result = EntityUtils.toByteArray(entity);
    } else {
        result = Util.toByteArray(entity);
    }
    EntityUtils.consume(entity);
    return result;
}

From source file:com.jive.myco.seyren.core.service.checker.JsonNodeResponseHandler.java

@Override
public JsonNode handleResponse(HttpResponse response) throws ClientProtocolException, IOException {
    HttpEntity entity = response.getEntity();
    try {//from w  ww. ja  v a2 s.c  om
        InputStream stream = entity.getContent();
        return MAPPER.readTree(stream);
    } finally {
        EntityUtils.consume(entity);
    }
}

From source file:com.emc.esu.api.rest.CommonsInputStreamWrapper.java

@Override
public void close() throws IOException {
    in.close();/*from w  w  w .  ja  va2s  . com*/
    if (response != null) {
        EntityUtils.consume(response.getEntity());
        response = null;
    }
}

From source file:net.oneandone.shared.artifactory.StatusCodeCodeLessThanScMultipleChoicesResponseHandler.java

final protected HttpEntity returnEntityWhenStatusValid(HttpResponse response) throws IOException {
    final StatusLine statusLine = response.getStatusLine();
    final HttpEntity entity = response.getEntity();
    if (statusLine.getStatusCode() >= HttpStatus.SC_MULTIPLE_CHOICES) {
        EntityUtils.consume(entity);
        throw new HttpResponseException(statusLine.getStatusCode(), statusLine.getReasonPhrase());
    }/*from  www  . j a  va2  s.c  om*/
    return entity;
}

From source file:com.github.sardine.impl.io.ConsumingInputStream.java

@Override
public void close() throws IOException {
    EntityUtils.consume(response.getEntity());
}