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.esri.geoportal.commons.ags.client.AgsClient.java

/**
 * Creates instance of the client./*  w w w  .  j a v a2  s . c  o m*/
 *
 * @param rootUrl "arcgis/rest" root URL
 */
public AgsClient(URL rootUrl) {
    this.rootUrl = adjustUrl(rootUrl);
    this.httpClient = HttpClients.createDefault();
}

From source file:org.wildfly.test.extras.jolokia.SmokeTest.java

@Test
@RunAsClient//from w w  w. ja v  a2s  .c om
public void testVersion() throws Exception {
    try (CloseableHttpClient client = HttpClients.createDefault()) {
        HttpUriRequest request = new HttpGet("http://localhost:8778/jolokia/version");
        try (CloseableHttpResponse response = client.execute(request)) {
            Assert.assertEquals(200, response.getStatusLine().getStatusCode());

            JSONParser parser = new JSONParser();
            JSONObject result = (JSONObject) parser
                    .parse(new InputStreamReader(response.getEntity().getContent()));
            Assert.assertNotNull(result);
            Assert.assertTrue(Number.class.cast(result.get("timestamp")).longValue() > 0);
        }
    }
}

From source file:au.org.ncallister.goodbudget.tools.coms.GoodBudgetSession.java

public void login(String username, String password) throws IOException {
    if (client == null) {
        client = HttpClients.createDefault();
    }/*from   w  w w  .j  a va2 s.  co  m*/

    HttpPost post = new HttpPost(BASE_URI.resolve(PATH_LOGIN));
    //      post.addHeader("Content-Type", "application/x-www-form-urlencoded");
    List<NameValuePair> postContent = new ArrayList<>();
    postContent.add(new BasicNameValuePair("_username", username));
    postContent.add(new BasicNameValuePair("_password", password));
    post.setEntity(new UrlEncodedFormEntity(postContent));

    CloseableHttpResponse response = client.execute(post, sessionContext);
    response.close();
}

From source file:org.jwifisd.httpclient.HttpBasedCard.java

/**
 * @return lazily created http client to connect to the card.
 *///  ww w  .  j  av a 2  s  .c o  m
public CloseableHttpClient getHttpClient() {
    if (httpclient == null) {
        httpclient = HttpClients.createDefault();
    }
    return httpclient;
}

From source file:cn.org.once.cstack.service.impl.MonitoringServiceImpl.java

@Override
public String getJsonFromCAdvisor(String containerId) {
    String result = "";
    try {/* w  ww . j a va  2s  . c  om*/
        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpGet httpget = new HttpGet(cAdvisorURL + "/api/v1.3/containers/docker/" + containerId);
        CloseableHttpResponse response = httpclient.execute(httpget);
        try {
            result = EntityUtils.toString(response.getEntity());
            if (logger.isDebugEnabled()) {
                logger.debug(result);
            }
        } finally {
            response.close();
        }
    } catch (Exception e) {
        logger.error(containerId, e);
    }
    return result;
}

From source file:vazkii.psi.client.core.helper.SharingHelper.java

public static String uploadImage(String title, String export) {
    try {//from  w  w w .j a va  2  s . co m
        String desc = "Spell Code:\n\n" + export;
        HttpClient client = HttpClients.createDefault();

        String url = "https://api.imgur.com/3/image";
        HttpPost post = new HttpPost(url);

        List<NameValuePair> list = new ArrayList();
        list.add(new BasicNameValuePair("type", "base64"));
        list.add(new BasicNameValuePair("image", takeScreenshot()));
        list.add(new BasicNameValuePair("name", title));
        list.add(new BasicNameValuePair("description", desc));

        post.setEntity(new UrlEncodedFormEntity(list));
        post.addHeader("Authorization", "Client-ID " + CLIENT_ID);

        HttpResponse res = client.execute(post);
        JsonObject resJson = new JsonParser().parse(EntityUtils.toString(res.getEntity())).getAsJsonObject();
        if (resJson.has("success") && resJson.get("success").getAsBoolean()) {
            JsonObject data = resJson.get("data").getAsJsonObject();
            String id = data.get("id").getAsString();

            return "https://imgur.com/" + id;
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

    return "N/A";
}

From source file:org.wuspba.ctams.ui.server.ServerUtils.java

public static String get(URI uri) throws IOException {
    CloseableHttpClient httpclient = HttpClients.createDefault();
    HttpGet httpGet = new HttpGet(uri);

    String ret;//from w w  w.java 2  s .  c om

    try (CloseableHttpResponse response = httpclient.execute(httpGet)) {
        HttpEntity entity = response.getEntity();

        ret = ServerUtils.convertEntity(entity);

        EntityUtils.consume(entity);
    }

    return ret;
}

From source file:gobblin.admin.AdminWebServerTest.java

@Test
public void testGetSettingsJs() throws IOException {
    CloseableHttpClient client = HttpClients.createDefault();
    HttpGet getReq = new HttpGet(String.format("http://localhost:%s/js/settings.js", this.portNumber));

    try (CloseableHttpResponse response = client.execute(getReq)) {
        assertEquals(200, response.getStatusLine().getStatusCode());
        HttpEntity body = response.getEntity();
        String bodyString = EntityUtils.toString(body);
        assertStringContains("http://foobar", bodyString);
        assertStringContains("3333", bodyString);
    }/*from  ww  w .ja  v a  2s.  c  o  m*/
}

From source file:com.zazuko.blv.outbreak.tools.SparqlClient.java

List<Map<String, RDFTerm>> queryResultSet(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);

    try {//from ww w. j  av  a  2  s .c o m
        HttpEntity entity2 = response2.getEntity();
        InputStream in = entity2.getContent();
        SAXParserFactory spf = SAXParserFactory.newInstance();
        spf.setNamespaceAware(true);
        SAXParser saxParser = spf.newSAXParser();
        XMLReader xmlReader = saxParser.getXMLReader();
        final SparqlsResultsHandler sparqlsResultsHandler = new SparqlsResultsHandler();
        xmlReader.setContentHandler(sparqlsResultsHandler);
        xmlReader.parse(new InputSource(in));
        /*
         for (int ch = in.read(); ch != -1; ch = in.read()) {
         System.out.print((char)ch);
         }
         */
        // do something useful with the response body
        // and ensure it is fully consumed
        EntityUtils.consume(entity2);
        return sparqlsResultsHandler.getResults();
    } catch (ParserConfigurationException ex) {
        throw new RuntimeException(ex);
    } catch (SAXException ex) {
        throw new RuntimeException(ex);
    } finally {
        response2.close();
    }

}

From source file:com.vmware.identity.rest.core.client.BaseClient.java

/**
 * Constructs a client with a {@code host} and {@code port}.
 * This will use the default {@link HttpClient} and a {@link SimpleHostRetriever}.
 *
 * @see HttpClients#createDefault()//from  w ww.j  a v a  2s  . com
 * @param host the hostname of the remote REST server.
 * @param port a flag indicating whether the connection is secure or not.
 */
protected BaseClient(String host, int port) {
    this(new SimpleHostRetriever(host, port), HttpClients.createDefault());
}