Example usage for org.apache.http.client.methods HttpPost setEntity

List of usage examples for org.apache.http.client.methods HttpPost setEntity

Introduction

In this page you can find the example usage for org.apache.http.client.methods HttpPost setEntity.

Prototype

public void setEntity(final HttpEntity entity) 

Source Link

Usage

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 {/* w  ww. j av  a 2s  .c  o  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:com.calclab.emite.core.client.services.android.AndroidConnector.java

public static void send(final String httpBase, final String request, final ConnectorCallback listener) {
    Runnable r = new Runnable() {
        public void run() {
            try {
                HttpPost method = new HttpPost(httpBase);
                method.addHeader("Content-Type", "text/xml; charset=\"utf-8\"");
                method.setEntity(new StringEntity(request, "utf-8"));

                final HttpResponse response = new DefaultHttpClient().execute(method);
                handler.post(new Runnable() {
                    public void run() {
                        try {
                            HttpEntity entity = response.getEntity();
                            String content = readInputStream(entity.getContent());
                            listener.onResponseReceived(response.getStatusLine().getStatusCode(), content);
                        } catch (Throwable e) {
                            listener.onError(request, e);
                        }//from  w w w .  j  a  va  2  s  .  c o  m
                    }
                });
            } catch (final Throwable e) {
                handler.post(new Runnable() {
                    public void run() {
                        listener.onError(request, e);
                    }
                });
            }
        }
    };
    new Thread(r).start();
}

From source file:att.jaxrs.client.Webinar.java

public static String deleteWebinar(long content_id) {
    List<NameValuePair> urlParameters = new ArrayList<NameValuePair>();
    urlParameters.add(new BasicNameValuePair("content_id", Long.toString(content_id)));

    String resultStr = "";

    try {/*from w  w  w  .j a  v a 2  s .c o m*/

        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost post = new HttpPost(Constants.DELETE_WEBINAR_RESOURCE);
        post.setEntity(new UrlEncodedFormEntity(urlParameters));
        HttpResponse result = httpClient.execute(post);
        System.out.println(Constants.RESPONSE_STATUS_CODE + result);
        resultStr = Util.getStringFromInputStream(result.getEntity().getContent());
        System.out.println(Constants.RESPONSE_BODY + resultStr);

    } catch (Exception e) {
        System.out.println(e.getMessage());

    }
    return resultStr;
}

From source file:fi.iki.dezgeg.matkakorttiwidget.gui.MatkakorttiWidgetApp.java

public static AsyncTask<Void, Void, Void> reportException(final String where, final Throwable e) {
    e.printStackTrace();/* w  ww.  j  a v a  2s  . co  m*/
    if (prefs.getLong("lastErrorReport", new Date().getTime()) - new Date().getTime() <= MIN_REPORT_INTERVAL_MS)
        return null;
    prefs.edit().putLong("lastErrorReport", new Date().getTime()).commit();

    return new AsyncTask<Void, Void, Void>() {

        @Override
        protected Void doInBackground(Void... voids) {
            try {
                StringWriter sw = new StringWriter();
                PrintWriter pw = new PrintWriter(sw);
                e.printStackTrace(pw);

                List<NameValuePair> data = new ArrayList<NameValuePair>();
                data.add(new BasicNameValuePair("where", where));
                data.add(new BasicNameValuePair("backtrace", sw.toString()));
                if (packageInfo != null) {
                    data.add(new BasicNameValuePair("version", String.format("%s (%s %s)",
                            packageInfo.versionName, packageInfo.versionCode, DEBUG ? "debug" : "release")));
                }

                HttpClient httpclient = new DefaultHttpClient();
                HttpPost post = new HttpPost(ERROR_REPORT_URL);
                try {
                    post.setEntity(new UrlEncodedFormEntity(data));
                } catch (UnsupportedEncodingException uee) {
                }
                try {
                    httpclient.execute(post);
                    System.out.println("Error reported.");
                } catch (IOException e1) {
                }
            } catch (Throwable t) {
                // ignore
            }
            return null;
        }
    }.execute();
}

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);
    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   ww w  .  j a v a2s .  c  o  m

}

