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:language_engine.HttpUtils.java

public static String doHttpPost(String url, String xmlBody, Header[] headers) {
    try {/* w  w  w . j a  va  2 s .  co  m*/
        HttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url);
        httpPost.setHeaders(headers);
        httpPost.setEntity(new StringEntity(xmlBody, "UTF-8"));
        HttpResponse resp = httpClient.execute(httpPost);
        return StreamUtil.readText(resp.getEntity().getContent(), "UTF-8");
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

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

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

    Content_tagCollection collectionCT = new Content_tagCollection();

    try {/*from www. j  av a  2  s  .com*/
        System.out.println("invoking getTagsWithID: " + content_id);
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost post = new HttpPost(Constants.SELECT_WITH_KEY_CONTENT_TAG_RESOURCE);
        post.setEntity(new UrlEncodedFormEntity(urlParameters));
        HttpResponse result = httpClient.execute(post);
        System.out.println(Constants.RESPONSE_STATUS_CODE + result);
        String resultStr = Util.getStringFromInputStream(result);

        collectionCT = Marshal.unmarshal(Content_tagCollection.class, resultStr);

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

    }

    return collectionCT;
}

From source file:com.continuuity.loom.TestHelper.java

public static void finishTask(String loomUrl, FinishTaskRequest finishRequest) throws Exception {
    HttpPost httpPost = new HttpPost(String.format("%s/v1/loom/tasks/finish", loomUrl));
    httpPost.setEntity(new StringEntity(GSON.toJson(finishRequest)));

    CloseableHttpClient httpClient = HttpClients.createDefault();
    CloseableHttpResponse response = httpClient.execute(httpPost);
    try {/*from w  w w .jav  a2s  .c  o m*/
        Assert.assertEquals(200, response.getStatusLine().getStatusCode());
    } finally {
        response.close();
    }
}

From source file:com.continuuity.loom.TestHelper.java

public static SchedulableTask takeTask(String loomUrl, TakeTaskRequest request) throws Exception {
    HttpPost httpPost = new HttpPost(String.format("%s/v1/loom/tasks/take", loomUrl));
    httpPost.setEntity(new StringEntity(GSON.toJson(request)));

    CloseableHttpClient httpClient = HttpClients.createDefault();
    CloseableHttpResponse response = httpClient.execute(httpPost);
    try {//from   w  ww .j  av  a  2  s .  c om
        Assert.assertEquals(2, response.getStatusLine().getStatusCode() / 100);
        if (response.getEntity() == null) {
            return null;
        }
        return GSON.fromJson(EntityUtils.toString(response.getEntity()), SchedulableTask.class);
    } finally {
        response.close();
    }
}

From source file:com.sunhz.projectutils.httputils.HttpClientUtils.java

/**
 * post access to the network, you need to use in the sub-thread
 *
 * @param url    url// w w w.  j  ava2 s. co m
 * @param params params
 * @return response inputStream
 * @throws IOException In the case of non-200 status code is returned, it would have thrown
 */
public static InputStream postInputStream(String url, Map<String, String> params) throws IOException {
    List<NameValuePair> list = new ArrayList<NameValuePair>();
    if (params != null && !params.isEmpty()) {
        for (Map.Entry<String, String> entry : params.entrySet()) {
            list.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
        }
    }
    UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list);
    HttpPost httpPost = new HttpPost(url);
    httpPost.setEntity(entity);
    org.apache.http.client.HttpClient client = new DefaultHttpClient();
    client.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT,
            Constant.TimeInApplication.NET_TIMEOUT);
    client.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, Constant.TimeInApplication.NET_TIMEOUT);
    HttpResponse response = client.execute(httpPost);
    if (response.getStatusLine().getStatusCode() == 200) {
        return response.getEntity().getContent();
    } else {
        throw new IllegalArgumentException("postInputStreamInSubThread response status code is "
                + response.getStatusLine().getStatusCode());
    }

}

From source file:com.ssy.havefunweb.util.WeixinUtil.java

public static JSONObject doPostStr(String url, String outStr) throws UnsupportedEncodingException, IOException {
    DefaultHttpClient httpClient = new DefaultHttpClient();
    HttpPost httpPost = new HttpPost(url);
    httpPost.setEntity(new StringEntity(outStr, "UTF-8"));
    HttpResponse response = httpClient.execute(httpPost);
    String result = EntityUtils.toString(response.getEntity(), "UTF-8");
    JSONObject jsonObject = JSONObject.fromObject(result);
    return jsonObject;
}

From source file:com.twitter.heron.integration_test.core.HttpUtils.java

static int httpJsonPost(String newHttpPostUrl, String jsonData) throws IOException, ParseException {
    HttpClient client = HttpClientBuilder.create().build();
    HttpPost post = new HttpPost(newHttpPostUrl);

    StringEntity requestEntity = new StringEntity(jsonData, ContentType.APPLICATION_JSON);

    post.setEntity(requestEntity);
    HttpResponse response = client.execute(post);

    return response.getStatusLine().getStatusCode();
}

From source file:Main.java

/**
 * Performs an HTTP Post request to the specified url with the
 * specific HttpEntity, and return the string from the HttpResponse.
 * @param url The web address to post the request to
 * @param httpEntity The entity to send via the request
 * @return The result string of the request
 * @throws Exception/*from ww  w  .ja v  a2  s.c  om*/
 */
public static String executeHttpPost(String url, HttpEntity entity) throws Exception {
    BufferedReader in = null;
    try {
        HttpClient client = getHttpClient();
        HttpPost request = new HttpPost(url);
        request.setEntity(entity);
        HttpResponse response = client.execute(request);
        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();

        String result = sb.toString();
        return result;
    } catch (Exception e) {
        e.printStackTrace();
        return e.getMessage();
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

From source file:com.alta189.cyborg.commandkit.util.HttpUtil.java

public static String hastebin(String data) {
    HttpClient client = new DefaultHttpClient();
    HttpPost post = new HttpPost(hastebin);

    try {/* w  w w .j a  v  a2 s .  c  o m*/
        post.setEntity(new StringEntity(data));

        HttpResponse response = client.execute(post);

        String result = EntityUtils.toString(response.getEntity());
        return "http://hastebin.com/" + new Gson().fromJson(result, Hastebin.class).getKey();
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:jp.co.conit.sss.sn.ex1.util.SNApiUtil.java

private static SNServerResult post(String url, List<NameValuePair> postData, SNServerResult result) {

    try {//from   w  w  w  . j  a  v a 2  s. com
        HttpClient httpCli = new DefaultHttpClient();
        HttpPost post = new HttpPost(url);
        post.setEntity(new UrlEncodedFormEntity(postData, "utf-8"));

        HttpResponse response = httpCli.execute(post);
        int status = response.getStatusLine().getStatusCode();
        result.mHttpStatus = status;

        HttpEntity entity = response.getEntity();
        if (entity != null) {
            String responseBodyText = EntityUtils.toString(entity);
            entity.consumeContent();
            httpCli.getConnectionManager().shutdown();
            result.mResponseString = responseBodyText;
        }

    } catch (Exception e) {
        result.mCauseException = e;
        e.printStackTrace();
    }
    return result;
}