Example usage for org.apache.http.client.methods HttpPost HttpPost

List of usage examples for org.apache.http.client.methods HttpPost HttpPost

Introduction

In this page you can find the example usage for org.apache.http.client.methods HttpPost HttpPost.

Prototype

public HttpPost(final String uri) 

Source Link

Usage

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);//w ww.  ja  v  a 2s.  c om
}

From source file:com.fengduo.bee.commons.util.HttpClientUtils.java

public static String postRequest(String url, List<NameValuePair> postParams) throws Exception {
    HttpPost post = new HttpPost(url);
    UrlEncodedFormEntity uefEntity;/*from  ww  w . j av  a2 s.c om*/
    String result = null;
    try {
        uefEntity = new UrlEncodedFormEntity(postParams, CHARSET);
        post.setEntity(uefEntity);
        HttpResponse rep = client.execute(post);
        if (rep.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
            HttpEntity httpEntity = rep.getEntity();
            if (httpEntity != null) {
                result = EntityUtils.toString(httpEntity, "UTF-8");
            }
        }

    } catch (Exception e) {
        throw e;
    } finally {
        post.abort();
    }
    return result;
}

From source file:com.calclab.emite.core.client.services.android.AndroidConnector.java

public static void send(final String httpBase, final String request, final ConnectorCallback listener) {
    Runnable r = new Runnable() {
        public void run() {
            try {
                HttpPost method = new HttpPost(httpBase);
                method.addHeader("Content-Type", "text/xml; charset=\"utf-8\"");
                method.setEntity(new StringEntity(request, "utf-8"));

                final HttpResponse response = new DefaultHttpClient().execute(method);
                handler.post(new Runnable() {
                    public void run() {
                        try {
                            HttpEntity entity = response.getEntity();
                            String content = readInputStream(entity.getContent());
                            listener.onResponseReceived(response.getStatusLine().getStatusCode(), content);
                        } catch (Throwable e) {
                            listener.onError(request, e);
                        }//  w  w w . j  a  va 2 s  .com
                    }
                });
            } catch (final Throwable e) {
                handler.post(new Runnable() {
                    public void run() {
                        listener.onError(request, e);
                    }
                });
            }
        }
    };
    new Thread(r).start();
}

From source file:com.ibm.xsp.xflow.activiti.util.HttpClientUtil.java