From source file:att.jaxrs.client.Webinar.java

public static String addWebinar(Webinar webinar) {
    List<NameValuePair> urlParameters = new ArrayList<NameValuePair>();
    urlParameters.add(new BasicNameValuePair("content_id", webinar.getContent_id().toString()));
    urlParameters.add(new BasicNameValuePair("presenter", webinar.getPresenter()));

    String resultStr = "";

    try {/*from   w  w  w  .j  a va2  s . c  o m*/

        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost post = new HttpPost(Constants.INSERT_WEBINAR_RESOURCE);
        post.setEntity(new UrlEncodedFormEntity(urlParameters));
        HttpResponse result = httpClient.execute(post);
        System.out.println(Constants.RESPONSE_STATUS_CODE + result);
        resultStr = Util.getStringFromInputStream(result.getEntity().getContent());
        System.out.println(Constants.RESPONSE_BODY + resultStr);

    } catch (Exception e) {
        System.out.println(e.getMessage());

    }
    return resultStr;
}

From source file:att.jaxrs.client.Webinar.java

public static String updateWebinar(Webinar webinar) {
    List<NameValuePair> urlParameters = new ArrayList<NameValuePair>();
    urlParameters.add(new BasicNameValuePair("content_id", webinar.getContent_id().toString()));
    urlParameters.add(new BasicNameValuePair("presenter", webinar.getPresenter()));

    String resultStr = "";

    try {/*from  www  .j  a  v  a 2  s .  c o m*/

        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost post = new HttpPost(Constants.UPDATE_WEBINAR_RESOURCE);
        post.setEntity(new UrlEncodedFormEntity(urlParameters));
        HttpResponse result = httpClient.execute(post);
        System.out.println(Constants.RESPONSE_STATUS_CODE + result);
        resultStr = Util.getStringFromInputStream(result.getEntity().getContent());
        System.out.println(Constants.RESPONSE_BODY + resultStr);

    } catch (Exception e) {
        System.out.println(e.getMessage());

    }
    return resultStr;
}

From source file:com.mysoft.b2b.event.util.HttpUtil.java

/**
 * http???//from   w w  w.j a  va  2s.  c o m
 * @param uri ?
 * @param body ? 
 */
public static Integer send(String uri, String body) {
    if (StringUtils.isEmpty(uri) || StringUtils.isEmpty(body))
        return null;
    HttpClient client = new DefaultHttpClient();
    try {
        HttpPost post = new HttpPost(uri + "/dealEvent.do");
        post.addHeader("Content-Type", "application/json;charset=UTF-8");
        HttpEntity entity = new StringEntity(body);
        post.setEntity(entity);
        HttpResponse response = client.execute(post);
        int code = response.getStatusLine().getStatusCode();
        return code;
    } catch (Exception e) {
        logger.error("http???" + e);
    }
    return null;
}

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

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

    try {//w ww .  j  av a2 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:com.ibm.watson.movieapp.dialog.rest.UtilityFunctions.java

/**
 * Makes HTTP POST request/*from   ww  w . ja  v a  2s. c  o m*/
 * <p>
 * This makes HTTP POST requests to the url provided.
 * 
 * @param httpClient the http client used to make the request
 * @param url the url for the request
 * @param nvps the {name, value} pair parameters to be embedded in the request
 * @return the JSON object response
 * @throws ClientProtocolException if it is unable to execute the call
 * @throws IOException if it is unable to execute the call
 * @throws IllegalStateException if the input stream could not be parsed correctly
 * @throws HttpException if the HTTP call responded with a status code other than 200 or 201
 */
public static JsonObject httpPost(CloseableHttpClient httpClient, String url, List<NameValuePair> nvps)
        throws ConnectException, ClientProtocolException, IOException, IllegalStateException, HttpException {
    HttpPost httpPost = new HttpPost(url);
    if (nvps != null) {
        httpPost.setEntity(new UrlEncodedFormEntity(nvps));
    }
    try (CloseableHttpResponse response = httpClient.execute(httpPost)) {
        return parseHTTPResponse(response, url);
    }
}