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.hackathon.gavin.string.parser.MyAccountParser.java

public static JSONArray httpGetCall(String urlString, String jsonArrayName) {
    JSONArray result = null;/* w  w w  .j  av  a 2  s  .  c o m*/
    HttpClient client = HttpClientBuilder.create().build();
    HttpGet request = new HttpGet(urlString);

    try {
        HttpResponse getResponse = client.execute(request);
        BufferedReader reader = new BufferedReader(
                new InputStreamReader(getResponse.getEntity().getContent(), "UTF-8"));
        StringBuilder builder = new StringBuilder();
        for (String line = null; (line = reader.readLine()) != null;) {
            builder.append(line).append("\n");
        }
        //result = new JSONObject(builder.toString()).getJSONArray(jsonArrayName);
        JSONObject obj = new JSONObject(builder.toString());
        result = obj.getJSONArray(jsonArrayName);
    } catch (Exception e) {
        System.out.println(e.toString());
    }
    return result;
}

From source file:com.fishcart.delivery.util.HttpClient.java

public static String postWhatsapp(String url, String nos, String message) {
    try {/*  w  w  w .  j a  va  2s  .  c o m*/
        CloseableHttpClient client = HttpClientBuilder.create().build();
        HttpPost post = new HttpPost(url);
        List<NameValuePair> urlParameters = new ArrayList<NameValuePair>();
        urlParameters.add(new BasicNameValuePair("nos", nos));
        urlParameters.add(new BasicNameValuePair("message", message));
        post.setEntity(new UrlEncodedFormEntity(urlParameters));
        HttpResponse response = client.execute(post);
        BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

        StringBuilder result = new StringBuilder();
        String line = "";
        while ((line = rd.readLine()) != null) {
            result.append(line);
        }
        return result.toString();
    } catch (IOException ex) {
        throw new RuntimeException(ex);
    }

}

From source file:professionalpractice3.GETClient.java

static String updateStocks(String customerID, String stockSymbol, int shares, String price)
        throws ClientProtocolException, IOException {
    BufferedReader responseBody = null;
    HttpClient client = HttpClientBuilder.create().build();

    try {//from   w  ww .j a va 2 s . c om
        //Define a HttpGet request
        HttpGet request = new HttpGet("http://" + localServer);

        //Set Http Headers
        request.addHeader("Accept", "application/xml");
        request.addHeader("Type", "update stocks");
        request.addHeader("Customer", customerID);
        request.addHeader("Symbol", stockSymbol);
        request.addHeader("Shares", Integer.toString(shares));
        request.addHeader("Price", price);

        //Invoke the service
        HttpResponse response = client.execute(request);

        //Verify if the response is valid
        int statusCode = response.getStatusLine().getStatusCode();
        //System.out.println(statusCode);
        if (statusCode != 200) {
            throw new RuntimeException("Failed with HTTP error code .... : " + statusCode);
        } else {
            //If valid, get the response
            responseBody = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
            String line = responseBody.readLine();
            return line;
        }
    } catch (Exception e) {
        e.printStackTrace();
        System.out.println("Invalid input");
        return "";
    } finally {
        if (responseBody != null)
            responseBody.close();
    }
}

From source file:org.dasein.cloud.qingcloud.util.requester.QingCloudHttpClientBuilderFactory.java

static public HttpClientBuilder newHttpClientBuilder() {
    HttpClientBuilder builder = HttpClientBuilder.create();
    builder.setUserAgent("Dasein Cloud");
    //HttpProtocolParams.setContentCharset(params, Consts.UTF_8.toString());
    return builder;
}

From source file:net.infstudio.inflauncher.game.downloading.URLConnectionTimer.java

public static long testTime(String url) {
    try {//  w  w  w  .  j a  va  2 s .  co  m
        HttpClient client = HttpClientBuilder.create().build();
        HttpUriRequest request = RequestBuilder.get(url).build();
        long start = System.currentTimeMillis();
        client.execute(request);
        long end = System.currentTimeMillis();
        return end - start;
    } catch (IOException e) {
        InfinityDownloader.logger.error(e);
        return Long.MAX_VALUE;
    }
}

From source file:net.lukecollins.dev.github.HttpUtils.java

/**
 * Get Response for Service.//from  w  w w .j a v a2 s.c o m
 * @param service servicename
 * @return returns HttpResponse of Http GET
 */
