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.ow2.bonita.facade.rest.apachehttpclient.ApacheHttpClientUtil.java

public static HttpResponse executeHttpConnection(String uri, List<NameValuePair> formParams, String options,
        String username, String password) throws URISyntaxException, IOException, IOException {
    UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formParams, "UTF-8");
    String serverAddress = HttpRESTUtil.getRESTServerAddress();
    HttpPost post = new HttpPost(serverAddress + uri);
    post.setEntity(entity);
    HttpClient client = ApacheHttpClientUtil.getHttpClient(serverAddress, username, password);
    HttpResponse httpresponse = client.execute(post);
    return httpresponse;
}

From source file:com.gozap.chouti.service.HttpService.java

/**
 *
 * @param uri uri/* ww  w  . j av a2 s .com*/
 * @return
 * @author saint
 * @date 2013-4-17
 */
public static Boolean post(String uri, UrlEncodedFormEntity entity) {
    HttpClient httpClient = HttpClientFactory.getInstance();
    HttpPost httpPost = new HttpPost(uri);
    httpPost.setEntity(entity);
    try {
        HttpResponse httpResponse = httpClient.execute(httpPost);
        HttpEntity httpEntity = httpResponse.getEntity();
        String jsonStr = EntityUtils.toString(httpEntity);
        JSONObject json = JSON.parseObject(jsonStr);
        if (!json.getString("code").equals("99999")) {
            LOGGER.error("post uri:" + uri + ":" + entity + "error!" + "error msg: " + jsonStr);
        } else {
            return true;
        }
    } catch (ClientProtocolException e) {
        LOGGER.error("get" + uri + "error", e);
    } catch (IOException e) {
        LOGGER.error("get" + uri + "error", e);
    } finally {
        httpPost.releaseConnection();
    }

    return false;
}

From source file:org.wso2.carbon.tfl.realtime.TflStream.java

