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.iluwatar.aggregator.microservices.ProductInventoryClientImpl.java

@Override
public int getProductInventories() {
    String response = "0";
    try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
        HttpGet httpGet = new HttpGet("http://localhost:51516/inventories");
        try (CloseableHttpResponse httpResponse = httpClient.execute(httpGet)) {
            response = EntityUtils.toString(httpResponse.getEntity());
        }//w ww . j ava  2  s  . co m
    } catch (IOException e) {
        e.printStackTrace();
    }
    return Integer.parseInt(response);
}

From source file:org.artifactory.repo.remote.browse.HtmlRepositoryBrowserTest.java

@BeforeMethod
public void setUp() {
    final CloseableHttpClient hc = HttpClients.createDefault();
    HttpExecutor httpExecutor = new HttpExecutor() {
        @Override/*from w  w  w  . jav  a 2s  .  c om*/
        public CloseableHttpResponse executeMethod(HttpRequestBase method) throws IOException {
            return hc.execute(method);
        }
    };
    urlLister = new HtmlRepositoryBrowser(httpExecutor);
    baseUrl = "http://blabla";
}

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

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

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

    HttpGet httpGet = new HttpGet(uri);

    try (CloseableHttpResponse response = httpclient.execute(httpGet)) {

        assertEquals(response.getStatusLine().toString(), IntegrationTestUtils.OK_STRING);

        HttpEntity entity = response.getEntity();

        String buff;//from  w  w w . j  a v a 2  s . co m
        try (BufferedReader in = new BufferedReader(new InputStreamReader(entity.getContent()))) {
            buff = in.readLine();
            while (buff != null) {
                assertEquals(buff, TestController.TEST_STRING);
                buff = in.readLine();
            }
        }

        EntityUtils.consume(entity);
    }
}

From source file:com.iluwatar.aggregator.microservices.ProductInformationClientImpl.java

@Override
public String getProductTitle() {
    String response = null;//from  ww w. ja va2 s  .  com
    try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
        HttpGet httpGet = new HttpGet("http://localhost:51515/information");
        try (CloseableHttpResponse httpResponse = httpClient.execute(httpGet)) {
            response = EntityUtils.toString(httpResponse.getEntity());
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return response;
}

From source file:io.tourniquet.junit.http.rules.examples.HttpServerPostResponseStubbingExample.java

@Test
public void testHttpServerPost_noParams() throws Exception {
    //prepare/*ww  w .  j  a  v  a2 s.  c  o m*/
    server.on(POST).resource("/action.do").respond("someContent");

    //act
    //act
    try (CloseableHttpClient client = HttpClients.createDefault();
            CloseableHttpResponse response = client.execute(post("http://localhost:55555/action.do"))) {
        String content = getString(response.getEntity());
        assertEquals("someContent", content);
    }
}

From source file:com.msds.km.service.Impl.DrivingLicenseRecognitionServcieiImpl.java

public DrivingLicenseRecognitionServcieiImpl() {
    httpclient = HttpClients.createDefault();
}

From source file:cat.calidos.morfeu.api.APITezt.java

@Before
public void setup() {

    client = HttpClients.createDefault();
    webappPrefix = getConfigurationVariable(WEBAPP_BASE_URL, DEFAULT_WEBAPP_BASE_URL);

}

From source file:org.apache.clerezza.commons.rdf.impl.sparql.SparqlClient.java

public Object queryResult(final String query) throws IOException {
    CloseableHttpClient httpclient = HttpClients.createDefault();
    HttpPost httpPost = new HttpPost(endpoint);
    List<NameValuePair> nvps = new ArrayList<NameValuePair>();
    nvps.add(new BasicNameValuePair("query", query));
    httpPost.setEntity(new UrlEncodedFormEntity(nvps));
    CloseableHttpResponse response2 = httpclient.execute(httpPost);
    HttpEntity entity2 = response2.getEntity();
    try {/*from ww w  .j  a  va2 s. c  om*/
        InputStream in = entity2.getContent();
        final String mediaType = entity2.getContentType().getValue();
        if ("application/sparql-results+xml".equals(mediaType)) {
            return SparqlResultParser.parse(in);
        } else {
            //assuming RDF response
            //FIXME clerezza-core-rdf to clerezza dependency
            Parser parser = Parser.getInstance();
            return parser.parse(in, mediaType);
        }
    } finally {
        EntityUtils.consume(entity2);
        response2.close();
    }

}

From source file:erainformatica.utility.JSONHelper.java

public static TelegramRequestResult<JSONObject> readJsonFromUrl(String url, InputFile file) {
    CloseableHttpClient httpClient = HttpClients.createDefault();
    HttpPost uploadFile = new HttpPost(url);

    MultipartEntityBuilder builder = MultipartEntityBuilder.create();
    //builder.addTextBody("field1", "yes", ContentType.TEXT_PLAIN);
    builder.addBinaryBody(file.getName(), file.getFile_content(), ContentType.APPLICATION_OCTET_STREAM,
            file.getName() + "." + file.getExtension());
    HttpEntity multipart = builder.build();

    uploadFile.setEntity(multipart);/* ww w  . j  a v  a2s.c o  m*/

    CloseableHttpResponse response = null;
    try {
        response = httpClient.execute(uploadFile);
    } catch (IOException ex) {
        Logger.getLogger(JSONHelper.class.getName()).log(Level.SEVERE, null, ex);
    }
    HttpEntity responseEntity = response.getEntity();

    try {
        return readJsonFromInputStream(responseEntity.getContent());
    } catch (Exception ex) {
        return new TelegramRequestResult<>(false, ex.toString(), null);
    }

}