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:org.rapidoid.http.HTTP.java

public static CloseableHttpClient client(String uri) {
    return HttpClientBuilder.create().build();
}

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;//from   ww w. ja v  a2s  . c o  m
    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:util.Slack.java

private static void sendMsg(StringEntity jsonMsg, String url) throws IOException {
    HttpClient httpClient = HttpClientBuilder.create().build(); //Use this instead 

    try {/*from w  ww . jav  a 2 s. c  om*/
        HttpPost request = new HttpPost(url);
        request.addHeader("content-type", "application/json; charset=UTF-8");
        request.setEntity(jsonMsg);
        HttpResponse response = httpClient.execute(request);
        System.out.println(response);
    } finally {
        httpClient.getConnectionManager().shutdown(); //Deprecated
    }
}

From source file:org.ops4j.pax.url.mvn.internal.HttpClients.java

public static CloseableHttpClient createClient(PropertyResolver resolver, String pid) {
    return HttpClientBuilder.create() //
            .useSystemProperties() //
            .disableConnectionState() //
            .setConnectionManager(createConnManager(resolver, pid)) //
            .setRetryHandler(createRetryHandler(resolver, pid)).build();
}

From source file:com.lego.proxy.client.RestProxy.java

public RestProxy() {
    client = HttpClientBuilder.create().build();
}

From source file:org.llorllale.youtrack.api.http.Client.java

@Override
public HttpClientBuilder get() {
    return HttpClientBuilder.create();
}

From source file:com.yoelnunez.mobilefirst.analytics.AnalyticsAPI.java

public static AnalyticsAPI createInstance(AppContext context) throws MissingServerContextException {
    if (serverContext == null) {
        throw new MissingServerContextException("Server context missing, analytics reporting will fail.");
    }/*w ww  .j  a  v  a 2  s . co  m*/

    if (httpClient == null) {
        HttpClientBuilder builder = HttpClientBuilder.create();

        httpClient = builder.build();
    }

    logs = new HashMap<JSONObject, JSONArray>();

    return new AnalyticsAPI(context);
}

From source file:com.mvn.aircraft.ApiTest.java

@Test(enabled = false)
public void testStatusCode() throws ClientProtocolException, IOException {

    HttpUriRequest request = new HttpGet("http://riiwards.com?login=bassan");

    HttpResponse httpResponse = (HttpResponse) HttpClientBuilder.create().build().execute(request);

    System.out.println("BOX: " + httpResponse.getStatus());

    Assert.assertEquals(httpResponse.getStatus(), HttpStatus.SC_OK);
    //    Assert.assertEquals("200","200");
}

From source file:utils.HttpClientGenerator.java

public static CloseableHttpClient getHttpClient(boolean checkCert) {

    if (checkCert == false) {
        HttpClientBuilder b = HttpClientBuilder.create();

        // setup a Trust Strategy that allows all certificates.
        SSLContext sslContext = null;
        try {//from   ww w .ja  va  2  s.c  om
            sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {
                public boolean isTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
                    return true;
                }
            }).build();
        } catch (NoSuchAlgorithmException e) {
            String err = "error occurred while creating SSL disables hhtp client";
        } catch (KeyManagementException e) {
            e.printStackTrace();
        } catch (KeyStoreException e) {
            e.printStackTrace();
        }
        b.setSslcontext(sslContext);

        // not to check Hostnames
        HostnameVerifier hostnameVerifier = SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER;

        //       create an SSL Socket Factory, to use weakened "trust strategy";
        //       and create a Registry, to register it.
        SSLConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(sslContext,
                (X509HostnameVerifier) hostnameVerifier);
        Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder
                .<ConnectionSocketFactory>create()
                .register("http", PlainConnectionSocketFactory.getSocketFactory())
                .register("https", sslSocketFactory).build();

        // creating connection-manager using our Registry.
        //      -- allows multi-threaded use
        PoolingHttpClientConnectionManager connMgr = new PoolingHttpClientConnectionManager(
                socketFactoryRegistry);
        connMgr.setDefaultMaxPerRoute(20);
        // Increase max connections for localhost:80 to 50
        HttpHost localhost = new HttpHost("localhost", 9443);
        connMgr.setMaxPerRoute(new HttpRoute(localhost), 10);
        b.setConnectionManager(connMgr);

        // finally, build the HttpClient;
        CloseableHttpClient client = b.build();
        return client;
    } else {
        PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
        // Increase default max connection per route to 20
        cm.setDefaultMaxPerRoute(20);
        // Increase max connections for localhost:80 to 50
        HttpHost localhost = new HttpHost("localhost", 9443);
        cm.setMaxPerRoute(new HttpRoute(localhost), 10);
        CloseableHttpClient client = HttpClients.custom().setConnectionManager(cm).build();
        return client;
    }
}

From source file:com.verizon.ExportDataToServer.java

public void sendDataToRESTService(String data) {

    try {/*from   w  w  w.  ja  v a  2  s .c  o  m*/
        String url = "http://localhost:8080";

        HttpClient client = HttpClientBuilder.create().build();
        HttpPost post = new HttpPost(url);
        List<NameValuePair> urlParameters = new ArrayList<NameValuePair>();
        urlParameters.add(new BasicNameValuePair("data", data));

        HttpEntity stringEnt = new StringEntity(data);
        post.setEntity(stringEnt);

        HttpResponse response = client.execute(post);
        System.out.println("Response Code : " + response.getStatusLine().getStatusCode());
    } catch (IOException ex) {
        System.out.println("Unable to send data to streaming service..");
    }

}