Example usage for org.apache.http.impl.client HttpClientBuilder create

List of usage examples for org.apache.http.impl.client HttpClientBuilder create

Introduction

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

Prototype

public static HttpClientBuilder create() 

Source Link

Usage

From source file:com.betfair.testing.utils.cougar.misc.PageManagerImpl.java

public PageManagerImpl() {
    httpClient = HttpClientBuilder.create().setConnectionManager(new BasicHttpClientConnectionManager())
            .build();
}

From source file:com.github.tomakehurst.wiremock.http.HttpClientFactory.java

public static CloseableHttpClient createClient(int maxConnections, int timeoutMilliseconds,
        ProxySettings proxySettings, KeyStoreSettings trustStoreSettings) {

    HttpClientBuilder builder = HttpClientBuilder.create().disableAuthCaching().disableAutomaticRetries()
            .disableCookieManagement().disableRedirectHandling().disableContentCompression()
            .setMaxConnTotal(maxConnections)
            .setDefaultSocketConfig(SocketConfig.custom().setSoTimeout(timeoutMilliseconds).build())
            .useSystemProperties().setHostnameVerifier(new AllowAllHostnameVerifier());

    if (proxySettings != NO_PROXY) {
        HttpHost proxyHost = new HttpHost(proxySettings.host(), proxySettings.port());
        builder.setProxy(proxyHost);/*from   w  w w.  j a v a2s. c  o m*/
    }

    if (trustStoreSettings != NO_STORE) {
        builder.setSslcontext(buildSSLContextWithTrustStore(trustStoreSettings));
    } else {
        builder.setSslcontext(buildAllowAnythingSSLContext());
    }

    return builder.build();
}

From source file:com.vmware.photon.controller.model.adapters.vsphere.vapi.VapiConnection.java

public static HttpClient newUnsecureClient() {
    return HttpClientBuilder.create().setSslcontext(IgnoreSslErrors.newInsecureSslContext("TLS"))
            .setHostnameVerifier(new AllowAllHostnameVerifier()).build();
}

From source file:AIR.Dictionary.DictionaryConnection.java

public String lookup(String word) {

    try (CloseableHttpClient httpClient = HttpClientBuilder.create().build()) {
        //        // make word url safe
        word = UrlEncoderDecoderUtils.encode(word);

        // create url to send
        String baseUrl = _apiUrl + word;
        URI restAPIURI = new URI(baseUrl + "?key=" + _apiKey);
        // create client
        HttpGet request = new HttpGet(restAPIURI);
        request.setHeader("Accept-Charset", "UTF-8");
        HttpResponse response = httpClient.execute(request);

        String responseXML = EntityUtils.toString(response.getEntity(), "UTF-8");
        return responseXML;

    } catch (Exception e) {
        _logger.error("Error Calling Dictionary rest API ", e);
    }//from  ww  w  .j  a  v a2 s  .  c  o m
    return null;
}

From source file:com.mycompany.horus.ServiceListener.java

private String getWsdlServicos() throws IOException {
    String resp;/*from w w  w. ja v  a 2s .co  m*/
    CloseableHttpClient httpclient = HttpClientBuilder.create().build();
    HttpPost post = new HttpPost(SERVICE_URL);
    HttpResponse response = httpclient.execute(post);
    HttpEntity respEntity = response.getEntity();
    resp = EntityUtils.toString(respEntity);
    return resp;
}

From source file:DefinitionObject.java

/*** Calls for server output ***/

public void fetch_output(String word) {
    entry = word;//ww  w .ja  v a  2s  .c o  m
    System.out.print("----------------\n" + "Word to be defined:\t " + word + "\n----------------");

    try {

        HttpClient httpClient = HttpClientBuilder.create().build();
        HttpGet getRequest = new HttpGet(
                "https://api.pearson.com:443/v2/dictionaries/entries?headword=" + word); //Here is word!
        getRequest.addHeader("accept", "application/json");

        HttpResponse response = httpClient.execute(getRequest);

        if (response.getStatusLine().getStatusCode() != 200) {
            throw new RuntimeException(
                    "Failed : HTTP error code : " + response.getStatusLine().getStatusCode());
        }

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

        String output;
        System.out.println("\nOutput from Server .... \n");
        while ((output = br.readLine()) != null) {
            System.out.println(output);
            serverOutput = output;
        }

        httpClient.getConnectionManager().shutdown();
    } catch (ClientProtocolException e) {

        e.printStackTrace();

    } catch (IOException e) {

        e.printStackTrace();
    }

}

From source file:ar.edu.ubp.das.src.chat.actions.EliminarMensajeAction.java

