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:eu.diacron.crawlservice.app.Util.java

public static String getCrawlid(URL urltoCrawl) {
    String crawlid = "";
    System.out.println("start crawling page");

    CredentialsProvider credsProvider = new BasicCredentialsProvider();

    credsProvider.setCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT),
            new UsernamePasswordCredentials(Configuration.REMOTE_CRAWLER_USERNAME,
                    Configuration.REMOTE_CRAWLER_PASS));
    CloseableHttpClient httpclient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build();
    try {/*from  www. ja va  2  s. c o m*/
        //HttpPost httppost = new HttpPost("http://diachron.hanzoarchives.com/crawl");
        HttpPost httppost = new HttpPost(Configuration.REMOTE_CRAWLER_URL_CRAWL_INIT);

        List<NameValuePair> urlParameters = new ArrayList<NameValuePair>();
        urlParameters.add(new BasicNameValuePair("name", UUID.randomUUID().toString()));
        urlParameters.add(new BasicNameValuePair("scope", "page"));
        urlParameters.add(new BasicNameValuePair("seed", urltoCrawl.toString()));

        httppost.setEntity(new UrlEncodedFormEntity(urlParameters));

        System.out.println("Executing request " + httppost.getRequestLine());
        CloseableHttpResponse response = httpclient.execute(httppost);
        try {
            System.out.println("----------------------------------------");
            System.out.println(response.getStatusLine());
            System.out.println("----------------------------------------");

            BufferedReader in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
            String inputLine;
            while ((inputLine = in.readLine()) != null) {
                crawlid = inputLine;
            }
            in.close();
            EntityUtils.consume(response.getEntity());
        } finally {
            response.close();
        }
    } catch (IOException ex) {
        Logger.getLogger(Util.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
        try {
            httpclient.close();
        } catch (IOException ex) {
            Logger.getLogger(Util.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    return crawlid;
}

From source file:com.bbc.util.ClientCustomSSL.java

public static String clientCustomSLL(String mchid, String path, String data) throws Exception {
    KeyStore keyStore = KeyStore.getInstance("PKCS12");

    System.out.println("?...");
    FileInputStream instream = new FileInputStream(new File("/payment/apiclient_cert.p12"));
    try {//from www.  jav a 2 s .co  m
        keyStore.load(instream, mchid.toCharArray());
    } finally {
        instream.close();
    }

    // Trust own CA and all self-signed certs
    SSLContext sslcontext = SSLContexts.custom().loadKeyMaterial(keyStore, mchid.toCharArray()).build();
    // Allow TLSv1 protocol only
    SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext, new String[] { "TLSv1" },
            null, SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);
    CloseableHttpClient httpclient = HttpClients.custom().setSSLSocketFactory(sslsf).build();
    try {

        HttpPost httpost = new HttpPost(path);
        httpost.addHeader("Connection", "keep-alive");
        httpost.addHeader("Accept", "*/*");
        httpost.addHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
        httpost.addHeader("Host", "api.mch.weixin.qq.com");
        httpost.addHeader("X-Requested-With", "XMLHttpRequest");
        httpost.addHeader("Cache-Control", "max-age=0");
        httpost.addHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0) ");

        httpost.setEntity(new StringEntity(data, "UTF-8"));
        CloseableHttpResponse response = httpclient.execute(httpost);
        try {
            HttpEntity entity = response.getEntity();
            System.out.println(response.getStatusLine());
            if (entity != null) {
                System.out.println("Response content length: " + entity.getContentLength());
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(entity.getContent()));
                String text;
                StringBuffer sb = new StringBuffer("");
                while ((text = bufferedReader.readLine()) != null) {
                    System.out.println(text);
                    sb.append(text);
                }
                return sb.toString();

            }
            EntityUtils.consume(entity);
            return "";
        } finally {
            response.close();
        }
    } finally {
        httpclient.close();
    }
}

From source file:org.jmonkey.external.bintray.BintrayApiClient.java

public List<BintrayFile> search(final String query) throws IOException {

    try (CloseableHttpClient httpClient = this.createClient()) {

        HttpUriRequest request = RequestBuilder.get().setUri("https://api.bintray.com/search/packages/maven?q=*"
                + cleanQuery(query) + "*&owner=" + config.getSubject() + "&repo=" + config.getRepo()).build();

        try (CloseableHttpResponse httpResponse = httpClient.execute(request)) {

            int statusCode = httpResponse.getStatusLine().getStatusCode();

            if (statusCode >= 400) {
                // @TODO: provide a consumable error
            }//w w w  .j a  v a 2s .  co m

            HttpEntity entity = httpResponse.getEntity();
            String entityString = EntityUtils.toString(entity);
            EntityUtils.consume(entity);

            List<BintrayFile> results = JmeResourceWebsite.getObjectMapper().readValue(entityString,
                    new TypeReference<List<BintrayFile>>() {
                    });

            return results;
        }
    }
}

From source file:de.bytefish.fcmjava.client.http.apache.DefaultHttpClient.java

private <TRequestMessage> void internalPost(TRequestMessage requestMessage) throws IOException {

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

        // Evaluate the Response:
        evaluateResponse(response);//from   w w  w  .j  a v a  2s. co m

        // 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);
        }
    }

}

From source file:org.wuspba.ctams.ui.server.ServerUtils.java