public static String post(String url, Cookie[] cookies, String content) {
    String body = null;//  w w  w  .  j a  v  a2 s . co  m
    try {
        StringBuffer buffer = new StringBuffer();
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost postRequest = new HttpPost(url);

        StringEntity input = new StringEntity(content);
        input.setContentType("application/json");
        postRequest.setEntity(input);

        httpClient.setCookieStore(new BasicCookieStore());
        String hostName = new URL(url).getHost();
        String domain = hostName.substring(hostName.indexOf("."));
        for (int i = 0; i < cookies.length; i++) {
            if (logger.isLoggable(Level.FINEST)) {
                logger.finest("Cookie:" + cookies[i].getName() + ":" + cookies[i].getValue() + ":"
                        + cookies[i].getDomain() + ":" + cookies[i].getPath());
            }
            BasicClientCookie cookie = new BasicClientCookie(cookies[i].getName(), cookies[i].getValue());
            cookie.setVersion(0);
            cookie.setDomain(domain);
            cookie.setPath("/");

            httpClient.getCookieStore().addCookie(cookie);
        }
        HttpResponse response = httpClient.execute(postRequest);
        BufferedReader br = new BufferedReader(new InputStreamReader((response.getEntity().getContent())));

        String output;
        if (logger.isLoggable(Level.FINEST)) {
            logger.finest("Output from Server .... \n");
        }
        while ((output = br.readLine()) != null) {
            if (logger.isLoggable(Level.FINEST)) {
                logger.finest(output);
            }
            buffer.append(output);
        }

        httpClient.getConnectionManager().shutdown();

        body = buffer.toString();

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

    return body;
}

From source file:org.opencastproject.util.HttpUtil.java

public static HttpPost post(String uri, NameValuePair... formParams) {
    final HttpPost post = new HttpPost(uri);
    setFormParams(post, formParams);/*from w  w  w .j ava  2  s . c o m*/
    return post;
}

From source file:org.openo.nfvo.emsdriver.northbound.client.HttpClientUtil.java

public static String doPost(String url, String json, String charset) {
    CloseableHttpClient httpClient = null;
    HttpPost httpPost = null;/*from  w w w  . java2  s .com*/
    String result = null;
    try {
        httpClient = HttpClientFactory.getSSLClientFactory();
        httpPost = new HttpPost(url);
        if (null != json) {
            StringEntity s = new StringEntity(json);
            s.setContentEncoding("UTF-8");
            s.setContentType("application/json"); // set contentType
            httpPost.setEntity(s);
        }
        CloseableHttpResponse response = httpClient.execute(httpPost);
        try {
            if (response != null) {
                HttpEntity resEntity = response.getEntity();
                if (resEntity != null) {
                    result = EntityUtils.toString(resEntity, charset);
                }
            }
        } catch (Exception e) {
            log.error("httpClient.execute(httpPost) is fail", e);
        } finally {
            if (response != null) {
                response.close();
            }
        }
    } catch (Exception e) {
        log.error("doPost is fail ", e);
    } finally {
        if (httpClient != null) {
            try {
                httpClient.close();
            } catch (IOException e) {
            }
        }

    }
    return result;
}

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 ww  w.  j  av  a2 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.firewallid.util.FIConnection.java

public static void sendJson(String host, String json) throws UnsupportedEncodingException, IOException {
    DefaultHttpClient httpClient = new DefaultHttpClient();
    HttpPost postRequest = new HttpPost(host);

    StringEntity input = new StringEntity(json);
    input.setContentType("application/json");
    postRequest.setEntity(input);/*from  w  w w  .  ja v  a2  s  .  com*/

    HttpResponse response = httpClient.execute(postRequest);
}

From source file:com.appassit.http.ApiRequestFactory.java

/**
 * ?Market API HttpReqeust//from  w  w  w .jav  a  2  s. co  m
 */
public static HttpUriRequest getRequest(String url, RequestMethod requestMethod,
        ArrayList<BasicNameValuePair> cv) throws IOException {

    if (requestMethod == RequestMethod.GET) {
        String finalUrl = getGetRequestUrl(url, cv);
        HttpGet httpGet = new HttpGet(finalUrl);
        return httpGet;
    } else if (requestMethod == RequestMethod.POST) {
        HttpEntity entity = getPostRequestEntity(cv);
        HttpPost request = new HttpPost(url);
        request.setEntity(entity);
        return request;
    } else {
        return null;
    }
}

From source file:Ahmedabad.SendingNum.java

public SendingNum() throws ClientProtocolException, IOException {

    HttpClient http = new DefaultHttpClient();
    HttpPost post = new HttpPost(
            "http://gmail.com/index.php?option=com_docman&task=license_result&gid=3933&bid=3933&Itemid=429");

    List<NameValuePair> lot = new ArrayList<NameValuePair>();
    lot.add(new BasicNameValuePair("option", "com_docman"));
    lot.add(new BasicNameValuePair("task", "license_result"));
    lot.add(new BasicNameValuePair("gid", "3933"));
    lot.add(new BasicNameValuePair("bid", "3933"));
    lot.add(new BasicNameValuePair("Itemid", "429"));

    post.setEntity(new UrlEncodedFormEntity(lot));
    HttpResponse str = null;//from  w w w.j  a  v a  2 s. c o m

    try {
        str = http.execute(post);

        InputStream in = str.getEntity().getContent();

        OutputStream outputStream = new FileOutputStream(
                new File("C:\\Users\\AbhinavPatluri\\Desktop\\Excelsheet.csv"));

        int read = 0;
        byte[] bytes = new byte[1024];

        while ((read = in.read(bytes)) != -1) {
            outputStream.write(bytes, 0, read);
        }

    } catch (Exception e) {

        e.printStackTrace();
    }

}