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.terracotta.management.cli.rest.PostCommand.java

private void doPost(Context context) throws IOException, CommandInvocationException {

    HttpClient httpclient = HttpServices.getHttpClient();
    HttpPost httpPost = new HttpPost(context.getUrl());

    if (context.getData() != null) {
        httpPost.setHeader(new BasicHeader("Content-Type", "application/x-www-form-urlencoded"));
        httpPost.setEntity(new StringEntity(context.getData()));
    }/*from  ww w  .  j  ava  2  s.  c om*/
    HttpResponse response = httpclient.execute(httpPost);
    processEntity(response.getEntity(), response.getFirstHeader("Content-Type"), context);

}

From source file:nl.igorski.lib.utils.network.HTTPTransfer.java

/**
 * quick wrapper to POST data to a server
 *
 * @param aURL    {String} URL of the server
 * @param aParams {List<NameValuePair>} optional list of parameters to send
        /*from  ww w  .j  a v a2 s .  co m*/
 * @return {HttpResponse}
 */
public static HttpResponse post(String aURL, List<NameValuePair> aParams) {
    HttpPost post = new HttpPost(urlEncode(aURL, null));

    post.getParams().setBooleanParameter("http.protocol.expect-continue", false);

    MultipartEntity entity = new MultipartEntity();

    for (NameValuePair pair : aParams) {
        try {
            entity.addPart(pair.getName(), new StringBodyNoHeaders(pair.getValue()));
        } catch (UnsupportedEncodingException e) {
        }
    }
    post.setEntity(entity);

    HttpResponse out = null;

    try {
        out = getClient().execute(post);
    } catch (Exception e) {
    }

    return out;
}

From source file:com.dtstack.jlogstash.distributed.http.cilent.HttpClient.java

public static String post(String url, Map<String, Object> bodyData) {
    String responseBody = null;//from  ww w . j a va 2 s  . c  o  m
    CloseableHttpClient httpClient = null;
    try {
        httpClient = getHttpClient();
        HttpPost httPost = new HttpPost(url);
        if (SetTimeOut) {
            RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(SocketTimeout)
                    .setConnectTimeout(ConnectTimeout).build();//
            httPost.setConfig(requestConfig);
        }
        if (bodyData != null && bodyData.size() > 0) {
            httPost.setEntity(new StringEntity(objectMapper.writeValueAsString(bodyData)));
        }
        //?
        CloseableHttpResponse response = httpClient.execute(httPost);
        int status = response.getStatusLine().getStatusCode();
        if (status == HttpStatus.SC_OK) {
            HttpEntity entity = response.getEntity();
            //FIXME ?header?
            responseBody = EntityUtils.toString(entity, Charsets.UTF_8);
        } else {
            logger.error("url:" + url + "--->http return status error:" + status);
        }
    } catch (Exception e) {
        logger.error("url:" + url + "--->http request error", e);
    } finally {
        try {
            if (httpClient != null)
                httpClient.close();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            logger.error(ExceptionUtil.getErrorMessage(e));
        }
    }
    return responseBody;
}

From source file:azureml_besapp.AzureML_BESApp.java

/**
 * Call REST API to submit job to Azure ML for batch predictions
 * @return response from the REST API//  w w  w  .j  av a2 s  .c  om
 */
public static String besHttpPost() {

    HttpPost post;
    HttpClient client;
    StringEntity entity;

    try {
        // create HttpPost and HttpClient object
        post = new HttpPost(apiurl);
        client = HttpClientBuilder.create().build();

        // setup output message by copying JSON body into 
        // apache StringEntity object along with content type
        entity = new StringEntity(jsonBody, HTTP.UTF_8);
        entity.setContentEncoding(HTTP.UTF_8);
        entity.setContentType("text/json");

        // add HTTP headers
        post.setHeader("Accept", "text/json");
        post.setHeader("Accept-Charset", "UTF-8");

        // set Authorization header based on the API key
        post.setHeader("Authorization", ("Bearer " + apikey));
        post.setEntity(entity);

        // Call REST API and retrieve response content
        HttpResponse authResponse = client.execute(post);

        jobId = EntityUtils.toString(authResponse.getEntity()).replaceAll("\"", "");

        return jobId;

    } catch (Exception e) {

        return e.toString();
    }

}

