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

/**
 * Make POST request and return plain response
 * @param url// w w w . j  av a 2 s .  c om
 * @param pairs
 * @return Plain text response
 * @throws Exception
 */
public static String makeHttpPostRequest(String url, List<NameValuePair> pairs) throws Exception {
    BufferedReader in = null;
    String result = "";
    try {
        HttpClient client = new DefaultHttpClient();
        HttpPost post = new HttpPost(url);
        post.setEntity(new UrlEncodedFormEntity(pairs));
        HttpResponse response = client.execute(post);

        in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
        StringBuffer sb = new StringBuffer("");
        String line = "";
        String NL = System.getProperty("line.separator");
        while ((line = in.readLine()) != null) {
            sb.append(line + NL);
        }
        in.close();
        result = sb.toString();
        // System.out.println(result);
    } catch (Exception e) {

        e.printStackTrace();
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    return result;
}

From source file:Main.java

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

    try {//from 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 makeRequest3(String url, String json) {
    Log.v(TAG, "URL-->" + url);
    Log.v(TAG, "input-->" + json);

    try {//from  w  w  w .j a  va  2s  .  c o  m
        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:org.codeqinvest.web.IntegrationTestHelper.java

private static void doPostRequest(String uri, List<NameValuePair> parameters) throws IOException {
    HttpClient httpClient = new DefaultHttpClient();
    HttpPost post = new HttpPost(uri);
    post.setEntity(new UrlEncodedFormEntity(parameters, Consts.UTF_8));
    httpClient.execute(post);// w ww . j  av a  2  s .  c o  m
}

From source file:org.eclipse.dirigible.cli.apis.ImportProjectAPI.java

public static void importProject(RequestConfig config, String url, InputStream in, String filename,
        Map<String, String> headers) throws IOException {
    CloseableHttpClient httpClient = HttpClients.createDefault();

    MultipartEntityBuilder entityBuilder = MultipartEntityBuilder.create();
    entityBuilder.addPart(PART_NAME, new InputStreamBody(in, filename));

    HttpPost postRequest = new HttpPost(url);
    postRequest.setEntity(entityBuilder.build());
    addHeaders(postRequest, headers);/*from   w  w w. ja v  a2 s .com*/
    if (config != null) {
        postRequest.setConfig(config);
    }
    executeRequest(httpClient, postRequest);
}

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  ww.ja  v  a 2  s. c om*/
        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:apidemo.APIDemo.java

public static String sendPost(String url, byte[] data)
        throws UnsupportedEncodingException, IOException, NoSuchAlgorithmException {
    //        logger.info("url: " + url);
    CloseableHttpClient httpclient = HttpClients.createDefault();
    HttpPost httpPost = new HttpPost(url);
    httpPost.setEntity(new ByteArrayEntity(data));

    try (CloseableHttpResponse res = httpclient.execute(httpPost)) {
        HttpEntity entity = res.getEntity();

        InputStream inputStream = entity.getContent();
        String sResponse = IOUtils.toString(inputStream, "UTF-8");

        return sResponse;
    }/*from  w  w w  .j  av a  2s .c o m*/
}

From source file:com.jkoolcloud.tnt4j.streams.fields.ActivityCacheTest.java

private static void sendRequest(HttpClient client, String file) throws Exception {
    URI url = makeURI();/*from  ww w .j a  va 2s  .  c  o m*/
    HttpPost post = new HttpPost(url);

    post.setEntity(EntityBuilder.create().setText(getFileContents(file)).build());
    final HttpResponse returned = client.execute(post);
}

From source file:io.fabric8.maven.docker.access.util.RequestUtil.java

public static HttpUriRequest newPost(String url, String body) {
    HttpPost post = new HttpPost(url);
    if (body != null) {
        post.setEntity(new StringEntity(body, Charset.defaultCharset()));
    }/*w w  w  .  j  a  v  a  2 s.com*/
    return addDefaultHeaders(post);
}

From source file:org.n52.shetland.util.HTTP.java

public static byte[] post(URI uri, byte[] bytes) throws IOException {
    HttpPost request = new HttpPost(uri);
    request.setEntity(new ByteArrayEntity(bytes));
    return execute(request);
}