public static void send(ArrayList<String> jsonList, String endPoint) {
    for (String data : jsonList) {
        HttpClient client = new DefaultHttpClient();
        HttpPost post = new HttpPost(endPoint);
        try {
            StringEntity entity = new StringEntity(data);
            post.setEntity(entity);/*from  w w w . j  a  v a  2 s  .  co  m*/
            HttpResponse response = client.execute(post);
            log.info("data sent : " + data);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

From source file:com.dnastack.bob.service.processor.util.HttpUtils.java

private static HttpPost createPost(String url, List<NameValuePair> data) throws UnsupportedEncodingException {
    HttpPost httpPost = new HttpPost(url);
    httpPost.setEntity(new UrlEncodedFormEntity(data));

    return httpPost;
}

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

public static HttpPost post(String url, BasicNameValuePair... params) throws UnsupportedEncodingException {

    HttpPost post = new HttpPost(url);
    if (params.length > 0) {
        post.setEntity(new UrlEncodedFormEntity(Arrays.asList(params)));
    }//from  w  w  w. ja v a 2  s.  com
    return post;
}

From source file:com.weitaomi.systemconfig.wechat.ClientCustomSSL.java

public static String connectKeyStore(String url, String xml, String path, int flag) throws Exception {
    KeyStore keyStore = KeyStore.getInstance("PKCS12");
    File file = LoadFileFactory.getFile(path);
    char[] arr = null;
    if (flag == 0) {
        arr = WechatConfig.MCHID.toCharArray();
    }//from   w w  w  .  j a v  a 2 s. c om
    if (flag == 1) {
        arr = WechatConfig.MCHID_OFFICIAL.toCharArray();
    }
    FileInputStream instream = new FileInputStream(file);
    try {
        keyStore.load(instream, arr);
    } finally {
        instream.close();
    }

    // Trust own CA and all self-signed certs
    SSLContext sslcontext = SSLContexts.custom().loadKeyMaterial(keyStore, arr).build();
    // Allow TLSv1 protocol only
    SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext, new String[] { "TLSv1" },
            null, SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);
    CloseableHttpClient httpclient = HttpClients.custom().setSSLSocketFactory(sslsf).build();

    StringEntity entityRequest = new StringEntity(xml, "utf-8");
    HttpPost httpPost = new HttpPost(url);
    httpPost.setEntity(entityRequest);
    //        httpPost.setHeader("Content-Type", "application/json");//; charset=utf-8
    HttpResponse response = httpclient.execute(httpPost);

    if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
        throw new RuntimeException("");
    }
    HttpEntity resEntity = response.getEntity();
    InputStream inputStream = resEntity.getContent();
    return HttpRequestUtils.readInstream(inputStream, "UTF-8");
}

From source file:org.fcrepo.camel.indexing.solr.integration.TestUtils.java

public static void httpPost(final String url, final String content, final String mimeType) throws Exception {
    final CloseableHttpClient httpClient = HttpClients.createDefault();
    final HttpPost post = new HttpPost(url);
    post.addHeader(Exchange.CONTENT_TYPE, mimeType);
    post.setEntity(new StringEntity(content));
    httpClient.execute(post);/*from ww w  .j a  v  a 2  s  . c  o  m*/
}

From source file:it.unicaradio.android.utils.NetworkUtils.java

public static byte[] httpPost(String url, byte[] postData, String contentType)
        throws ClientProtocolException, IOException, HttpException {
    HttpParams httpParams = new BasicHttpParams();

    HttpPost post = new HttpPost(url);
    post.setEntity(new ByteArrayEntity(postData));
    post.addHeader("Content-Type", contentType);

    HttpClient client = new DefaultHttpClient(httpParams);
    HttpResponse response = client.execute(post);

    HttpEntity httpEntity = response.getEntity();
    if (httpEntity == null) {
        throw new HttpException("No answer from server.");
    }//  w  w  w .  j  a  v  a  2s .c o  m

    return EntityUtils.toByteArray(httpEntity);
}

From source file:com.jivesoftware.os.amza.service.AmzaSetStress.java

private static void feed(org.apache.http.client.HttpClient httpClient, String hostName, int port,
        String partitionName, int firstDocId, int count, int batchSize)
        throws IOException, InterruptedException {
    long start = System.currentTimeMillis();
    for (int key = firstDocId; key < count; key++) {
        StringBuilder url = new StringBuilder();
        url.append("http://");
        url.append(hostName).append(":").append(port);
        url.append("/amza/multiSet");
        url.append("/").append(partitionName);

        Map<String, String> values = new LinkedHashMap<>();
        for (int b = 0; b < batchSize; b++) {

            values.put(b + "k" + key, b + "v" + key);
        }/*from  ww  w .  ja v a 2 s . c om*/

        String postJsonBody = MAPPER.writeValueAsString(values);
        while (true) {
            HttpPost method = new HttpPost(url.toString());
            method.setEntity(new StringEntity(postJsonBody, ContentType.APPLICATION_JSON));
            method.setHeader(CONTENT_TYPE_HEADER_NAME, APPLICATION_JSON_CONTENT_TYPE);

            StatusLine statusLine;
            try {
                try {
                    HttpResponse response = httpClient.execute(method);
                    statusLine = response.getStatusLine();
                    if (statusLine.getStatusCode() == 200) {
                        break;
                    }
                } catch (Exception x) {
                    x.printStackTrace();
                }
                Thread.sleep(1000);
            } finally {
                method.releaseConnection();
            }
        }

        if (key % 100 == 0) {
            long elapse = System.currentTimeMillis() - start;
            double millisPerAdd = (elapse / (double) key);
            System.out.println(partitionName + " millisPerAdd:" + millisPerAdd + " addsPerSec:"
                    + (1000d / millisPerAdd) + " key:" + key);
        }
    }
}

From source file:com.github.rnewson.couchdb.lucene.couchdb.HttpUtils.java

public static final String post(final HttpClient httpClient, final String url, final JSONObject body)
        throws IOException {
    final HttpPost post = new HttpPost(url);
    post.setHeader("Content-Type", Constants.APPLICATION_JSON);
    post.setEntity(new StringEntity(body.toString(), "UTF-8"));
    return execute(httpClient, post);
}