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.vsct.dt.strowgr.admin.template.locator.UriTemplateLocator.java

public UriTemplateLocator() {
    this.client = HttpClients.createDefault();
}

From source file:com.sample.RSSAdapterResource.java

public static void init() {
    client = HttpClients.createDefault();
    host = new HttpHost("rss.cnn.com");
}

From source file:FlowPusher.ApacheHttpClient.java

public void postFloodlightClient(String jsonBody) throws UnsupportedEncodingException, IOException {

    try {// w ww .jav  a2 s . c om

        CloseableHttpClient httpClient = HttpClients.createDefault();

        URI uri = new URIBuilder().setScheme("http").setHost(controllerIP + ":8080")
                .setPath("/wm/staticflowentrypusher/json").build();

        HttpPost httpPost = new HttpPost(uri);

        StringEntity params = new StringEntity(jsonBody);
        httpPost.addHeader("content-type", "application/json");
        httpPost.setEntity(params);
        HttpResponse response = httpClient.execute(httpPost);

        String x = "kostas";
    } catch (URISyntaxException ex) {
        Logger.getLogger(ApacheHttpClient.class.getName()).log(Level.SEVERE, null, ex);
    }

}

From source file:com.macrossx.wechat.http.WechatHttpClient.java

public String send(final HttpUriRequest request) {
    String result = "";
    try {//  w  w w .ja  v  a  2 s  .co  m
        CloseableHttpResponse response = HttpClients.createDefault().execute(request);
        int statusCode = response.getStatusLine().getStatusCode();
        if (HttpStatus.SC_OK != statusCode) {
            log.info("Failed to get!");
            return "";
        }
        HttpEntity entity = response.getEntity();
        result = EntityUtils.toString(entity, "utf-8");
    } catch (IOException e) {
        log.info(e.getMessage());
    }
    return result;
}

From source file:com.kingmed.dp.ndp.impl.SignOutResponseHandlerTest.java

@Test
public void testSignOut() {
    for (NDPServe ndpServe : allNDPServes) {
        String signinUrl = ndpServe.getUrlSignin();
        String signOutUrl = ndpServe.getUrlSignout();
        CloseableHttpClient httpclient = HttpClients.createDefault();
        NDPServeResponseHandler responeHandler = new SignInResponseHandler();
        String cookie = null;//w  w w .j  av a 2s.  co  m
        try {
            HttpGet httpget = new HttpGet(signinUrl);
            httpclient.execute(httpget, responeHandler);
            cookie = responeHandler.getCookie();
            Assert.notNull(cookie);
        } catch (Exception e) {
            e.printStackTrace();
            fail("?");
        } finally {
            try {
                httpclient.close();
            } catch (IOException ex) {
                fail("");
            }
        }

        Header header = new BasicHeader("Cookie", cookie);
        httpclient = HttpClients.createDefault();
        responeHandler = new SignOutResponseHandler();
        try {
            HttpGet httpget = new HttpGet(signOutUrl);
            httpget.setHeader(header.getName(), header.getValue());
            String status = httpclient.execute(httpget, responeHandler);
            assertTrue("?", NDPServeImpl.STATUS_SUCCEEDED.equals(status));
        } catch (Exception e) {
            e.printStackTrace();
            fail("");
        } finally {
            try {
                httpclient.close();
            } catch (IOException ex) {
                fail("");
            }
        }

    }
}

From source file:gertjvr.slacknotifier.SlackApiProcessor.java

public void sendNotification(String url, SlackNotificationMessage notification) throws IOException {
    String content = new Gson().toJson(notification);
    logger.debug(String.format("sendNotification: %s", content));

    CloseableHttpClient httpClient = HttpClients.createDefault();
    HttpPost httpPost = new HttpPost(url);

    httpPost.setEntity(new StringEntity(content, ContentType.APPLICATION_JSON));
    CloseableHttpResponse response = httpClient.execute(httpPost);

    try {//from  w  w  w  .ja v  a  2  s  .  co  m
        HttpEntity entity = response.getEntity();
        EntityUtils.consume(entity);
    } catch (Exception ex) {
        logger.error(String.format("sendNotification %s", ex.getMessage()), ex);
    } finally {
        response.close();
    }
}

