Example usage for org.apache.http.impl.client CloseableHttpClient execute

List of usage examples for org.apache.http.impl.client CloseableHttpClient execute

Introduction

In this page you can find the example usage for org.apache.http.impl.client CloseableHttpClient execute.

Prototype

public CloseableHttpResponse execute(final HttpUriRequest request) throws IOException, ClientProtocolException 

Source Link

Usage

From source file:com.turn.ttorrent.common.Utils.java

/**
 * Resolves a file from URI and returns its byte array.
 * //from   w  ww.j  a v a 2  s.  com
 * @param uri
 * @return
 * @throws ClientProtocolException
 * @throws IOException
 */
public static byte[] resolveUrlFileToByteArray(URI uri) throws ClientProtocolException, IOException {
    CloseableHttpClient httpclient = HttpClients.createDefault();

    try {

        HttpGet get = new HttpGet(uri);
        HttpResponse response = httpclient.execute(get);
        HttpEntity entity = response.getEntity();

        if (entity != null) {
            InputStream inputStream = entity.getContent();
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            IOUtils.copy(inputStream, outputStream);
            return outputStream.toByteArray();
        }

        throw new IOException("Could not resolve file... [" + uri + "]");

    } finally {
        httpclient.close();
    }

}

From source file:com.github.tmyroadctfig.icloud4j.util.ICloudUtils.java

/**
 * Parses a JSON response from the request.
 *
 * @param httpClient the HTTP client.//www. j av a  2  s.  c o m
 * @param httpGet the request.
 * @param responseClass the type of JSON object to parse the values into.
 * @param <T> the type to parse into.
 * @return the object.
 * @throws ICloudException if there was an error returned from the request.
 */
public static <T> T parseJsonResponse(CloseableHttpClient httpClient, HttpGet httpGet, Class<T> responseClass) {
    try (CloseableHttpResponse response = httpClient.execute(httpGet)) {
        String rawResponseContent = new StringResponseHandler().handleResponse(response);

        try {
            return fromJson(rawResponseContent, responseClass);
        } catch (JsonSyntaxException e1) {
            Map<String, Object> errorMap = fromJson(rawResponseContent, Map.class);
            throw new ICloudException(response, errorMap);
        }
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    }
}

From source file:org.ebaysf.ostara.upgrade.util.GitUtils.java

private static StringBuilder readData(Set<String> committers, String url)
        throws IOException, ClientProtocolException {
    CloseableHttpClient httpclient = HttpClients.createDefault();
    HttpGet httpGet = new HttpGet(url);
    CloseableHttpResponse response1 = httpclient.execute(httpGet);
    BufferedReader br = new BufferedReader(new InputStreamReader((response1.getEntity().getContent())));

    if (response1.getStatusLine().getStatusCode() != 200) {
        LOG.warn("Unable to get git committers for " + url + " status code is "
                + response1.getStatusLine().getStatusCode());
        return null;
    }//from  ww w . j  av  a  2s.co m

    StringBuilder sb = new StringBuilder();
    String output;
    while ((output = br.readLine()) != null) {
        sb.append(output);
    }
    return sb;
}

From source file:downloadwolkflow.getWorkFlowList.java

