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:com.ericsson.gerrit.plugins.syncindex.HttpSession.java

IndexResult post(String endpoint) throws IOException {
    return httpClient.execute(new HttpPost(url + endpoint), new IndexResponseHandler());
}

From source file:kcb.billerengine.processors.CallJSON.java

private String getJson(HttpClient httpClient, String jsonURL, String body) {
    String sb = null;/* w  w w.j  a va 2 s.co m*/
    try {
        System.out.println("JSON REQUEST: " + body);
        StringEntity params = new StringEntity(body);

        HttpPost request = new HttpPost(jsonURL);
        request.addHeader("content-type", "application/text");
        request.setEntity(params);
        HttpResponse response = httpClient.execute(request);

        sb = EntityUtils.toString(response.getEntity(), "UTF-8");

    } catch (IOException | ParseException e) {
        e.printStackTrace();
    }
    System.out.println("ECITIZEN RESPONSE : " + sb);
    httpClient.getConnectionManager().shutdown();
    return sb;
}

From source file:mobi.salesforce.client.upload.DemoFileUploader.java

public void executeMultiPartRequest(String urlString, File file, String fileName, String fileDescription)
        throws Exception {
    HttpClient client = new DefaultHttpClient();
    HttpPost postRequest = new HttpPost(urlString);
    try {//from   www .ja  va  2s .co  m
        // Set various attributes
        MultipartEntity multiPartEntity = new MultipartEntity();
        multiPartEntity.addPart("fileDescription",
                new StringBody(fileDescription != null ? fileDescription : ""));
        multiPartEntity.addPart("fileName", new StringBody(fileName != null ? fileName : file.getName()));

        FileBody fileBody = new FileBody(file, "application/octect-stream");
        // Prepare payload
        multiPartEntity.addPart("attachment", fileBody);

        // Set to request body
        postRequest.setEntity(multiPartEntity);

        // Send request
        HttpResponse response = client.execute(postRequest);

        // Verify response if any
        if (response != null) {
            System.out.println(response.getStatusLine().getStatusCode());
        }
    } catch (Exception ex) {
    }
}

From source file:hobbyshare.testclient.RestService_TestClient.java

public void test_Login() {
    HttpClient httpClient = HttpClientBuilder.create().build();

    JSONObject loginAttempt = new JSONObject();
    loginAttempt.put("password", "1234");
    loginAttempt.put("userName", "77_username");

    try {//from   w  w w . ja va 2s  .  com
        HttpPost request = new HttpPost("http://localhost:8095/login");
        StringEntity params = new StringEntity(loginAttempt.toString());
        request.addHeader("content-type", "application/json");
        request.setEntity(params);
        HttpResponse response = httpClient.execute(request);

        System.out.println("---Testing Login----");
        System.out.println(response.toString());
        HttpEntity entity = response.getEntity();
        String responseString = EntityUtils.toString(entity, "UTF-8");
        System.out.println(responseString);
        System.out.println("----End of login Test----");
    } catch (Exception ex) {
        // handle exception here
    } finally {
        httpClient.getConnectionManager().shutdown();
    }
}

From source file:de.pubflow.server.core.restConnection.WorkflowSender.java

/**
 * Sends a post request to the Workflow engine to use a certain Workflow
 * specified through the given path./*from  w  w w.  j  a  v  a 2 s  .com*/
 * 
 * @param wfCall
 *            The message with all necessary informations to create a new
 *            Workflow
 * @param workflowPath
 *            The path for the specific Workflow to be used.
 * @throws WFRestException
 *             if the connection responses with a HTTP response code other
 *             than 2xx
 */
