Example usage for org.apache.http.entity StringEntity StringEntity

List of usage examples for org.apache.http.entity StringEntity StringEntity

Introduction

In this page you can find the example usage for org.apache.http.entity StringEntity StringEntity.

Prototype

public StringEntity(String str) throws UnsupportedEncodingException 

Source Link

Usage

From source file:org.fcrepo.camel.indexing.solr.integration.TestUtils.java

public static void httpPost(final String url, final String content, final String mimeType) throws Exception {
    final CloseableHttpClient httpClient = HttpClients.createDefault();
    final HttpPost post = new HttpPost(url);
    post.addHeader(Exchange.CONTENT_TYPE, mimeType);
    post.setEntity(new StringEntity(content));
    httpClient.execute(post);/*from  w w  w  .  j  a  v a 2 s.c  o m*/
}

From source file:com.firewallid.util.FIConnection.java

public static void sendJson(String host, String json) throws UnsupportedEncodingException, IOException {
    DefaultHttpClient httpClient = new DefaultHttpClient();
    HttpPost postRequest = new HttpPost(host);

    StringEntity input = new StringEntity(json);
    input.setContentType("application/json");
    postRequest.setEntity(input);//  www  .  j a v a2 s.  com

    HttpResponse response = httpClient.execute(postRequest);
}

From source file:org.openo.nfvo.emsdriver.northbound.client.HttpClientUtil.java

public static String doPost(String url, String json, String charset) {
    CloseableHttpClient httpClient = null;
    HttpPost httpPost = null;//from   w w  w.  j  ava2  s.c o  m
    String result = null;
    try {
        httpClient = HttpClientFactory.getSSLClientFactory();
        httpPost = new HttpPost(url);
        if (null != json) {
            StringEntity s = new StringEntity(json);
            s.setContentEncoding("UTF-8");
            s.setContentType("application/json"); // set contentType
            httpPost.setEntity(s);
        }
        CloseableHttpResponse response = httpClient.execute(httpPost);
        try {
            if (response != null) {
                HttpEntity resEntity = response.getEntity();
                if (resEntity != null) {
                    result = EntityUtils.toString(resEntity, charset);
                }
            }
        } catch (Exception e) {
            log.error("httpClient.execute(httpPost) is fail", e);
        } finally {
            if (response != null) {
                response.close();
            }
        }
    } catch (Exception e) {
        log.error("doPost is fail ", e);
    } finally {
        if (httpClient != null) {
            try {
                httpClient.close();
            } catch (IOException e) {
            }
        }

    }
    return result;
}

From source file:es.ugr.swad.swadroid.webservices.RestEasy.java

public static HttpResponse doPost(String url, JSONObject c) throws ClientProtocolException, IOException {
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost request = new HttpPost(url);
    StringEntity s = new StringEntity(c.toString());
    s.setContentEncoding("UTF-8");
    s.setContentType("application/json");

    request.setEntity(s);/*  w ww.j  av  a2s.c  o m*/
    request.addHeader("accept", "application/json");

    return httpclient.execute(request);
}

From source file:com.ksc.http.apache.utils.ApacheUtils.java

/**
 * Utility function for creating a new StringEntity and wrapping any errors
 * as an AmazonClientException./*  w w w  .  j  a  v  a  2 s  .c  om*/
 *
 * @param s The string contents of the returned HTTP entity.
 * @return A new StringEntity with the specified contents.
 */
public static HttpEntity newStringEntity(String s) {
    try {
        return new StringEntity(s);
    } catch (UnsupportedEncodingException e) {
        throw new KscClientException("Unable to create HTTP entity: " + e.getMessage(), e);
    }
}

From source file:com.world.watch.worldwatchcron.util.PushToUser.java