private static void downloadFiles(String downloadUrl, CloseableHttpClient httpclient) {
    HttpGet httpget = new HttpGet(downloadUrl);
    HttpEntity entity = null;/*w  ww  .  j av a 2 s .  c  om*/
    try {
        HttpResponse response = httpclient.execute(httpget);
        entity = response.getEntity();
        if (entity != null) {
            InputStream is = entity.getContent();
            String filename = downloadUrl.split("/")[downloadUrl.split("/").length - 1].split("\\?")[0];
            System.out.println(filename);
            BufferedInputStream bis = new BufferedInputStream(is);
            BufferedOutputStream bos = new BufferedOutputStream(
                    new FileOutputStream(new File("data/" + filename)));
            int readedByte;
            while ((readedByte = bis.read()) != -1) {
                bos.write(readedByte);
            }
            bis.close();
            bos.close();
        }
    } catch (IOException ex) {
        Logger.getLogger(DownloadFileTest.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:org.openo.nfvo.monitor.umc.monitor.common.HttpClientUtil.java

/**
 * @Title httpPost//from   w w w .j  a va2  s  .  co  m
 * @Description TODO(realize the rest interface to access by httpPost)
 * @param url
 * @throws Exception
 * @return int (return StatusCode,If zero error said)
 */
public static int httpPost(String url) throws Exception {

    CloseableHttpClient httpClient = HttpClients.createDefault();
    HttpPost httpPost = new HttpPost(url);
    try {
        CloseableHttpResponse res = httpClient.execute(httpPost);
        try {
            return res.getStatusLine().getStatusCode();
        } finally {
            res.close();
        }
    } catch (ParseException e) {
        LOGGER.error("HttpClient throw ParseException:" + url, e);
    } catch (IOException e) {
        LOGGER.error("HttpClient throw IOException:" + url, e);
    } finally {
        try {
            httpClient.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            LOGGER.error("HttpClient Close throw IOException", e);
        }
    }

    return 0;

}

From source file:apidemo.APIDemo.java

public static String sendPost(String url, byte[] data)
        throws UnsupportedEncodingException, IOException, NoSuchAlgorithmException {
    //        logger.info("url: " + url);
    CloseableHttpClient httpclient = HttpClients.createDefault();
    HttpPost httpPost = new HttpPost(url);
    httpPost.setEntity(new ByteArrayEntity(data));

    try (CloseableHttpResponse res = httpclient.execute(httpPost)) {
        HttpEntity entity = res.getEntity();

        InputStream inputStream = entity.getContent();
        String sResponse = IOUtils.toString(inputStream, "UTF-8");

        return sResponse;
    }/*from   ww w  . ja va 2  s .  c  o m*/
}

From source file:org.fcrepo.camel.indexing.solr.integration.TestUtils.java

public static void httpPost(final String url, final String content, final String mimeType) throws Exception {
    final CloseableHttpClient httpClient = HttpClients.createDefault();
    final HttpPost post = new HttpPost(url);
    post.addHeader(Exchange.CONTENT_TYPE, mimeType);
    post.setEntity(new StringEntity(content));
    httpClient.execute(post);
}

From source file:org.openo.nfvo.monitor.umc.monitor.common.HttpClientUtil.java

/**
 * @Title httpDelete//from w  w  w . j a va 2s  .c  o m
 * @Description TODO(realize the rest interface to access by httpDelete)
 * @param url
 * @return
 * @throws Exception
 * @return int (return StatusCode,If zero error said)
 */
public static int httpDelete(String url) throws Exception {

    CloseableHttpClient httpClient = HttpClients.createDefault();
    HttpDelete httpDelete = new HttpDelete(url);
    try {
        CloseableHttpResponse res = httpClient.execute(httpDelete);
        try {
            return res.getStatusLine().getStatusCode();
        } finally {
            res.close();
        }
    } catch (ParseException e) {
        LOGGER.error("HttpClient throw ParseException:" + url, e);
    } catch (IOException e) {
        LOGGER.error("HttpClient throw IOException:" + url, e);
    } finally {
        try {
            httpClient.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            LOGGER.error("HttpClient Close throw IOException", e);
        }
    }

    return 0;

}

From source file:com.oneapm.base.tools.UrlPostMethod.java

public static String urlGetMethod(String url) {
    CloseableHttpClient httpClient = HttpClientBuilder.create().build();
    HttpGet get = new HttpGet(url);
    HttpEntity entity = null;// ww w .ja va 2  s  .com
    String json = null;
    try {
        CloseableHttpResponse response = httpClient.execute(get);
        json = EntityUtils.toString(response.getEntity(), "UTF-8");
    } catch (IOException e) {
        e.printStackTrace();
    }
    return json.toString();

}

From source file:fridgegameinstaller.MCJsonConf.java

public static void getJson(String path, int mb) throws IOException {
    CloseableHttpClient httpclient = HttpClients.createDefault();
    HttpGet httpGet = new HttpGet("http://api.fridgegame.com/mcconf/Fridgegame.json");

    CloseableHttpResponse response1 = httpclient.execute(httpGet);
    try {/*from www .  j  av a  2 s .  co  m*/
        System.out.println(httpGet.toString());
        System.out.println(response1.getStatusLine());
        BufferedReader br = new BufferedReader(new InputStreamReader(response1.getEntity().getContent()));
        String a;
        String output = "";
        while ((a = br.readLine()) != null) {
            output += a + "\n";
        }
        System.out.println(output);
        try {

            JSONObject json = new JSONObject(output);
            String mcArgs = json.getString("minecraftArguments");
            String regex = "(-Xmx[^ ]*)";
            Pattern p = Pattern.compile(regex);
            Matcher m = p.matcher(mcArgs);
            String newArgs = m.replaceAll("-Xmx" + mb + "M");
            json.put("minecraftArguments", newArgs);
            FileWriter file = new FileWriter(path);

            try {
                file.write(json.toString());

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

            } finally {
                file.flush();
                file.close();
            }

        } catch (JSONException e) {

        }
    } finally {
        response1.close();
    }

}