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:apimanager.ZohoReportsAPIManager.java

public void postaddSingleRow() {

    String URL = "https://reportsapi.zoho.com/api/harshavardhan.r@zohocorp.com/Test/Name_password?ZOHO_ACTION=ADDROW&ZOHO_OUTPUT_FORMAT=json&ZOHO_ERROR_FORMAT=json&authtoken=7ed717b94bc30455aad11ce5d31d34f9&ZOHO_API_VERSION=1.0";

    CloseableHttpClient httpclient = HttpClients.createDefault();
    HttpPost post = new HttpPost(
            "https://reportsapi.zoho.com/api/harshavardhan.r@zohocorp.com/Test/Name_password?ZOHO_ACTION=ADDROW&ZOHO_OUTPUT_FORMAT=json&ZOHO_ERROR_FORMAT=json&authtoken=7ed717b94bc30455aad11ce5d31d34f9&ZOHO_API_VERSION=1.0");

    try {//  w w w  .j  a v  a  2s  .  c  o m
        List<NameValuePair> nameValuePairs = new ArrayList<>(2);
        nameValuePairs.add(new BasicNameValuePair("Name", "harshaViaPostnet"));
        nameValuePairs.add(new BasicNameValuePair("Password", "harshaviaPost123net"));
        post.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        HttpResponse response = httpclient.execute(post);
        BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
        String line = "";
        while ((line = rd.readLine()) != null) {
            System.out.println(line);
        }

    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:webrequester.AbstractWebRequest.java

public String requestWithGet() {
    String s = "";
    try {// w  ww.j  a v  a2s  .  c  o  m
        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpGet httpget = new HttpGet(buildURI());
        CloseableHttpResponse response = httpclient.execute(httpget);
        try {
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                s = EntityUtils.toString(entity, "UTF-8");
            }
        } finally {
            response.close();
        }
    } catch (IOException ex) {
        return null;
    }
    return s;
}

From source file:testingproject.TestingProjectDLB.java

static List<String> getDLBResults(String lotteryCode, String date)
        throws UnsupportedEncodingException, IOException { //  16/11/17
    map.put("1", "Makara");
    map.put("2", "Kumba");
    map.put("3", "meena");
    map.put("4", "mesha");
    map.put("5", "wrushabha");
    map.put("6", "mithuna");
    map.put("7", "kataka");
    map.put("8", "sinha");
    map.put("9", "kanya");
    map.put("10", "thula");
    map.put("11", "dhanu");
    map.put("12", "Thula");

    HttpClient httpclient = HttpClients.createDefault();
    HttpPost httppost = new HttpPost(
            "http://dlb.today/dlb/index.php?option=com_jumi&amp;fileid=31&amp;Itemid=31");

    // Request parameters and other properties.
    List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>(2);

    params.add(new BasicNameValuePair("db", lotteryCode));
    params.add(new BasicNameValuePair("dn", ""));
    params.add(new BasicNameValuePair("ename", date));

    httppost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));

    //Execute and get the response.
    HttpResponse response = httpclient.execute(httppost);
    HttpEntity entity = response.getEntity();

    if (entity != null) {
        InputStream instream = entity.getContent();
        try {/*from   w w w  .  j a v a  2  s  .c om*/
            String content = convertStreamToString(instream);
            //System.out.println(content);
            List<String> dataList = getData(content);

            // for (String data : dataList) {
            //   System.out.println(data);
            // }
            return dataList;
        } finally {
            instream.close();
        }
    }
    return null;
}

From source file:com.github.caldav4j.BaseTestCase.java

public static HttpClient createHttpClientWithNoCredentials() {
    return HttpClients.createDefault();
}

From source file:eu.citadel.converter.io.index.CitadelIndexUtil.java

/**
 * Get the list of city allowed by citadel index
 * @param url The url that host the city list
 * @return List of allowed city sorted by name
 * @throws ConverterException/*from  w  w w.j  av a 2s  . c o m*/
 */
public static List<CitadelCityInfo> getCitadelCityInfo(String url) throws ConverterException {
    try {
        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpPost request = new HttpPost(url);
        CloseableHttpResponse response = httpclient.execute(request);
        String jsonResponse = EntityUtils.toString(response.getEntity());
        Gson gson = new GsonBuilder().create();
        if (jsonResponse.length() < 11) {
            log.error("Invalid response from server: " + jsonResponse + ".");
            throw new ConverterException("Invalid response from server: " + jsonResponse + ".");
        }
        jsonResponse = jsonResponse.substring(10, jsonResponse.length() - 1);
        List<CitadelCityInfo> infos = gson.fromJson(jsonResponse, new TypeToken<List<CitadelCityInfo>>() {
        }.getType());
        Collections.sort(infos);
        return infos;
    } catch (ParseException | IOException e) {
        log.error(e.getMessage());
        throw new ConverterException(e.getMessage());
    }
}