From source file:io.kahu.hawaii.util.call.http.PostRequest.java

public PostRequest(RequestPrototype<HttpResponse, T> prototype, URI uri) {
    super(prototype, new HttpPost(uri));
}

From source file:geotag.core.HttpHelper.java

public static HttpHelper getInstance(String url) {
    if (helperInstance == null) {
        helperInstance = new HttpHelper(url);
    } else {// ww w.j a v a 2  s.c  o m
        clientPostRequest = new HttpPost(url);
        serverURL = url;
    }

    return helperInstance;
}

From source file:com.arrow.acs.client.api.ClientUserApi.java

public UserModel authenticate(String username, String password) {
    String method = "authenticate";
    try {//from ww  w . jav  a  2  s.co m
        URI uri = buildUri(USERS_ROOT_URL + "/auth");
        UserAuthenticationModel model = new UserAuthenticationModel().withUsername(username)
                .withPassword(password);
        UserModel result = execute(new HttpPost(uri), JsonUtils.toJson(model), UserModel.class);
        if (result != null && isDebugEnabled())
            logDebug(method, "hid: %s, login: %s", result.getHid(), result.getLogin());
        return result;
    } catch (Throwable t) {
        throw handleException(t);
    }
}

From source file:com.asposewords.restfulapi.HttpClientClass.java

public String httpPostMethod(String fileName) {
    HttpClient client = new DefaultHttpClient();
    List<NameValuePair> nameValuePairs;
    HttpPost post = null;//from w w  w .  j  a  v a2s  .  com
    String strResponse = "", line = "";
    post = new HttpPost("http://localhost:8080/AsposeWords/rest/words/protection");

    try {
        nameValuePairs = new ArrayList<NameValuePair>(1);
        nameValuePairs.add(new BasicNameValuePair("src", fileName));
        post.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        HttpResponse response = client.execute(post);
        BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

        while ((line = rd.readLine()) != null) {
            if (line != null) {
                strResponse = "" + line;
            }

        }

    } catch (IOException e) {
        e.printStackTrace();
    }
    return strResponse;
}

From source file:edu.isi.karma.util.HTTPUtil.java

public static String executeHTTPPostRequest(String serviceURL, String contentType, String acceptContentType,
        HttpEntity entity) throws ClientProtocolException, IOException {
    HttpPost httpPost = new HttpPost(serviceURL);
    httpPost.setEntity(entity);/*from   www  .  j a v  a2  s  . com*/
    return invokeHTTPRequest(httpPost, contentType, acceptContentType);
}

From source file:org.jitsi.meet.test.util.JvbUtil.java

static private void triggerShutdown(HttpClient client, String jvbEndpoint, boolean force) throws IOException {
    String url = jvbEndpoint + "/colibri/shutdown";

    HttpPost post = new HttpPost(url);

    StringEntity requestEntity = new StringEntity(
            force ? "{ \"force-shutdown\": \"true\" }" : "{ \"graceful-shutdown\": \"true\" }",
            ContentType.APPLICATION_JSON);

    post.setEntity(requestEntity);//from   ww  w  .j  av  a2  s  .c o  m

    System.out.println("\nSending 'POST' request to URL : " + url);
    System.out.println("Post parameters : " + post.getEntity());

    HttpResponse response = client.execute(post);

    int responseCode = response.getStatusLine().getStatusCode();
    if (200 != responseCode) {
        throw new RuntimeException(
                "Failed to trigger graceful shutdown on: " + jvbEndpoint + ", response code: " + responseCode);
    }
}