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:Main.java

public static String makeRequest3(String url, String json) {
    Log.v(TAG, "URL-->" + url);
    Log.v(TAG, "input-->" + json);

    try {/*w  w  w  . j a  va  2 s.c om*/
        HttpPost httpPost = new HttpPost(url);
        httpPost.setEntity(new StringEntity(json));
        //httpPost.setHeader("sessionToken",AuthToken);
        // httpPost.setHeader("Accept", "application/json");
        //  httpPost.setHeader("Content-type", "application/json"); //text/html
        HttpResponse httpResponse = new DefaultHttpClient().execute(httpPost);

        // receive response as inputStream
        InputStream inputStream = httpResponse.getEntity().getContent();
        // convert inputstream to string
        if (inputStream != null) {
            String result = convertInputStreamToString(inputStream);
            Log.d("tag", "outputtttttttttt-->" + result);
            return result;
        }
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return "";
}

From source file:Main.java

public static String makeRequest55(String url, String json) {
    Log.v(TAG, "URL-->" + url);
    Log.v(TAG, "input-->" + json);

    try {/*  w  w w .j a  v  a2 s  .  c  o m*/
        HttpPost httpPost = new HttpPost(url);
        httpPost.setEntity(new StringEntity(json));
        httpPost.setHeader("Accept", "application/json");
        httpPost.setHeader("Content-type", "application/json");
        //text/html
        HttpResponse httpResponse = new DefaultHttpClient().execute(httpPost);

        // receive response as inputStream
        InputStream inputStream = httpResponse.getEntity().getContent();
        // convert inputstream to string
        if (inputStream != null) {
            String result = convertInputStreamToString(inputStream);
            Log.v(TAG, "output-->" + result);
            return result;
        }
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return "";
}

From source file:Main.java

public static String makeRequest(String url, String json) {
    Log.v(TAG, "URL-->" + url);
    Log.v(TAG, "input-->" + json);

    try {/*from  w  w w .j av  a 2  s .  com*/
        HttpPost httpPost = new HttpPost(url);
        httpPost.setEntity(new StringEntity(json));
        httpPost.setHeader("Accept", "application/json");
        httpPost.setHeader("Content-type", "application/json");
        httpPost.setHeader("sessionToken", "61");
        //text/html
        HttpResponse httpResponse = new DefaultHttpClient().execute(httpPost);

        // receive response as inputStream
        InputStream inputStream = httpResponse.getEntity().getContent();
        // convert inputstream to string
        if (inputStream != null) {
            String result = convertInputStreamToString(inputStream);
            Log.v(TAG, "output-->" + result);
            return result;
        }
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return "";
}

From source file:com.mycompany.trader.HTTPTransport.java

public static HttpPost createPost(String resource, String data) {
    try {/*from w  ww. j  av  a 2s .  c  om*/
        HttpPost post = new HttpPost(url + resource);
        StringEntity input = new StringEntity(data);
        input.setContentType("application/json");
        post.setEntity(input);
        return post;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:coolmapplugin.util.HTTPRequestUtil.java

public static String executePost(String targetURL, String jsonBody) {

    try (CloseableHttpClient httpClient = HttpClientBuilder.create().build()) {

        HttpPost request = new HttpPost(targetURL);

        StringEntity params = new StringEntity(jsonBody);
        request.addHeader("content-type", "application/json");
        request.setEntity(params);/*from  ww w  . ja  v a 2s .co m*/

        HttpResponse result = httpClient.execute(request);

        HttpEntity entity = result.getEntity();
        if (entity == null)
            return null;

        String jsonResult = EntityUtils.toString(result.getEntity(), "UTF-8");

        return jsonResult;

    } catch (IOException e) {
        return null;
    }
}

From source file:it410.gmu.edu.OrderingServiceClient.java

public static void addCustomerJSON(Customer customer) throws IOException {

    Gson gson = new Gson();
    String customerString = gson.toJson(customer);
    System.out.println("Customer JSON = " + customerString);

    HttpClient client = new DefaultHttpClient();

    HttpPost post = new HttpPost("http://localhost:8080/BookstoreRestService/generic/addOrderJSON");
    post.setHeader("Content-Type", "application/json");
    StringEntity entity = new StringEntity(customerString);
    post.setEntity(entity);//  w  ww. j  a v a  2 s . com
    HttpResponse response = client.execute(post);

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

}

From source file:com.niceapps.app.HttpClient.java

public static JSONObject SendHttpPost(String URL, JSONObject jsonObjSend) {

    try {//from  w  w w. j a v a  2  s .c  o m
        DefaultHttpClient httpclient = new DefaultHttpClient();
        HttpPost httpPostRequest = new HttpPost(URL);

        StringEntity se;
        se = new StringEntity(jsonObjSend.toString());

        // Set HTTP parameters
        httpPostRequest.setEntity(se);
        httpPostRequest.setHeader("Accept", "application/json");
        httpPostRequest.setHeader("Content-Type", "application/json");

        HttpResponse response = (HttpResponse) httpclient.execute(httpPostRequest);

        if (response.getStatusLine().getStatusCode() == 200) {
            HttpEntity entity = response.getEntity();
            JSONObject jsonObjRecv = new JSONObject(EntityUtils.toString(entity));

            return jsonObjRecv;
        } else {
            return null;
        }

    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:org.jboss.pnc.mavenrepositorymanager.ArtifactUploadUtils.java

public static boolean put(CloseableHttpClient client, String url, String content) throws IOException {
    HttpPut put = new HttpPut(url);
    put.setEntity(new StringEntity(content));
    return client.execute(put, response -> {
        try {/*w ww . ja  v a 2 s.c om*/
            return response.getStatusLine().getStatusCode() == 201;
        } finally {
            if (response instanceof CloseableHttpResponse) {
                IOUtils.closeQuietly((CloseableHttpResponse) response);
            }
        }
    });
}

From source file:net.saga.sync.gameserver.portal.CreatePlayer.java

public static Player createPlayer(HttpServletRequest req) throws UnsupportedEncodingException {
    SkeletonKeySession session = (SkeletonKeySession) req.getAttribute(SkeletonKeySession.class.getName());

    HttpClient client = new HttpClientBuilder().trustStore(session.getMetadata().getTruststore())
            .hostnameVerification(HttpClientBuilder.HostnameVerificationPolicy.ANY).build();
    try {/*from ww  w .j  a  v a2  s.  co m*/
        HttpPost post = new HttpPost("https://localhost:8443/gameserver-database/player");
        post.addHeader("Authorization", "Bearer " + session.getTokenString());
        post.setEntity(new StringEntity("{}"));
        try {
            HttpResponse response = client.execute(post);
            if (response.getStatusLine().getStatusCode() != 200) {
                throw new RuntimeException("" + response.getStatusLine().getStatusCode());
            }
            HttpEntity entity = response.getEntity();
            InputStream is = entity.getContent();
            try {
                return JsonSerialization.readValue(is, Player.class);
            } finally {
                is.close();
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    } finally {
        client.getConnectionManager().shutdown();
    }
}

From source file:wvw.utils.pc.HttpHelper.java

public static String post(String url, JSONArray headers, String data) {
    HttpPost httpPost = new HttpPost(url);

    for (int i = 0; i < headers.length(); i++) {
        JSONArray header = headers.getJSONArray(i);

        httpPost.addHeader(header.getString(0), header.getString(1));
    }//from w  w w .  ja v  a  2  s.c o m

    String ret = null;
    try {
        httpPost.setEntity(new StringEntity(data));

        HttpClient httpClient = new DefaultHttpClient();
        HttpResponse response = httpClient.execute(httpPost);

        StatusLine status = response.getStatusLine();

        switch (status.getStatusCode()) {

        case 200:
            ret = IOUtils.readFromStream(response.getEntity().getContent());

            break;

        default:
            ret = genError(status.getReasonPhrase());

            break;
        }

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

        ret = genError(e.getClass() + ": " + e.getMessage());
    }

    return ret;
}