public static HttpResponse getRequest(String service) {
    HttpClient httpClient = HttpClientBuilder.create().build();
    HttpGet httpGet = new HttpGet(GhatConstantsUtil.URL + service);

    //Add token to header
    String token = GhatConstantsUtil.APIKEY + ":x-oauth-basic";
    String authString = null;
    try {
        authString = "Basic " + Base64.encodeBase64String(token.getBytes("UTF-8"));
    } catch (UnsupportedEncodingException exp) {
        log.error(LOG_CONTEXT, exp);
    }
    httpGet.addHeader("Authorization", authString);

    HttpResponse response = null;
    try {
        response = httpClient.execute(httpGet);
    } catch (IOException exp) {
        log.error(LOG_CONTEXT, exp);
    }

    return response;
}

From source file:com.twitter.heron.integration_test.core.HttpUtils.java

static int httpJsonPost(String newHttpPostUrl, String jsonData) throws IOException, ParseException {
    HttpClient client = HttpClientBuilder.create().build();
    HttpPost post = new HttpPost(newHttpPostUrl);

    StringEntity requestEntity = new StringEntity(jsonData, ContentType.APPLICATION_JSON);

    post.setEntity(requestEntity);/*from w  w  w.  j  a v  a 2 s  . com*/
    HttpResponse response = client.execute(post);

    return response.getStatusLine().getStatusCode();
}

From source file:org.roda.core.util.RESTClientUtility.java

public static <T extends Serializable> T sendPostRequest(T element, Class<T> elementClass, String url,
        String resource, String username, String password) throws RODAException {
    CloseableHttpClient httpClient = HttpClientBuilder.create().build();
    String basicAuthToken = new String(Base64.encode((username + ":" + password).getBytes()));
    HttpPost httpPost = new HttpPost(url + resource);
    httpPost.setHeader("Authorization", "Basic " + basicAuthToken);
    httpPost.addHeader("content-type", "application/json");
    httpPost.addHeader("Accept", "application/json");

    try {/*from  w w w .ja  v  a2  s.c om*/
        httpPost.setEntity(new StringEntity(JsonUtils.getJsonFromObject(element)));
        HttpResponse response;
        response = httpClient.execute(httpPost);
        HttpEntity responseEntity = response.getEntity();

        int responseStatusCode = response.getStatusLine().getStatusCode();
        if (responseStatusCode == 201) {
            return JsonUtils.getObjectFromJson(responseEntity.getContent(), elementClass);
        } else {
            throw new RODAException("POST request response status code: " + responseStatusCode);
        }
    } catch (IOException e) {
        throw new RODAException("Error sending POST request", e);
    }
}

From source file:com.e2.MessageSender.java

public static void send(String text) {
    Map<String, String> json = new HashMap<String, String>();
    int second = Integer.parseInt(String.valueOf(System.currentTimeMillis()).substring(9, 10));
    try {/*  w  ww  .  jav  a 2 s .c  o m*/
        String url = "http://taku.st-sweet.com:8082/e_000/hosts";

        HttpClient client = HttpClientBuilder.create().build();
        HttpPost httpPost = new HttpPost(url);

        //Create Message Object
        json.put("mmCall", "1");
        json.put("mType", "5");
        json.put("id", "tw_" + String.valueOf(System.currentTimeMillis()));
        json.put("x", "50");
        json.put("y", second % 2 == 0 ? "5" : "95");
        json.put("appearFade", "true");
        json.put("appearSlide", "2");
        json.put("appearSize", "1");

        json.put("disappearFade", "true");
        json.put("disappearSlide", "3");
        json.put("disappearSize", "2");

        json.put("liveTime", "150");

        json.put("rectColor", colorPattern[second / 2]);
        json.put("text", text);
        json.put("textColor", "#DDD");
        json.put("textAlign", "2");

        List<NameValuePair> nvps = new ArrayList<>();
        for (Iterator it = json.entrySet().iterator(); it.hasNext();) {
            Map.Entry entry = (Map.Entry<String, String>) it.next();
            nvps.add(new BasicNameValuePair(entry.getKey().toString(), entry.getValue().toString()));
        }
        //    System.out.println(new UrlEncodedFormEntity(nvps).toString());

        httpPost.setEntity(new UrlEncodedFormEntity(nvps, "UTF-8"));
        client.execute(httpPost);

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

From source file:net.locosoft.fold.neo4j.internal.Neo4jRestUtil.java

public static JsonObject doGetJson(String uri) {
    try (CloseableHttpClient httpClient = HttpClientBuilder.create().build()) {
        CloseableHttpResponse response = httpClient.execute(new HttpGet(uri));

        String bodyText = EntityUtils.toString(response.getEntity());
        JsonObject jsonObject = JsonObject.readFrom(bodyText);
        return jsonObject;
    } catch (Exception ex) {
        ex.printStackTrace();//ww w  .j ava  2 s  . c om
    }
    return null;
}