public static void initWorkflow(WorkflowRestCall wfCall, String workflowPath) throws WFRestException {
    Logger myLogger = LoggerFactory.getLogger(WorkflowSender.class);

    myLogger.info("Trying to use workflow on: " + workflowPath);
    Gson gson = new Gson();
    HttpClient httpClient = HttpClientBuilder.create().build();
    HttpPost post = new HttpPost(workflowPath);
    HttpResponse response = null;

    try {
        StringEntity postingString = new StringEntity(gson.toJson(wfCall), ContentType.APPLICATION_JSON);

        post.setEntity(postingString);
        post.setHeader("Content-type", "application/json;charset=utf-8");
        response = httpClient.execute(post);
        System.out.println(post.getURI());
        myLogger.info("Http response: " + response.toString());

    } catch (Exception e) {
        myLogger.error("Could not deploy new Workflow with ID: " + wfCall.getID());
        myLogger.error(e.toString());
        throw new WFRestException("Workflow could not be started");
    }
    if (response.getStatusLine().getStatusCode() >= 300) {
        throw new WFRestException(
                "The called WorkflowService send status code: " + response.getStatusLine().getStatusCode()
                        + " and error: " + response.getStatusLine().getReasonPhrase());
    }

}

From source file:com.net.plus.common.http.transport.PostBytesHttpTransport.java

public byte[] send(HttpClient httpClient, Object obj) {
    byte[] bytes = (byte[]) obj;
    HttpPost post = new HttpPost(getSendUrl());
    ByteArrayEntity entity = new ByteArrayEntity(bytes, ContentType.create(mimeType, charset));
    entity.setChunked(true);/*from w  ww  .j ava2  s  .  c om*/
    post.setEntity(entity);
    try {
        byte[] response = httpClient.execute(post, resHandler);
        return response;
    } catch (ConnectException ce) {
        post.abort();
        log.error("Http transport error." + getSendUrl().toString(), ce);
        /*CommunicationException cce = new  CommunicationException("pe.connect_failed");
        cce.setDefaultMessage(getSendUrl().toString());
        throw cce;*/
    } catch (Exception ex) {
        post.abort();
        /*log.error("Http transport error."+getSendUrl().toString(), ex);
        throw new CommunicationException("pe.error.undefined", ex);*/
    }
    return null;
}

From source file:com.vmware.identity.rest.core.client.RequestFactory.java

public static HttpPost createPostRequest(URI uri, AccessToken token)
        throws ClientException, JsonProcessingException {
    return prepareRequest(new HttpPost(uri), token, null);
}

From source file:language_engine.HttpUtils.java

public static String doHttpPost(String url, List<NameValuePair> params) {
    try {//  w  w w.j ava  2  s  .co  m
        HttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url);
        httpPost.setEntity(new UrlEncodedFormEntity(params, "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:util.servlet.PaypalListenerServlet.java

@Override
protected void service(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    HttpClient client = new DefaultHttpClient();
    HttpPost post = new HttpPost(Constants.IPN_SANDBOX_ENDPOINT);
    List<NameValuePair> params = new ArrayList<>();
    params.add(new BasicNameValuePair("cmd", "_notify-validate")); //You need to add this parameter to tell PayPal to verify
    for (Enumeration<String> e = request.getParameterNames(); e.hasMoreElements();) {
        String name = e.nextElement();
        String value = request.getParameter(name);
        params.add(new BasicNameValuePair(name, value));
    }//  ww  w. j  av  a2  s.co m
    post.setEntity(new UrlEncodedFormEntity(params));
    String rc = getRC(client.execute(post)).trim();
    if ("VERIFIED".equals(rc)) {
        //Your business code comes here
    }
}

From source file:com.flexilogix.HTTPNotifierFunction.java

public void call(Tuple5<Long, String, Float, Float, String> tweet) {
    String webserver = Properties.getString("rts.spark.webserv");
    HttpClient client = new DefaultHttpClient();
    HttpPost post = new HttpPost(webserver);
    String content = String.format("{\"id\": \"%d\", " + "\"text\": \"%s\", " + "\"pos\": \"%f\", "
            + "\"neg\": \"%f\", " + "\"score\": \"%s\" }", tweet._1(), tweet._2(), tweet._3(), tweet._4(),
            tweet._5());/*from  w w w.j  ava2  s .co m*/

    try {
        post.setEntity(new StringEntity(content));
        HttpResponse response = client.execute(post);
        org.apache.http.util.EntityUtils.consume(response.getEntity());
    } catch (Exception ex) {
        Logger LOG = Logger.getLogger(this.getClass());
        LOG.error("exception thrown while attempting to post", ex);
        LOG.trace(null, ex);
    }
}