@Override
public ForwardConfig execute(ActionMapping mapping, DynaActionForm form, HttpServletRequest request,
        HttpServletResponse response) throws SQLException, RuntimeException {
    try (CloseableHttpClient httpClient = HttpClientBuilder.create().build()) {

        //get request data
        String id_mensaje = form.getItem("id_mensaje");
        String authToken = String.valueOf(request.getSession().getAttribute("token"));

        URIBuilder builder = new URIBuilder();
        builder.setScheme("http").setHost("25.136.78.82").setPort(8080).setPath("/mensajes/" + id_mensaje);

        HttpDelete delete = new HttpDelete();
        delete.setURI(builder.build());/*ww w  . j  av a  2s .co m*/
        delete.addHeader("Authorization", "BEARER " + authToken);
        delete.addHeader("accept", "application/json");

        CloseableHttpResponse deleteResponse = httpClient.execute(delete);

        HttpEntity responseEntity = deleteResponse.getEntity();
        StatusLine responseStatus = deleteResponse.getStatusLine();
        String restResp = EntityUtils.toString(responseEntity);

        if (responseStatus.getStatusCode() != 200) {
            throw new RuntimeException(restResp);
        }

        return mapping.getForwardByName("success");

    } catch (IOException | URISyntaxException | RuntimeException e) {
        String id_mensaje = form.getItem("id_mensaje");
        request.setAttribute("message",
                "Error al intentar eliminar mensaje " + id_mensaje + "; " + e.getMessage());
        response.setStatus(400);
        return mapping.getForwardByName("failure");
    }
}

From source file:ar.edu.ubp.das.src.chat.actions.ExpulsarUsuarioAction.java

@Override
public ForwardConfig execute(ActionMapping mapping, DynaActionForm form, HttpServletRequest request,
        HttpServletResponse response) throws SQLException, RuntimeException {
    try (CloseableHttpClient httpClient = HttpClientBuilder.create().build()) {

        //get request data
        String id_usuario = form.getItem("id_usuario");
        String id_sala = form.getItem("id_sala");
        String authToken = String.valueOf(request.getSession().getAttribute("token"));

        URIBuilder builder = new URIBuilder();
        builder.setScheme("http").setHost("25.136.78.82").setPort(8080)
                .setPath("/usuarios-salas/" + id_usuario + "/" + id_sala);

        HttpDelete delete = new HttpDelete();
        delete.setURI(builder.build());/*w w w  .  j a v a 2s  .com*/
        delete.addHeader("Authorization", "BEARER " + authToken);
        delete.addHeader("accept", "application/json");

        CloseableHttpResponse deleteResponse = httpClient.execute(delete);

        HttpEntity responseEntity = deleteResponse.getEntity();
        StatusLine responseStatus = deleteResponse.getStatusLine();
        String restResp = EntityUtils.toString(responseEntity);

        if (responseStatus.getStatusCode() != 200) {
            throw new RuntimeException(restResp);
        }

        return mapping.getForwardByName("success");

    } catch (IOException | URISyntaxException | RuntimeException e) {
        String id_usuario = form.getItem("id_usuario");
        request.setAttribute("message",
                "Error al intentar expulsar usuario: " + id_usuario + "; " + e.getMessage());
        response.setStatus(400);
        return mapping.getForwardByName("failure");
    }
}

From source file:com.dhenton9000.embedded.jetty.ServerTest.java

@Test
public void testServer() throws IOException {
    HttpClient client = HttpClientBuilder.create().build();
    HttpGet mockRequest = new HttpGet(APP_URL);

    HttpResponse mockResponse = client.execute(mockRequest);
    BufferedReader rd = new BufferedReader(new InputStreamReader(mockResponse.getEntity().getContent()));
    String theString = IOUtils.toString(rd);
    assertNotNull(theString);//from  www .j a  va  2s.  c o m
    LOG.debug(theString);
    assertTrue(theString.contains("Hello World"));
}

From source file:com.jubination.io.chatbot.backend.service.core.DashBotUpdater.java

public String sendAutomatedUpdate(DashBot dashbot, String type) {
    String responseText = "";
    try {//w w w  .  j  av  a  2  s  .  c  o m
        String url = "https://tracker.dashbot.io/track?platform=generic&v=0.8.2-rest&type=" + type
                + "&apiKey=bJt7U0oEG79HSUm4nJWUQTPm1nhjKu3gieZ83M0O";
        ObjectMapper mapper = new ObjectMapper();
        //Object to JSON in String
        String jsonString = mapper.writeValueAsString(dashbot);
        HttpClient httpClient = HttpClientBuilder.create().build();
        // System.out.println(jsonString+"STRING JSON TO DASH BOT");
        StringEntity requestEntity = new StringEntity(jsonString, ContentType.APPLICATION_JSON);
        HttpPost postMethod = new HttpPost(url);
        postMethod.setEntity(requestEntity);
        HttpResponse response = httpClient.execute(postMethod);
        HttpEntity entity = response.getEntity();
        responseText = EntityUtils.toString(entity, "UTF-8");

    } catch (Exception ex) {
        Logger.getLogger(DashBotUpdater.class.getName()).log(Level.SEVERE, null, ex);
    }
    // System.out.println(responseText);
    return responseText;
}