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:monitor.console.control.HTTPRequest.java

public HTTPRequest(String login, byte[] password, FindUrl url)
        throws UnsupportedEncodingException, IOException {

    HttpClient client = new DefaultHttpClient();
    System.out.println(url.getHostName() + ":" + url.getPortNumber() + "/" + url.getRestServiceName());
    HttpPost post = new HttpPost(
            url.getHostName() + ":" + url.getPortNumber() + "/" + url.getRestServiceName());
    StringEntity input = new StringEntity(
            "{\"login\":" + "\"" + login + "\",\"password\":\"" + Arrays.toString(password) + "\"}");
    input.setContentType("application/json");
    post.setEntity(input);//from   w  w w .  j  a va 2  s .  c om
    HttpResponse response = client.execute(post);
    BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
    String tmp = "";
    while ((tmp = rd.readLine()) != null) {
        resp = tmp.replaceAll("\"", "");
        System.out.println(resp);
    }
}

From source file:org.stem.api.BaseHttpClient.java

protected static StringEntity prepareJsonBody(Object obj) {
    try {/*from www .  j  av  a2  s.c  o m*/
        return new StringEntity(JsonUtils.encode(obj));
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException(e);
    }
}

From source file:httpscheduler.LateBindingTaskSubmitThread.java

@Override
public void run() {
    StringEntity stringEntity = null;/*  w  ww  .j  ava2  s.com*/
    response.setStatusCode(HttpStatus.SC_OK);
    System.out.println("Received probe response.");

    // send NOOP if task queue empty for specified job
    if (taskQueue.isEmpty()) {
        response.setStatusCode(HttpStatus.SC_OK);
        try {
            stringEntity = new StringEntity("NOOP");
        } catch (UnsupportedEncodingException ex) {
            Logger.getLogger(LateBindingTaskSubmitThread.class.getName()).log(Level.SEVERE, null, ex);
        }
        System.out.println("Responding with NOOP");
    }
    // else send job id, task id and task commmand to worker
    else {
        Task task = (Task) taskQueue.remove();
        try {
            stringEntity = new StringEntity(String.valueOf(task.getDuration()));
        } catch (UnsupportedEncodingException ex) {
            Logger.getLogger(LateBindingTaskSubmitThread.class.getName()).log(Level.SEVERE, null, ex);
        }
        //System.out.println("Responding with task duration: " + task.getDuration());
    }
    response.setEntity(stringEntity);
}

From source file:requestToApi.java