From source file:com.infinities.keystone4j.PatchClient.java

public JsonNode connect(Object obj) throws ClientProtocolException, IOException {
    String input = JsonUtils.toJsonWithoutPrettyPrint(obj);
    logger.debug("input: {}", input);
    StringEntity requestEntity = new StringEntity(input, ContentType.create("application/json", Consts.UTF_8));

    CloseableHttpClient httpclient = HttpClients.createDefault();
    try {//from   ww w  . j a  v  a 2  s. co  m
        HttpPatch request = new HttpPatch(url);
        request.addHeader("accept", "application/json");
        request.addHeader("X-Auth-Token", Config.Instance.getOpt(Config.Type.DEFAULT, "admin_token").asText());
        request.setEntity(requestEntity);
        ResponseHandler<JsonNode> rh = new ResponseHandler<JsonNode>() {

            @Override
            public JsonNode handleResponse(final HttpResponse response) throws IOException {
                StatusLine statusLine = response.getStatusLine();
                HttpEntity entity = response.getEntity();
                if (entity == null) {
                    throw new ClientProtocolException("Response contains no content");
                }
                String output = getStringFromInputStream(entity.getContent());
                logger.debug("output: {}", output);
                assertEquals(200, statusLine.getStatusCode());

                JsonNode node = JsonUtils.convertToJsonNode(output);
                return node;
            }

            private String getStringFromInputStream(InputStream is) {

                BufferedReader br = null;
                StringBuilder sb = new StringBuilder();

                String line;
                try {

                    br = new BufferedReader(new InputStreamReader(is));
                    while ((line = br.readLine()) != null) {
                        sb.append(line);
                    }

                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    if (br != null) {
                        try {
                            br.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }

                return sb.toString();

            }
        };
        JsonNode node = httpclient.execute(request, rh);

        return node;
    } finally {
        httpclient.close();
    }
}

From source file:io.djigger.monitoring.java.instrumentation.subscription.HttpClientTracerTest.java

private CloseableHttpResponse callGet() throws Exception {
    CloseableHttpClient client = HttpClients.createDefault();
    CloseableHttpResponse response = null;
    try {/*from ww  w . j  av  a  2 s. co m*/
        HttpGet get = new HttpGet("http://localhost:12298");
        response = client.execute(get);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return response;
}

From source file:com.kingja.springmvc.util.HttpRequestUtils.java

/**
 * ??HttpWeb//  w w w .ja v a 2  s  .co  m
 *
 * @param path Web?
 * @param map Http?
 * @param encode ??
 * @return Web?
 */
public String sendHttpClientPost(String path, Map<String, String> map, String encode) {
    List<NameValuePair> list = new ArrayList<NameValuePair>();
    if (map != null && !map.isEmpty()) {
        for (Map.Entry<String, String> entry : map.entrySet()) {
            //?Map?BasicNameValuePair?
            list.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
        }
    }

    try {
        // ???HttpEntity
        UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list, encode);
        //HttpPost?
        HttpPost httpPost = new HttpPost(path);
        //?Form
        httpPost.setEntity(entity);
        //HttpAndroidHttpClient
        CloseableHttpClient httpclient = HttpClients.createDefault();
        //??
        HttpResponse httpResponse = httpclient.execute(httpPost);
        //??200??
        if (httpResponse.getStatusLine().getStatusCode() == 200) {
            //HttpEntity??
            InputStream inputStream = httpResponse.getEntity().getContent();
            return changeInputStream(inputStream, encode);
        }

    } catch (UnsupportedEncodingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return "";
}

From source file:org.maximachess.jdrone.HipChatNotifier.java

public HipChatNotifier(Router router, String id) {
    this.router = router;
    this.id = id;
    this.router.addListener(this);

    /* initialize mail */
    Config cfg = router.getClient().getConfig();
    if (cfg.getProperty("hipchat.url") != null) {
        roomUrl = cfg.getProperty("hipchat.url");

        httpclient = HttpClients.createDefault();
    }//from   w w w .ja v  a2s .  c om
}

From source file:kr.riotapi.core.ApiDomain.java

public String execute(ApiCall call) throws IOException, StatusCodeException {
    HttpGet get = new HttpGet(call.toUrlString());
    String body = null;//from  w w w. j a  v a2  s . com
    int statusCode = 0;
    try (CloseableHttpClient client = HttpClients.createDefault()) {
        CloseableHttpResponse response = client.execute(get);
        statusCode = response.getStatusLine().getStatusCode();
        body = EntityUtils.toString(response.getEntity());
        response.close();
    } catch (IOException ex) {
        throw new IOException("There was a problem receiving or processing a server response", ex);
    }
    if (statusCode != HttpStatus.SC_OK)
        throw new StatusCodeException("status code was not 200", statusCode);
    return body;
}