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:com.splunk.shuttl.archiver.util.UtilsHttp.java

/**
 * @param response/*from w ww. ja v  a 2 s.c om*/
 */
public static void consumeResponse(HttpResponse response) {
    try {
        if (response != null)
            EntityUtils.consume(response.getEntity());
    } catch (IOException e) {
        logger.error(did("Tried to consume http response of archive bucket request", e, "no exception",
                "response", response));
    }
}

From source file:com.sematext.ag.http.HttpUtils.java

public static boolean processRequestSilently(HttpClient httpClient, HttpRequestBase request) {
    try {/*from   ww w. j a v  a2  s. c om*/
        HttpResponse response = httpClient.execute(request);
        LOG.info("Event sent");
        if (HttpStatus.SC_OK != response.getStatusLine().getStatusCode()) {
            return false;
        }
        HttpEntity entity = response.getEntity();
        EntityUtils.consume(entity);
        return true;
    } catch (IOException e) {
        LOG.error("Sending event failed", e);
        return false;
    } finally {
        request.releaseConnection();
    }
}

From source file:org.sonar.plugins.buildstability.ci.jenkins.JenkinsUtils.java

public static void doLogin(HttpClient client, String hostName, String username, String password)
        throws IOException {
    String hudsonLoginEntryUrl = hostName + "/login";
    HttpGet loginLink = new HttpGet(hudsonLoginEntryUrl);
    HttpResponse response = client.execute(loginLink);
    checkResult(response.getStatusLine().getStatusCode(), hudsonLoginEntryUrl);
    EntityUtils.consume(response.getEntity());

    String location = hostName + "/j_acegi_security_check";
    boolean loggedIn = false;
    while (!loggedIn) {
        HttpPost loginMethod = new HttpPost(location);
        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
        nvps.add(new BasicNameValuePair("j_username", username));
        nvps.add(new BasicNameValuePair("j_password", password));
        nvps.add(new BasicNameValuePair("action", "login"));
        loginMethod.setEntity(new UrlEncodedFormEntity(nvps));
        try {/*ww w . ja v a2 s  .  c o m*/
            HttpResponse response2 = client.execute(loginMethod);
            if (response2.getStatusLine().getStatusCode() / 100 == 3) {
                // Commons HTTP client refuses to handle redirects for POST
                // so we have to do it manually.
                location = response2.getFirstHeader("Location").getValue();
            } else {
                checkResult(response2.getStatusLine().getStatusCode(), location);
                loggedIn = true;
            }
            EntityUtils.consume(response2.getEntity());
        } finally {
            loginMethod.releaseConnection();
        }
    }
}

From source file:org.jboss.narayana.rts.TxnHelper.java

public static Set<Link> beginTxn(Client client, String txurl) throws IOException {
    Response response = null;/* www . j av a 2 s  .c  o  m*/

    try {
        response = client.target(txurl).request()
                .post(Entity.entity(new Form(), MediaType.APPLICATION_FORM_URLENCODED_TYPE));
        Set<Link> links = response.getLinks();

        EntityUtils.consume((HttpEntity) response.getEntity());
        if (response.getStatus() != HttpURLConnection.HTTP_CREATED)
            throw new RuntimeException("beginTxn returned " + response.getStatus());

        return links;
    } finally {
        if (response != null)
            response.close();
    }
}

From source file:com.nibss.util.Request.java

public void get(String url) throws Exception {
    CloseableHttpClient httpclient = HttpClients.createDefault();
    try {/*  w w  w.  j  a  v a 2s.  c  o  m*/
        HttpGet httpGet = new HttpGet(url);
        CloseableHttpResponse response1 = httpclient.execute(httpGet);
        try {
            HttpEntity entity1 = response1.getEntity();

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

From source file:nayan.netty.client.FileUploadClient.java

private static void uploadFile() throws Exception {
    File file = new File("small.jpg");

    HttpEntity httpEntity = MultipartEntityBuilder.create()
            .addBinaryBody("file", file, ContentType.create("image/jpeg"), file.getName()).build();

    CloseableHttpClient httpclient = HttpClients.createDefault();
    HttpPost httppost = new HttpPost("http://localhost:8080");

    httppost.setEntity(httpEntity);/*ww  w.j a  v  a  2 s.c om*/
    System.out.println("executing request " + httppost.getRequestLine());

    CloseableHttpResponse response = httpclient.execute(httppost);

    System.out.println("----------------------------------------");
    System.out.println(response.getStatusLine());
    HttpEntity resEntity = response.getEntity();
    if (resEntity != null) {
        System.out.println("Response content length: " + resEntity.getContentLength());
    }

    EntityUtils.consume(resEntity);

    response.close();
}

From source file:org.opf_labs.fmts.fidget.droid.PRONOMSigGenerator.java

/**
 * @param sigdef//w  ww  .j  a  va2s  .  c  om
 */
public static void generatePRONOMSigFile(SigDefSubmission sigdef) {
    DefaultHttpClient httpclient = new DefaultHttpClient();
    try {

        HttpPost httpost = new HttpPost(SERVICE_URL);

        httpost.setEntity(new UrlEncodedFormEntity(createNameValuePairs(sigdef), Consts.UTF_8));

        HttpResponse response = httpclient.execute(httpost);
        HttpEntity entity = response.getEntity();

        // Print out:
        IOUtils.copy(entity.getContent(), System.out);
        // Finish up:
        EntityUtils.consume(entity);

    } catch (ClientProtocolException e) {
        e.printStackTrace();

    } catch (IOException e) {
        e.printStackTrace();

    } finally {
        // When HttpClient instance is no longer needed,
        // shut down the connection manager to ensure
        // immediate deallocation of all system resources
        httpclient.getConnectionManager().shutdown();
    }
}

From source file:BuildArgsIT.java

@Test
public void buildArgsContainerRunsCorrectly() throws Exception {
    String baseUrl = System.getProperty("app.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 . ja v a 2 s  .co m*/
}

From source file:com.jive.myco.seyren.core.util.graphite.ByteArrayResponseHandler.java

@Override
public byte[] handleResponse(HttpResponse response) throws ClientProtocolException, IOException {
    HttpEntity entity = response.getEntity();
    try {// w ww  .  jav a2 s .  c o  m
        InputStream stream = entity.getContent();
        return IOUtils.toByteArray(stream);
    } finally {
        EntityUtils.consume(entity);
    }
}

From source file:de.bytefish.fcmjava.client.utils.HttpUtils.java

private static <TRequestMessage> void internalPost(HttpClientBuilder httpClientBuilder,
        IFcmClientSettings settings, TRequestMessage requestMessage) throws Exception {

    try (CloseableHttpClient client = httpClientBuilder.build()) {

        // Initialize a new post Request:
        HttpPost httpPost = new HttpPost(settings.getFcmUrl());

        // Set the JSON String as data:
        httpPost.setEntity(new StringEntity(JsonUtils.getAsJsonString(requestMessage)));

        // Execute the Request:
        try (CloseableHttpResponse response = client.execute(httpPost)) {

            // Get the HttpEntity:
            HttpEntity entity = response.getEntity();

            // Let's be a good citizen and consume the HttpEntity:
            if (entity != null) {

                // Make Sure it is fully consumed:
                EntityUtils.consume(entity);
            }//  w  w  w  . j  a  v a2 s. c om
        }
    }
}