Example usage for org.apache.http.impl.client HttpClients createDefault

List of usage examples for org.apache.http.impl.client HttpClients createDefault

Introduction

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

Prototype

public static CloseableHttpClient createDefault() 

Source Link

Document

Creates CloseableHttpClient instance with default configuration.

Usage

From source file:com.dc.runbook.dt.locator.RunBookLocator.java

private static String locateRunBookFileOverHttp(String uri) {
    String responseBody;/* ww w  .  j  a va2 s  . c om*/

    try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
        HttpGet httpget = new HttpGet(uri);
        // Create a custom response handler
        ResponseHandler<String> responseHandler = new ResponseHandler<String>() {

            @Override
            public String handleResponse(final HttpResponse response) throws IOException {
                int status = response.getStatusLine().getStatusCode();
                if (status >= 200 && status < 300) {
                    HttpEntity entity = response.getEntity();
                    return entity != null ? EntityUtils.toString(entity) : null;
                } else {
                    throw new RunBookException(
                            "Unexpected response status: " + status + " while locating RunBook");
                }
            }

        };
        responseBody = httpClient.execute(httpget, responseHandler);
    } catch (IOException e) {
        throw new RunBookException("Unable to locate RunBook for URI : " + uri, e);
    }
    return responseBody;
}

From source file:com.salesforce.dva.argus.service.callback.HttpClientPool.java

@Override
protected HttpClient createObject() {
    return HttpClients.createDefault();
}

From source file:cn.org.once.cstack.utils.TestUtils.java

/**
 * Return the content of an URL.//from  w ww .j a  v a2 s .c  o  m
 *
 * @param url
 * @return
 * @throws ParseException
 * @throws IOException
 */
public static String getUrlContentPage(String url) throws ParseException, IOException {
    HttpGet request = new HttpGet(url);
    CloseableHttpClient httpClient = HttpClients.createDefault();
    HttpResponse response = httpClient.execute(request);
    HttpEntity entity = response.getEntity();
    return EntityUtils.toString(entity);
}

From source file:com.restfiddle.handler.http.DeleteHandler.java

public RfResponseDTO process(RfRequestDTO rfRequestDTO) throws IOException {
    RfResponseDTO response = null;//from  www. j a  v a  2  s.  c  o m
    CloseableHttpClient httpclient = HttpClients.createDefault();
    HttpDelete httpDelete = new HttpDelete(rfRequestDTO.getApiUrl());
    httpDelete.addHeader("Content-Type", "application/json");
    try {
        response = processHttpRequest(httpDelete, httpclient);
    } finally {
        httpclient.close();
    }
    return response;
}

From source file:processingtest.CitySense.java

private CloseableHttpResponse executeRequest(String URL) throws IOException {
    CloseableHttpClient httpclient = HttpClients.createDefault();
    HttpGet httpget = new HttpGet(URL);
    System.out.println(httpget.getURI());
    CloseableHttpResponse response = httpclient.execute(httpget);
    return response;
}

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);/*  w  ww  .j av  a  2  s  . co m*/
    }
}

From source file:ch.sourcepond.maven.plugin.jenkins.it.utils.HttpServerStartupBarrier.java

@Override
protected CloseableHttpClient createClient() {
    return HttpClients.createDefault();
}

From source file:eionet.gdem.utils.HttpUtils.java

/**
 * Downloads remote file/*www  .j  a  v a 2s .  com*/
 * @param url URL
 * @return Downloaded file
 * @throws DCMException If an error occurs.
 * @throws IOException If an error occurs.
 */
public static byte[] downloadRemoteFile(String url) throws DCMException, IOException {
    byte[] responseBody = null;
    CloseableHttpClient client = HttpClients.createDefault();

    // Create a method instance.
    HttpGet method = new HttpGet(url);
    // Execute the method.
    CloseableHttpResponse response = null;
    try {
        response = client.execute(method);
        HttpEntity entity = response.getEntity();
        int statusCode = response.getStatusLine().getStatusCode();

        if (statusCode != HttpStatus.SC_OK) {
            LOGGER.error("Method failed: " + response.getStatusLine().getReasonPhrase());
            throw new DCMException(BusinessConstants.EXCEPTION_SCHEMAOPEN_ERROR,
                    response.getStatusLine().getReasonPhrase());
        }

        // Read the response body.
        InputStream instream = entity.getContent();
        responseBody = IOUtils.toByteArray(instream);

        // Deal with the response.
        // Use caution: ensure correct character encoding and is not binary data
        // System.out.println(new String(responseBody));
        /*catch (HttpException e) {
        LOGGER.error("Fatal protocol violation: " + e.getMessage());
        e.printStackTrace();
        throw e;*/
    } catch (IOException e) {
        LOGGER.error("Fatal transport error: " + e.getMessage());
        e.printStackTrace();
        throw e;
    } finally {
        // Release the connection.
        response.close();
        method.releaseConnection();
        client.close();
    }
    return responseBody;
}

From source file:com.restfiddle.handler.http.PutHandler.java

public RfResponseDTO process(RfRequestDTO rfRequestDTO) throws IOException {
    RfResponseDTO response = null;//  w  w w  . ja va 2 s.c o  m
    CloseableHttpClient httpclient = HttpClients.createDefault();
    HttpPut httpPut = new HttpPut(rfRequestDTO.getApiUrl());
    httpPut.addHeader("Content-Type", "application/json");
    httpPut.setEntity(new StringEntity(rfRequestDTO.getApiBody()));
    try {
        response = processHttpRequest(httpPut, httpclient);
    } finally {
        httpclient.close();
    }
    return response;
}

From source file:com.currencyfair.minfraud.MinFraudBuilder.java

/**
 * Construct and configure the {@link MinFraud} implementation and return
 * it./*from   w w  w. j  ava  2  s  .  co m*/
 * @return A newly built and configured MinFraud implementation.
 */
public MinFraud build() {
    MinFraudImpl minFraud = new MinFraudImpl();
    if (httpClient != null) {
        minFraud.setHttpClient(httpClient);
    } else {
        minFraud.setHttpClient(HttpClients.createDefault());
    }
    if (methodFactory != null) {
        minFraud.setMethodFactory(methodFactory);
    } else {
        minFraud.setMethodFactory(new HttpMethodFactoryImpl());
    }
    if (uri != null) {
        minFraud.setEndpoint(new SimpleValueFactory<URI>(uri));
    } else {
        minFraud.setEndpoint(new SimpleValueFactory<URI>(MinFraud.DEFAULT_URL));
    }
    minFraud.setLicenseKey(new SimpleValueFactory<String>(licenseKey));
    return minFraud;
}