From source file:com.kingmed.dp.ndp.impl.UpdateLinkedFolderResponseHandlerTest.java

@Test
public void testUpdateLinkedFolder() {
    for (NDPServe ndpServe : allNDPServes) {
        String signinUrl = ndpServe.getUrlSignin();
        CloseableHttpClient httpclient = HttpClients.createDefault();
        NDPServeResponseHandler responeHandler = new SignInResponseHandler();
        String cookie = null;//from w  ww  .  jav a 2 s .  c  om
        try {
            HttpGet httpget = new HttpGet(signinUrl);
            httpclient.execute(httpget, responeHandler);
            cookie = responeHandler.getCookie();
            Assert.notNull(cookie);
        } catch (Exception e) {
            e.printStackTrace();
            fail("?");
        } finally {
            try {
                httpclient.close();
            } catch (IOException ex) {
                fail("");
            }
        }

        String uri = ndpServe.getUrlForUpdateLinkedFolders();
        httpclient = HttpClients.createDefault();
        responeHandler = new UpdateLinkedFoldersResponseHandler();

        try {
            HttpGet httpget = new HttpGet(uri);
            httpget.setHeader("Cookie", cookie);
            String status = httpclient.execute(httpget, responeHandler);
            System.out.println("status==================" + status);
            assertTrue("?succeed", status.equals(NDPServeImpl.STATUS_SUCCEEDED));
        } catch (Exception e) {
            e.printStackTrace();
            fail("");
        } finally {
            try {
                httpclient.close();
            } catch (IOException ex) {
                fail("");
            }
        }

    }
}

From source file:org.wso2.carbon.device.mgt.iot.input.adapter.mqtt.util.MQTTUtil.java

/**
 * Return a http client instance//from   w w w  .  j a  v a2 s.  c o m
 *
 * @param protocol- service endpoint protocol http/https
 * @return
 */
public static HttpClient getHttpClient(String protocol)
        throws IOException, KeyStoreException, NoSuchAlgorithmException, KeyManagementException {
    HttpClient httpclient;
    if (HTTPS_PROTOCOL.equals(protocol)) {
        SSLContextBuilder builder = new SSLContextBuilder();
        builder.loadTrustMaterial(null, new TrustSelfSignedStrategy());
        SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(builder.build());
        httpclient = HttpClients.custom().setSSLSocketFactory(sslsf).build();
    } else {
        httpclient = HttpClients.createDefault();
    }
    return httpclient;
}

From source file:retsys.client.helper.LovHandler.java

public String getSuggestions(String filterOn) {
    String response = null;//from w ww  .  j  av a 2 s.co m
    HttpHelper helper = new HttpHelper();
    StringBuilder queryString = new StringBuilder(entity);
    try {
        if (conditionFld != null && filterOn != null) {
            queryString.append("/" + conditionFld + "/" + filterOn);
        }

        if (startPosition > 0 && maxResults > 0) {
            queryString.append("?start=" + startPosition + "&max=" + maxResults);
        }

        response = helper.executeHttpRequest(HttpClients.createDefault(),
                helper.getHttpGetObj(queryString.toString()));
    } catch (IOException ex) {
        Logger.getLogger(LovHandler.class.getName()).log(Level.SEVERE, null, ex);
    }

    return response;
}

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

public static void sendPut(String serverURL, String postBody) throws Exception {

    CloseableHttpClient client = HttpClients.createDefault();
    HttpPut post = new HttpPut(serverURL);

    StringEntity entity = new StringEntity(postBody, "UTF-8");

    post.setEntity(entity);// w  w  w  .  j a  v a2  s  .c om

    HttpResponse response = client.execute(post);
    // System.out.println("\nSending 'PUT' request to URL : " + serverURL);

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

    StringBuffer result = new StringBuffer();
    String line = "";
    while ((line = rd.readLine()) != null) {
        result.append(line);
    }
    client.close();
    // System.out.println(result.toString());
}