public String addSensor(String sensorName, String sensorType, String deviceId) {
    String output2 = "";
    try {//  w w  w .j  a  v a2  s .  c o m

        StringEntity input = new StringEntity("{\"sensorName\":\"" + sensorName + "\",\"sensorType\":\""
                + sensorType + "\",\"deviceId\":\"" + deviceId + "\"}");
        String URL = "http://smarthomeinterface.azurewebsites.net/addSensor";
        output2 = send(input, URL);

    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return output2;
}

From source file:util.Slack.java

public void sendMsgExecucaoIniciada(String titulo, String texto) {

    try {/*from w  w w.  j  a v a 2s  .c  o m*/
        String str = "{ " + "    \"attachments\": [ " + "        { "
                + "            \"fallback\": \"Monitoria DA\", " + "            \"pretext\": \"Monitoria DA\", "
                + "            \"title\": \"" + titulo + "\", " + "            \"text\": \"" + texto + "\", "
                + "            \"color\": \"#7CD197\" " + "        } " + "    ] " + "}";
        System.out.println(str);
        sendMsg(new StringEntity(converteCaracteresEspeciais(str)));
    } catch (IOException ex) {
        new Msg().msgErro(ex.getMessage());
    }
}

From source file:org.wso2.carbon.integration.test.client.HttpEventPublisherClient.java

public static void publish(String url, String username, String password, String testCaseFolderName,
        String dataFileName) {/*from  w w  w .j  a  v a2  s  .  co  m*/
    log.info("Starting WSO2 HttpEventPublisher Client");
    KeyStoreUtil.setTrustStoreParams();
    HttpClient httpClient = new SystemDefaultHttpClient();
    try {
        HttpPost method = new HttpPost(url);
        List<String> messagesList = readMsg(getTestDataFileLocation(testCaseFolderName, dataFileName));
        for (String message : messagesList) {
            StringEntity entity = new StringEntity(message);
            log.info("Sending message:");
            log.info(message + "\n");
            method.setEntity(entity);
            if (url.startsWith("https")) {
                processAuthentication(method, username, password);
            }
            httpClient.execute(method).getEntity().getContent().close();
            Thread.sleep(1000);
        }
        Thread.sleep(500); // Waiting time for the message to be sent

    } catch (Throwable t) {
        log.error("Error when sending the messages", t);
    }
}

From source file:com.verizon.ExportDataToServer.java

public void sendDataToRESTService(String data) {

    try {/*from   ww w  .  j av  a2  s. com*/
        String url = "http://localhost:8080";

        HttpClient client = HttpClientBuilder.create().build();
        HttpPost post = new HttpPost(url);
        List<NameValuePair> urlParameters = new ArrayList<NameValuePair>();
        urlParameters.add(new BasicNameValuePair("data", data));

        HttpEntity stringEnt = new StringEntity(data);
        post.setEntity(stringEnt);

        HttpResponse response = client.execute(post);
        System.out.println("Response Code : " + response.getStatusLine().getStatusCode());
    } catch (IOException ex) {
        System.out.println("Unable to send data to streaming service..");
    }

}

From source file:com.vikingbrain.nmt.test.util.MockUtils.java

public static HttpResponse prepareHttpResponse(int expectedResponseStatus, String expectedResponseBody) {
    HttpResponse response = new BasicHttpResponse(
            new BasicStatusLine(new ProtocolVersion("HTTP", 1, 1), expectedResponseStatus, ""));
    response.setStatusCode(expectedResponseStatus);
    try {/*from  w  w  w  . j  a v a2  s . c  o  m*/
        response.setEntity(new StringEntity(expectedResponseBody));
    } catch (UnsupportedEncodingException e) {
        throw new IllegalArgumentException(e);
    }
    return response;
}

From source file:com.networknt.light.server.handler.loader.MenuLoader.java

private static void loadMenuFile(File file) {
    Scanner scan = null;/*w ww .ja  v  a  2  s .c om*/
    try {
        scan = new Scanner(file, Loader.encoding);
        // the content is only the data portion. convert to map
        String content = scan.useDelimiter("\\Z").next();
        HttpPost httpPost = new HttpPost("http://injector:8080/api/rs");
        StringEntity input = new StringEntity(content);
        input.setContentType("application/json");
        httpPost.setEntity(input);
        CloseableHttpResponse response = httpclient.execute(httpPost);

        try {
            System.out.println(response.getStatusLine());
            HttpEntity entity = response.getEntity();
            BufferedReader rd = new BufferedReader(new InputStreamReader(entity.getContent()));
            String json = "";
            String line = "";
            while ((line = rd.readLine()) != null) {
                json = json + line;
            }
            System.out.println("json = " + json);
            EntityUtils.consume(entity);
        } finally {
            response.close();
        }
    } catch (IOException ioe) {
        ioe.printStackTrace();
    } finally {
        if (scan != null)
            scan.close();
    }
}

From source file:net.modelbased.proasense.storage.registry.RegisterSensorSSN.java

public static String postSensor(Sensor sensor) throws RequestErrorException {
    String content = JsonPrinter.sensorToJson(sensor);
    URI target;/*from  ww w .  j  av a2 s . c o m*/
    try {
        target = new URI(sensor.getUri().toString() + SENSOR_PATH);
    } catch (URISyntaxException e1) {
        e1.printStackTrace();
        throw new RequestErrorException(e1.getMessage());
    }
    HttpClient client = new DefaultHttpClient();
    HttpPost request = new HttpPost(target);
    request.setHeader("Content-type", "application/json");
    String response = null;
    try {
        StringEntity seContent = new StringEntity(content);
        seContent.setContentType("text/json");
        request.setEntity(seContent);
        response = resolveResponse(client.execute(request));
    } catch (Exception e) {
        throw new RequestErrorException(e.getMessage());
    }
    return response;
}