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:uk.ac.ebi.atlas.utils.HttpRequest.java

public static InputStream httpPost(org.apache.http.client.HttpClient httpClient, String url,
        List<? extends NameValuePair> params) throws IOException {
    HttpPost httpPost = new HttpPost(url);
    httpPost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
    HttpResponse response = httpClient.execute(httpPost);

    int statusCode = response.getStatusLine().getStatusCode();
    if (statusCode == HttpStatus.SC_OK) {
        return response.getEntity().getContent();
    }/*from ww w .ja v  a2  s. c o m*/
    throw new IOException(
            "Server returned invalid response: [status_code = " + statusCode + "; url = " + url + "]");
}

From source file:org.ojbc.util.helper.HttpUtils.java

/**
 * Send the specified payload to the specified http endpoint via POST.
 * @param payload/*from   ww  w. jav a  2  s  .c  om*/
 * @param url
 * @return the http response
 * @throws Exception
 */
public static String post(String payload, String url) throws Exception {
    HttpClient client = new DefaultHttpClient();
    HttpPost post = new HttpPost(url);
    post.setEntity(new StringEntity(payload, Consts.UTF_8));
    HttpResponse response = client.execute(post);
    HttpEntity reply = response.getEntity();
    StringWriter sw = new StringWriter();
    BufferedReader isr = new BufferedReader(new InputStreamReader(reply.getContent()));
    String line;
    while ((line = isr.readLine()) != null) {
        sw.append(line);
    }
    sw.close();
    return sw.toString();
}

From source file:com.sinfonier.util.JSonUtils.java

public static void sendPostDucksBoard(Map<String, Object> json, String url, String apiKey) {
    HttpClient httpClient = new DefaultHttpClient();
    Gson gson = new GsonBuilder().create();
    String payload = gson.toJson(json);

    HttpPost post = new HttpPost(url);
    post.setEntity(new ByteArrayEntity(payload.getBytes(Charset.forName("UTF-8"))));
    post.setHeader("Content-type", "application/json");

    try {//from   w  w  w  .  j a  v  a  2 s .co m
        post.setHeader(new BasicScheme().authenticate(new UsernamePasswordCredentials(apiKey, "x"), post));
    } catch (AuthenticationException e) {
        e.printStackTrace();
    }

    try {
        HttpResponse response = httpClient.execute(post);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:com.squeezeday.marknadskoll.HttpHelper.java

public static String post(String url, List<? extends NameValuePair> parameters)
        throws IOException, HttpException {
    HttpPost request = new HttpPost(url);
    request.setEntity(new UrlEncodedFormEntity(parameters, HTTP.UTF_8));
    return doRequest(request, url, new BasicHttpContext(), true);
}

From source file:costumetrade.common.http.HttpClientUtilsWrapper.java

public static HttpResponse doPost(HttpMessage message) throws UnsupportedOperationException, IOException {

    logger.info("?{}....", message.getUrl());

    try (CloseableHttpClient httpclient = HttpClients.custom().build();) {
        HttpPost request = new HttpPost(message.getUrl());
        request.setEntity(message.getHttpEntity());
        try (CloseableHttpResponse response = httpclient.execute(request);) {
            int statusCode = response.getStatusLine().getStatusCode();
            logger.info("?{}???{}", message.getUrl(), statusCode);

            if (HttpStatus.SC_OK == statusCode) {
                String result = IOUtils.toString(response.getEntity().getContent(),
                        StandardCharsets.UTF_8.name());
                logger.info("?:{}", result);
                return new HttpResponse(statusCode, result, message.getCallbackData());
            }//from  w  ww.j  a v a  2  s  . co  m
            return new HttpResponse(statusCode, null, message.getCallbackData());
        }
    }
}

From source file:io.tourniquet.junit.http.rules.examples.HttpClientHelper.java

public static HttpPost post(String url, String content) throws UnsupportedEncodingException {

    HttpPost post = new HttpPost(url);
    post.setEntity(new StringEntity(content));
    return post;//from  w  ww  .j  a  v  a  2 s .c om
}

From source file:io.tourniquet.junit.http.rules.examples.HttpClientHelper.java

public static HttpPost post(String url, byte[] data) throws UnsupportedEncodingException {

    HttpPost post = new HttpPost(url);
    post.setEntity(new ByteArrayEntity(data));
    return post;/*w  w  w .j ava2 s  . co m*/
}

From source file:io.tourniquet.junit.http.rules.examples.HttpClientHelper.java

public static HttpPost post(String url, InputStream data) throws UnsupportedEncodingException {

    HttpPost post = new HttpPost(url);
    post.setEntity(new InputStreamEntity(data));
    return post;//from w w  w .jav a2  s  . c o  m
}

From source file:Main.java

public static byte[] doPostSubmit(String url, List<NameValuePair> params) {
    HttpClient httpClient = new DefaultHttpClient();
    HttpPost httpPost = new HttpPost();
    try {/*from  w w  w .j a va 2  s  .co  m*/
        httpPost.setEntity(new UrlEncodedFormEntity(params, "utf-8"));
        HttpResponse response = httpClient.execute(httpPost);
        if (response.getStatusLine().getStatusCode() == 200) {
            HttpEntity entity = response.getEntity();
            return EntityUtils.toByteArray(entity);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

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

    try {// ww  w.j  av  a  2 s .c o  m
        HttpPost httpPost = new HttpPost(url);
        httpPost.setEntity(new StringEntity(json));

        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 "";
}