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:tv.icntv.log.crawl2.commons.HttpClientUtil.java

public static String getContentPost(String url, Map<String, String> params) throws IOException {
    HttpPost httpPost = new HttpPost(url);
    List<NameValuePair> formParams = new ArrayList<NameValuePair>();
    if (params != null) {
        Set set = params.keySet();
        Iterator iterator = set.iterator();
        while (iterator.hasNext()) {
            Object key = iterator.next();
            Object value = params.get(key);
            formParams.add(new BasicNameValuePair(key.toString(), value.toString()));
        }//from w  w  w  . java  2s.  c  o m
    }
    httpPost.setHeader(new BasicHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"));
    httpPost.setEntity(new UrlEncodedFormEntity(formParams, HTTP.UTF_8));

    // execute
    CloseableHttpClient client = HttpClientHolder.getClient();
    return EntityUtils.toString(client.execute(httpPost).getEntity(), "utf-8");
}

From source file:org.nuxeo.elasticsearch.http.readonly.HttpClient.java

public static String get(String url) throws IOException {
    CloseableHttpClient client = HttpClients.createDefault();
    HttpGet httpget = new HttpGet(url);
    try (CloseableHttpResponse response = client.execute(httpget)) {
        HttpEntity entity = response.getEntity();
        return entity != null ? EntityUtils.toString(entity) : null;
    }/*w w  w.  j  a v  a2  s  .  co m*/
}

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

/**
 * Get data from the provided URL/*from  ww  w .  j a  va 2  s .c o  m*/
 */
public static InputStream httpGet(final String url) throws Exception {
    final CloseableHttpClient httpClient = HttpClients.createDefault();
    final HttpGet get = new HttpGet(url);
    return httpClient.execute(get).getEntity().getContent();
}

From source file:tv.icntv.common.HttpClientUtil.java

public static String getContent(String url, Charset charset) throws IOException {
    HttpGet request = new HttpGet(url);
    request.setConfig(RequestConfig.custom().setConnectionRequestTimeout(CONNECTION_REQUEST_TIMEOUT)
            .setSocketTimeout(SOCKET_TIMEOUT).build());
    CloseableHttpClient client = HttpClientHolder.getClient();
    CloseableHttpResponse response = null;

    response = client.execute(request);
    return EntityUtils.toString(response.getEntity(), charset);

}

From source file:com.atomiton.watermanagement.ngo.util.HttpRequestResponseHandler.java

public static String sendGet(String serverURL, String qParams) throws Exception {

    String url = serverURL + "?" + qParams;

    CloseableHttpClient client = HttpClients.createDefault();
    HttpGet request = new HttpGet(url);

    HttpResponse response = client.execute(request);

    // System.out.println("\nSending 'GET' request to URL : " + url);

    BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

    StringBuffer result = new StringBuffer();
    String line = "";
    while ((line = rd.readLine()) != null) {
        result.append(line);/* www.  j  ava2  s .c  o  m*/
    }
    client.close();
    return result.toString();
}

From source file:bit.changepurse.wdk.util.CheckedExceptionMethods.java

public static CloseableHttpResponse execute(CloseableHttpClient client, HttpRequestBase request) {
    try {/*from ww w  . ja v  a 2s. co  m*/
        return client.execute(request);
    } catch (IOException e) {
        throw new UncheckedException(e);
    }
}

From source file:com.econcept.pingconnectionutility.utility.PingConnectionUtility.java

private static void httpPingable(String targetURI) throws IOException, URISyntaxException {
    CloseableHttpClient httpClient = HttpClients.createDefault();
    HttpGet httpGet = new HttpGet();
    httpGet.setURI(new URI(targetURI));
    CloseableHttpResponse response = httpClient.execute(httpGet);

    int currentCode = response.getStatusLine().getStatusCode();
    try {//w w w .  j a v a2 s. c  om

        if (currentCode >= 200 && currentCode < 300) {
            HttpEntity entity = response.getEntity();

            InputStream responseStream = entity.getContent();

            StringWriter writer = new StringWriter();

            IOUtils.copy(responseStream, writer, "UTF-8");

            System.out.println("Target Server are ok: " + currentCode);
            System.out.println(writer.toString());

            EntityUtils.consume(entity);
        } // if
        else {
            System.out.println("Target Server are not ok: " + currentCode);
        } // else
    } // try
    finally {
        response.close();
    } // finally

}

From source file:communication.Communicator.java

/**
 * Gets all the cartrackers from the Movementsystem api
 *
 * @return A list with all the cartrackers from the Movementsystem api
 * @throws IOException Could be thrown when executing the http request, or
 * when converting the result to a String
 *//*  w ww .ja v a  2 s.  co m*/
public static List<CarTracker> getAllCartrackers() throws IOException {
    CloseableHttpClient httpClient = HttpClientBuilder.create().build();
    HttpGet get = new HttpGet(BASE_URL_PRODUCTION);
    HttpResponse response = httpClient.execute(get);

    String responseString = EntityUtils.toString(response.getEntity(), CHARACTER_SET);
    Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ss").create();
    try {
        return gson.fromJson(responseString, new TypeToken<List<CarTracker>>() {
        }.getType());
    } catch (JsonSyntaxException ex) {
        Logger.getLogger(Communicator.class.getName()).log(Level.SEVERE, null, ex);
        throw new IOException(ex.getMessage());
    }
}

From source file:org.eclipse.dirigible.cli.apis.ImportProjectAPI.java

private static void executeRequest(CloseableHttpClient httpClient, HttpPost postRequest)
        throws IOException, ClientProtocolException {
    try {/* w  ww  .j  av a 2  s.  c o  m*/
        CloseableHttpResponse response = httpClient.execute(postRequest);
        try {
            logger.log(Level.INFO, "----------------------------------------");
            logger.log(Level.INFO, response.getStatusLine().toString());
            HttpEntity resultEntity = response.getEntity();
            if (resultEntity != null) {
                logger.log(Level.INFO, "Response content length: " + resultEntity.getContentLength());
            }
            EntityUtils.consume(resultEntity);
        } finally {
            response.close();
        }
    } finally {
        httpClient.close();
    }
}

From source file:downloadwolkflow.getWorkFlowList.java

private static void downloadWorkFlow(String detailUrl, CloseableHttpClient httpclient) {
    try {/* ww w . j a v  a 2s.  c o  m*/
        HttpGet httpget = new HttpGet(detailUrl);
        HttpResponse response = httpclient.execute(httpget);
        String page = EntityUtils.toString(response.getEntity());
        Document mainDoc = Jsoup.parse(page);
        Element downloadEle = mainDoc.select("div#myexp_content ul li a").first();
        if (downloadEle == null) {
            downloadEle = mainDoc.select("div#myexp_content ul li:nth-child(1) span a").first();
        }

        String downloadUrl = downloadEle.attributes().get("href");
        Thread.sleep(500);
        if (downloadUrl.contains("download")) {
            downloadFiles(downloadUrl, httpclient);
        } else {
            System.out.println(detailUrl + " do not contain valuable resource");
        }
    } catch (IOException ex) {
        Logger.getLogger(getWorkFlowList.class.getName()).log(Level.SEVERE, null, ex);
    } catch (InterruptedException ex) {
        Logger.getLogger(getWorkFlowList.class.getName()).log(Level.SEVERE, null, ex);
    }
}