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:org.fcrepo.auth.oauth.integration.api.AbstractOAuthResourceIT.java

protected static HttpPost postDSMethod(final String pid, final String ds, final String content)
        throws UnsupportedEncodingException {
    final HttpPost post = new HttpPost(serverAddress + "objects/" + pid + "/" + ds + "/fcr:content");
    post.setEntity(new StringEntity(content));
    return post;/*w w w .j  ava 2s.com*/
}

From source file:org.roman.findme.RegistrationIntentService.java

public static void makeResponse(List<NameValuePair> nameValuePairs) {
    String url = "http://task-master.zzz.com.ua/server.php";
    Log.d(TAG, "sendRequest");
    try {//  w w w. jav a  2  s. c o  m
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(url);
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        HttpResponse response = httpclient.execute(httppost);
        InputStream in = response.getEntity().getContent();

        StringBuilder stringbuilder = new StringBuilder();
        BufferedReader bfrd = new BufferedReader(new InputStreamReader(in), 1024);
        String line;
        while ((line = bfrd.readLine()) != null)
            stringbuilder.append(line);

        String downloadedString = stringbuilder.toString();
        Log.d(TAG, downloadedString);

    } catch (Exception e) {
        Log.d(TAG, e.toString());
    }
}

From source file:com.google.gct.stats.GoogleUsageTracker.java

private static void sendPing(@NotNull final List<? extends NameValuePair> postData) {
    ApplicationManager.getApplication().executeOnPooledThread(new Runnable() {
        public void run() {
            CloseableHttpClient client = HttpClientBuilder.create().build();
            HttpPost request = new HttpPost(ANALYTICS_URL);

            try {
                request.setEntity(new UrlEncodedFormEntity(postData));
                CloseableHttpResponse response = client.execute(request);
                StatusLine status = response.getStatusLine();
                if (status.getStatusCode() >= 300) {
                    LOG.debug("Non 200 status code : " + status.getStatusCode() + " - "
                            + status.getReasonPhrase());
                }//from   w  ww.j av  a  2s  .  co m
            } catch (IOException ex) {
                LOG.debug("IOException during Analytics Ping", new Object[] { ex.getMessage() });
            } finally {
                HttpClientUtils.closeQuietly(client);
            }

        }
    });
}

From source file:com.lagrange.LabelApp.java

private static void sendPhotoToTelegramBot() {
    File file = new File(pathToImage.toUri());
    HttpEntity httpEntity = MultipartEntityBuilder.create()
            .addBinaryBody("photo", file, ContentType.create("image/jpeg"), file.getName())
            .addTextBody("chat_id", CHAT_ID).build();

    String urlSendPhoto = "https://api.telegram.org/" + BOT_ID + "/sendPhoto";

    CloseableHttpClient httpclient = HttpClients.createDefault();
    HttpPost httpPost = new HttpPost(urlSendPhoto);
    httpPost.setEntity(httpEntity);
    try {/*  ww w  . java  2 s.  c o  m*/
        CloseableHttpResponse response = httpclient.execute(httpPost);
    } catch (IOException e) {
        e.printStackTrace();
    }
    System.out.println("Done sending photo");
}

From source file:com.mber.client.HTTParty.java

public static Call post(final String url, final JSONObject data)
        throws UnsupportedEncodingException, IOException {
    HttpPost request = new HttpPost(url);
    request.setEntity(toStringEntity(data));

    return execute(request);
}

From source file:org.fcrepo.integration.generator.AbstractResourceIT.java

protected static HttpPost postDSMethod(final String pid, final String ds, final String content)
        throws UnsupportedEncodingException {
    final HttpPost post = new HttpPost(
            serverAddress + pid + "/" + ds + "?mixin=" + FedoraJcrTypes.FEDORA_DATASTREAM);
    post.setEntity(new StringEntity(content));
    return post;/*w w  w  .j  ava 2 s  .  c  o m*/
}

From source file:com.atomiton.watermanagement.ngo.util.HttpRequestResponseHandler.java

public static String sendPost(String serverURL, String postBody) throws Exception {

    CloseableHttpClient client = HttpClients.createDefault();
    HttpPost post = new HttpPost(serverURL);

    StringEntity entity = new StringEntity(postBody, "UTF-8");

    post.setEntity(entity);

    HttpResponse response = client.execute(post);
    // System.out.println("\nSending 'POST' request to URL : " + serverURL);

    BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

    StringBuffer result = new StringBuffer();
    String line = "";
    while ((line = rd.readLine()) != null) {
        result.append(line);/*from  w w w  . j  av a 2s.com*/
    }
    client.close();
    // System.out.println(result.toString());
    return result.toString();
}

From source file:com.musevisions.android.SudokuSolver.HttpPostUtils.java

static public String send(HttpPostSettings settings, List<NameValuePair> values) {
    try {/*  w  w  w. ja  va2  s  .  c o  m*/
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(settings.getPostTarget());
        httppost.setEntity(new UrlEncodedFormEntity(values));
        HttpResponse response = httpclient.execute(httppost);
        return EntityUtils.toString(response.getEntity());
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:org.smartloli.kafka.eagle.common.util.HttpClientUtils.java

/**
 * Send request by post method.//  w w w  .  ja v  a 2 s. c o m
 * 
 * @param uri:
 *            http://ip:port/demo
 * @param parames:
 *            new BasicNameValuePair("code", "200")
 * 
 *            new BasicNameValuePair("name", "smartloli")
 */
public static String doPostForm(String uri, List<BasicNameValuePair> parames) {
    String result = "";
    try {
        CloseableHttpClient client = null;
        CloseableHttpResponse response = null;
        try {

            HttpPost httpPost = new HttpPost(uri);
            httpPost.setEntity(new UrlEncodedFormEntity(parames, "UTF-8"));
            client = HttpClients.createDefault();
            response = client.execute(httpPost);
            HttpEntity entity = response.getEntity();
            result = EntityUtils.toString(entity);
        } finally {
            if (response != null) {
                response.close();
            }
            if (client != null) {
                client.close();
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
        LOG.error("Do post form request has error, msg is " + e.getMessage());
    }
    return result;
}

From source file:br.com.vpsa.oauth2android.common.Connection.java

/**
 *
 * Makes a standard http post request. The list of NameValuePairs can contain
 * all parameters and parameter designations for the request.
 *
 * @param parameterList//w  w w .jav a  2 s.c  o m
 * @param url
 * @return <code>Response</code> with the servers response
 * @throws UnsupportedEncodingException
 * @throws IOException
 */
public static Response httpPostRequest(List<NameValuePair> parameterList, String url)
        throws InvalidRequestException, InvalidClientException, InvalidGrantException,
        UnauthorizedClientException, UnsupportedGrantTypeException, InvalidScopeException, OAuthException,
        IOException {
    // prepare
    HttpClient httpclient = new DefaultHttpClient(); // client that executes the post request
    HttpPost httpPost = new HttpPost(url); // post request with the url
    try {
        httpPost.setEntity(new UrlEncodedFormEntity(parameterList)); // set entity with all parameters
    } catch (UnsupportedEncodingException ex) {
        // won't bw thrown
    }

    // request
    Response response = new Response(httpclient.execute(httpPost));

    // set original request url to response (not important)
    response.setRequestUrl(EntityUtils.toString(httpPost.getEntity()));

    return response;
}