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:core.RESTCalls.RESTPost.java

public static String httpPost(String urlStr, String data) throws Exception {

    String result = null;//  w  ww .  j av a2 s. co m

    HttpClient client = new DefaultHttpClient();

    HttpPost post = new HttpPost(urlStr);

    StringEntity input = new StringEntity(data);

    post.setEntity(input);

    HttpResponse response = client.execute(post);

    if (response != null && response.getEntity() != null) {

        BufferedReader r = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

        StringBuilder total = new StringBuilder();

        String line = null;

        while ((line = r.readLine()) != null)
            total.append(line + "\n");

        total.toString();
    }

    return result;
}

From source file:org.opencastproject.remotetest.server.resource.IngestResources.java

public static HttpResponse ingest(TrustedHttpClient client, String mediaPackageId) throws Exception {
    HttpPost post = new HttpPost(getServiceUrl() + "ingest");
    List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
    params.add(new BasicNameValuePair("mediaPackage", mediaPackageId));
    post.setEntity(new UrlEncodedFormEntity(params));
    return client.execute(post);
}

From source file:Main.java

public static String openPostConnection(String urlString, Map<String, String> map) {
    HttpClient client = new DefaultHttpClient();
    HttpPost post = new HttpPost(urlString);
    List<NameValuePair> nameValuePairs = new ArrayList<>();
    for (String key : map.keySet()) {
        nameValuePairs.add(new BasicNameValuePair(key, map.get(key)));
    }/*from www. j  a  v a 2  s . c o m*/
    try {
        post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
    try {
        HttpResponse httpResponse = client.execute(post);
        BufferedReader br = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent()));
        StringBuilder json = new StringBuilder("");
        String line = null;
        while ((line = br.readLine()) != null) {
            json.append(line);
        }
        return json.toString();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

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.// www  .j a v a  2s . c o m
 * 
 * @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:ai.serotonin.haystack.validator.Source.java

/**
 * Read a remote database via the Project-Haystack protocol.
 * //from   ww w . j a  va  2 s.  c o m
 * This method currently does not support authentication.
 * 
 * @param endpoint
 * @return the list of rows returned
 * @throws Exception
 */
public static List<HMap> remote(String endpoint) throws Exception {
    String filter = "id";
    //int limit = 10000;

    HMap map = new HMap().put("filter", filter); //.put("limit", limit);
    String entityStr = ZincWriter.gridToString(new HGrid(map));

    HttpClient client = HttpClientBuilder.create().build();
    HttpPost post = new HttpPost(endpoint + "read");
    // Set the auth as required.
    StringEntity entity = new StringEntity(entityStr, ContentType.TEXT_PLAIN);
    post.setEntity(entity);

    String responseStr = HttpUtils4.getTextContent(client, post, 1);
    HGrid response = new ZincReader(responseStr).readGrid();
    return response.getRows();
}

From source file:org.wuspba.ctams.ui.server.ServerUtils.java

public static String post(URI uri, String xml) throws IOException {
    CloseableHttpClient httpclient = HttpClients.createDefault();
    HttpPost httpPost = new HttpPost(uri);
    StringEntity xmlEntity = new StringEntity(xml, ContentType.APPLICATION_XML);

    String ret;/*from w w w . j av  a2s  .com*/

    httpPost.setEntity(xmlEntity);

    try (CloseableHttpResponse response = httpclient.execute(httpPost)) {
        HttpEntity entity = response.getEntity();

        ret = convertEntity(entity);

        EntityUtils.consume(entity);
    }

    return ret;
}

From source file:org.opencastproject.remotetest.server.resource.FilesResources.java

public static HttpResponse postFile(TrustedHttpClient client, String mediaPackageID,
        String mediaPackageElementID, String media) throws Exception {
    HttpPost post = new HttpPost(
            getServiceUrl() + "mediapackage/" + mediaPackageElementID + "/" + mediaPackageElementID);
    MultipartEntity entity = new MultipartEntity();
    entity.addPart("file", new FileBody(new File(media)));
    post.setEntity(entity);
    return client.execute(post);
}

From source file:org.ow2.bonita.facade.rest.apachehttpclient.ApacheHttpClientUtil.java

public static HttpResponse executeHttpConnection(String uri, byte[] content, String optionsHeader,
        String username, String password)
        throws URISyntaxException, IOException, IOException, ClassNotFoundException {
    String serverAddress = HttpRESTUtil.getRESTServerAddress();
    HttpPost post = new HttpPost(serverAddress + uri);

    ByteArrayEntity entity = new ByteArrayEntity(content);
    entity.setContentType("application/octet-stream");
    post.setHeader("options", optionsHeader);
    post.setEntity(entity);

    HttpClient client = ApacheHttpClientUtil.getHttpClient(serverAddress, username, password);
    HttpResponse httpresponse = client.execute(post);
    return httpresponse;
}

From source file:com.fishcart.delivery.util.HttpClient.java

public static String postWhatsapp(String url, String nos, String message) {
    try {//from   ww w.j  ava2s.  c  o m
        CloseableHttpClient client = HttpClientBuilder.create().build();
        HttpPost post = new HttpPost(url);
        List<NameValuePair> urlParameters = new ArrayList<NameValuePair>();
        urlParameters.add(new BasicNameValuePair("nos", nos));
        urlParameters.add(new BasicNameValuePair("message", message));
        post.setEntity(new UrlEncodedFormEntity(urlParameters));
        HttpResponse response = client.execute(post);
        BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

        StringBuilder result = new StringBuilder();
        String line = "";
        while ((line = rd.readLine()) != null) {
            result.append(line);
        }
        return result.toString();
    } catch (IOException ex) {
        throw new RuntimeException(ex);
    }

}

From source file:com.quietlycoding.android.reader.util.api.Authentication.java

/**
 * This method returns back to the caller a proper authentication token to
 * use with the other API calls to Google Reader.
 * /*  w  w w  .j  a  v a2 s. c  o m*/
 * @param user
 *            - the Google username
 * @param pass
 *            - the Google password
 * @return sid - the returned authentication token for use with the API.
 * 
 */
public static String getAuthToken(String user, String pass) {
    final NameValuePair username = new BasicNameValuePair("Email", user);
    final NameValuePair password = new BasicNameValuePair("Passwd", pass);
    final NameValuePair service = new BasicNameValuePair("service", "reader");
    final List<NameValuePair> pairs = new ArrayList<NameValuePair>();
    pairs.add(username);
    pairs.add(password);
    pairs.add(service);

    try {
        final DefaultHttpClient client = new DefaultHttpClient();
        final HttpPost post = new HttpPost(AUTH_URL);
        final UrlEncodedFormEntity entity = new UrlEncodedFormEntity(pairs);

        post.setEntity(entity);

        final HttpResponse response = client.execute(post);
        final HttpEntity respEntity = response.getEntity();

        Log.d(TAG, "Server Response: " + response.getStatusLine());

        final InputStream in = respEntity.getContent();
        final BufferedReader reader = new BufferedReader(new InputStreamReader(in));
        String line = null;
        String result = null;

        while ((line = reader.readLine()) != null) {
            if (line.startsWith("SID")) {
                result = line.substring(line.indexOf("=") + 1);
            }
        }

        reader.close();
        client.getConnectionManager().shutdown();

        return result;
    } catch (final Exception e) {
        Log.d(TAG, "Exception caught:: " + e.toString());
        return null;
    }
}