public static String delete(URI uri) throws IOException {
    CloseableHttpClient httpclient = HttpClients.createDefault();
    HttpDelete httpDelete = new HttpDelete(uri);

    String ret;/*from   w ww  . ja v a 2  s. co  m*/

    try (CloseableHttpResponse response = httpclient.execute(httpDelete)) {
        HttpEntity responseEntity = response.getEntity();

        ret = convertEntity(responseEntity);

        EntityUtils.consume(responseEntity);
    }

    return ret;
}

From source file:com.pipinan.githubcrawler.GithubCrawler.java

/**
 *
 * @param username the owner name of respoitory
 * @param reponame the name of respoitory
 * @param path which folder would you like to save the zip file
 * @throws IOException/* w  ww. ja  v a 2s  . c  o m*/
 */
public void crawlRepoZip(String username, String reponame, String path) throws IOException {
    GHRepository repo = github.getRepository(username + "/" + reponame);

    HttpClient httpclient = getHttpClient();
    //the url pattern is https://github.com/"USER_NAME"/"REPO_NAME"/archive/master.zip
    HttpGet httpget = new HttpGet("https://github.com/" + username + "/" + reponame + "/archive/master.zip");
    HttpResponse response = httpclient.execute(httpget);
    try {
        System.out.println(response.getStatusLine());
        if (response.getStatusLine().toString().contains("200 OK")) {

            //the header "Content-Disposition: attachment; filename=JSON-java-master.zip" can find the filename
            String filename = null;
            Header[] headers = response.getHeaders("Content-Disposition");
            for (Header header : headers) {
                System.out.println("Key : " + header.getName() + " ,Value : " + header.getValue());
                String tmp = header.getValue();
                filename = tmp.substring(tmp.lastIndexOf("filename=") + 9);
            }

            if (filename == null) {
                System.err.println("Can not find the filename in the response.");
                System.exit(-1);
            }

            HttpEntity entity = response.getEntity();

            BufferedInputStream bis = new BufferedInputStream(entity.getContent());
            String filePath = path + filename;
            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File(filePath)));
            int inByte;
            while ((inByte = bis.read()) != -1) {
                bos.write(inByte);
            }
            bis.close();
            bos.close();

            EntityUtils.consume(entity);
        }
    } finally {

    }

}

From source file:org.wuspba.ctams.ws.ITSoloResultController.java

@Test
public void testListAll() throws Exception {
    CloseableHttpClient httpclient = HttpClients.createDefault();

    URI uri = new URIBuilder().setScheme(PROTOCOL).setHost(HOST).setPort(PORT).setPath(PATH).build();

    LOG.info("Connecting to " + uri.toString());

    HttpGet httpGet = new HttpGet(uri);

    try (CloseableHttpResponse response = httpclient.execute(httpGet)) {
        assertEquals(response.getStatusLine().toString(), IntegrationTestUtils.OK_STRING);

        HttpEntity entity = response.getEntity();

        CTAMSDocument doc = IntegrationTestUtils.convertEntity(entity);

        assertEquals(doc.getSoloContestResults().size(), 1);
        testEquality(doc.getSoloContestResults().get(0), TestFixture.INSTANCE.soloResult);

        EntityUtils.consume(entity);
    }/*from w w  w . j a  va  2s  . c  o  m*/
}

From source file:org.everit.authentication.cas.ecm.tests.SampleApp.java

/**
 * Ping CAS login URL./* w  ww  .  java2 s .c  om*/
 */
public static void pingCasLoginUrl(final BundleContext bundleContext) throws Exception {
    CloseableHttpClient httpClient = new SecureHttpClient(null, bundleContext).getHttpClient();

    HttpGet httpGet = new HttpGet(CAS_LOGIN_URL + "?" + LOCALE);
    HttpResponse httpResponse = null;
    try {
        httpResponse = httpClient.execute(httpGet);
        Assert.assertEquals(CAS_PING_FAILURE_MESSAGE, HttpServletResponse.SC_OK,
                httpResponse.getStatusLine().getStatusCode());
    } catch (Exception e) {
        LOGGER.error(CAS_PING_FAILURE_MESSAGE, e);
        Assert.fail(CAS_PING_FAILURE_MESSAGE);
    } finally {
        if (httpResponse != null) {
            EntityUtils.consume(httpResponse.getEntity());
        }
        httpClient.close();
    }
}

From source file:org.piraso.ui.base.manager.HttpUpdateManager.java

@Override
public void execute() throws IOException {
    if (StringUtils.isNotBlank(model.getName())) {
        super.execute();
    }//w  ww .ja  va2s .c  o m

    try {
        doExecuteInternal();
    } finally {
        EntityUtils.consume(responseEntity);
    }
}

From source file:org.qucosa.camel.component.opus4.Opus4DataSource.java

public OpusDocument get(Opus4ResourceID qid) throws Exception {
    URIBuilder uriBuilder = new URIBuilder(host + WEBAPI_DOCUMENT_RESOURCE_PATH + "/" + qid.getIdentifier());
    HttpGet request = new HttpGet(uriBuilder.build());

    HttpResponse response = httpClient.execute(request);

    if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
        return OpusDocument.Factory.parse(response.getEntity().getContent());
    } else {//www .  j av  a2 s . com
        String reason = response.getStatusLine().getReasonPhrase();
        EntityUtils.consume(response.getEntity());
        throw new Exception(reason);
    }
}