public static void push(List<String> data, String userId) {
    try {/*from  w  w w. ja va  2  s . co  m*/
        InputStream jsonStream = PushToUser.class.getResourceAsStream("/parsePush.json");
        ObjectMapper mapper = new ObjectMapper();
        PushData push = mapper.readValue(jsonStream, PushData.class);
        push.getWhere().setUsername(userId);
        push.getData().setKeywords(data);
        String json = mapper.writeValueAsString(push);

        HttpClient httpClient = HttpClients.custom().setDefaultRequestConfig(RequestConfig.custom().build())
                .build();
        HttpPost post = new HttpPost(URL);
        post.setHeader("X-Parse-Application-Id", "WhqWj009luOxOtIH3rM9iWJICLdf0NKbgqdaui8Q");
        post.setHeader("X-Parse-REST-API-Key", "lThhKObAz1Tkt092Cl1HeZv4KLUsdATvscOaGN2y");
        post.setHeader("Content-Type", "application/json");
        logger.debug("JSON to push {}", json.toString());
        StringEntity strEntity = new StringEntity(json);
        post.setEntity(strEntity);
        httpClient.execute(post);
        logger.debug("Pushed {} to userId {}", data.toString(), userId);
    } catch (Exception ex) {
        logger.error("Push Failed for {} ", userId, ex);
    }

}

From source file:de.bytefish.fcmjava.client.utils.HttpUtils.java

private static <TRequestMessage> void internalPost(HttpClientBuilder httpClientBuilder,
        IFcmClientSettings settings, TRequestMessage requestMessage) throws Exception {

    try (CloseableHttpClient client = httpClientBuilder.build()) {

        // Initialize a new post Request:
        HttpPost httpPost = new HttpPost(settings.getFcmUrl());

        // Set the JSON String as data:
        httpPost.setEntity(new StringEntity(JsonUtils.getAsJsonString(requestMessage)));

        // Execute the Request:
        try (CloseableHttpResponse response = client.execute(httpPost)) {

            // Get the HttpEntity:
            HttpEntity entity = response.getEntity();

            // Let's be a good citizen and consume the HttpEntity:
            if (entity != null) {

                // Make Sure it is fully consumed:
                EntityUtils.consume(entity);
            }/*from  ww w  .  j av a 2 s.  co m*/
        }
    }
}

From source file:coolmapplugin.util.HTTPRequestUtil.java

public static boolean executePut(String targetURL, String jsonBody) {
    try (CloseableHttpClient httpClient = HttpClientBuilder.create().build()) {

        HttpPut request = new HttpPut(targetURL);

        if (jsonBody != null && !jsonBody.isEmpty()) {
            StringEntity params = new StringEntity(jsonBody);
            request.addHeader("content-type", "application/json");
            request.setEntity(params);/*from   www .  j a  va  2 s. c o  m*/
        }

        HttpResponse result = httpClient.execute(request);

        if (result.getStatusLine().getStatusCode() == 412 || result.getStatusLine().getStatusCode() == 500) {
            return false;
        }

    } catch (IOException e) {
        return false;
    }

    return true;
}

From source file:appserver.grupo5.http.java

public static Response Request(Method method, String url, String content, String contentType) {
    Response response;//ww  w.jav a  2s.  co m
    try {
        HttpClient client = new DefaultHttpClient();
        HttpUriRequest request;
        switch (method) {
        case GET:
            request = new HttpGet(url);
            break;
        case POST:
            request = new HttpPost(url);
            ((HttpPost) request).setEntity(new StringEntity(content));
            break;
        case PUT:
            request = new HttpPut(url);
            ((HttpPut) request).setEntity(new StringEntity(content));
            break;
        case DELETE:
            request = new HttpDeleteWithBody(url);
            ((HttpDeleteWithBody) request).setEntity(new StringEntity(content));
            break;
        default:
            request = new HttpGet(url);
            break;
        }
        if (method != Method.GET) {
            request.setHeader("Content-type", contentType);
        }
        response = executeRequest(client, request);
    } catch (Exception e) {
        response = Response.status(400).entity(e.getMessage()).build();
    }
    return response;
}

From source file:com.continuuity.loom.TestHelper.java

public static SchedulableTask takeTask(String loomUrl, TakeTaskRequest request) throws Exception {
    HttpPost httpPost = new HttpPost(String.format("%s/v1/loom/tasks/take", loomUrl));
    httpPost.setEntity(new StringEntity(GSON.toJson(request)));

    CloseableHttpClient httpClient = HttpClients.createDefault();
    CloseableHttpResponse response = httpClient.execute(httpPost);
    try {//from  w w  w  . j a va 2  s.c o m
        Assert.assertEquals(2, response.getStatusLine().getStatusCode() / 100);
        if (response.getEntity() == null) {
            return null;
        }
        return GSON.fromJson(EntityUtils.toString(response.getEntity()), SchedulableTask.class);
    } finally {
        response.close();
    }
}