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:forplay.android.AndroidNet.java

private void doHttp(boolean isPost, String url, String data, Callback callback) {
    // TODO: use AsyncTask
    HttpClient httpclient = new DefaultHttpClient();
    HttpRequestBase req = null;/*from  w  w  w.  j a v  a2s  .c  o  m*/
    if (isPost) {
        HttpPost httppost = new HttpPost(url);
        if (data != null) {
            try {
                httppost.setEntity(new StringEntity(data));
            } catch (UnsupportedEncodingException e) {
                // TODO Auto-generated catch block
                callback.failure(e);
            }
        }
        req = httppost;
    } else {
        req = new HttpGet(url);
    }
    try {
        HttpResponse response = httpclient.execute(req);
        callback.success(EntityUtils.toString(response.getEntity()));
    } catch (Exception e) {
        callback.failure(e);
    }
}

From source file:org.digidoc4j.impl.bdoc.SKOcspDataLoader.java

@Override
public byte[] post(final String url, final byte[] content) throws DSSException {
    logger.info("Getting OCSP response from " + url);

    HttpPost httpRequest = null;//from  w ww .  jav  a  2  s.c  om
    HttpResponse httpResponse = null;

    try {
        final URI uri = URI.create(url.trim());
        httpRequest = new HttpPost(uri);
        httpRequest.setHeader("User-Agent", userAgent);

        // The length for the InputStreamEntity is needed, because some receivers (on the other side) need this information.
        // To determine the length, we cannot read the content-stream up to the end and re-use it afterwards.
        // This is because, it may not be possible to reset the stream (= go to position 0).
        // So, the solution is to cache temporarily the complete content data (as we do not expect much here) in a byte-array.
        final ByteArrayInputStream bis = new ByteArrayInputStream(content);

        final HttpEntity httpEntity = new InputStreamEntity(bis, content.length);
        final HttpEntity requestEntity = new BufferedHttpEntity(httpEntity);
        httpRequest.setEntity(requestEntity);
        if (contentType != null) {
            httpRequest.setHeader(CONTENT_TYPE, contentType);
        }

        httpResponse = getHttpResponse(httpRequest, url);

        final byte[] returnedBytes = readHttpResponse(url, httpResponse);
        return returnedBytes;
    } catch (IOException e) {
        throw new DSSException(e);
    } finally {
        if (httpRequest != null) {
            httpRequest.releaseConnection();
        }
        if (httpResponse != null) {
            EntityUtils.consumeQuietly(httpResponse.getEntity());
        }
    }
}

From source file:com.jubination.io.chatbot.backend.service.core.DashBotUpdater.java

public String sendAutomatedUpdate(DashBot dashbot, String type) {
    String responseText = "";
    try {//from   ww w .  jav  a 2  s .  co  m
        String url = "https://tracker.dashbot.io/track?platform=generic&v=0.8.2-rest&type=" + type
                + "&apiKey=bJt7U0oEG79HSUm4nJWUQTPm1nhjKu3gieZ83M0O";
        ObjectMapper mapper = new ObjectMapper();
        //Object to JSON in String
        String jsonString = mapper.writeValueAsString(dashbot);
        HttpClient httpClient = HttpClientBuilder.create().build();
        // System.out.println(jsonString+"STRING JSON TO DASH BOT");
        StringEntity requestEntity = new StringEntity(jsonString, ContentType.APPLICATION_JSON);
        HttpPost postMethod = new HttpPost(url);
        postMethod.setEntity(requestEntity);
        HttpResponse response = httpClient.execute(postMethod);
        HttpEntity entity = response.getEntity();
        responseText = EntityUtils.toString(entity, "UTF-8");

    } catch (Exception ex) {
        Logger.getLogger(DashBotUpdater.class.getName()).log(Level.SEVERE, null, ex);
    }
    // System.out.println(responseText);
    return responseText;
}

From source file:org.apache.olingo.client.core.http.DefaultHttpUriRequestFactory.java

@Override
public HttpUriRequest create(final HttpMethod method, final URI uri) {
    HttpUriRequest result;//from   www  .  j a  v a  2s.com

    switch (method) {
    case POST:
        result = new HttpPost(uri);
        break;

    case PUT:
        result = new HttpPut(uri);
        break;

    case PATCH:
        result = new HttpPatch(uri);
        break;

    case MERGE:
        result = new HttpMerge(uri);
        break;

    case DELETE:
        result = new HttpDelete(uri);
        break;

    case GET:
    default:
        result = new HttpGet(uri);
        break;
    }

    return result;
}

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);/*  w  w  w  .ja v a2 s . c om*/
    HttpClient client = ApacheHttpClientUtil.getHttpClient(serverAddress, username, password);
    HttpResponse httpresponse = client.execute(post);
    return httpresponse;
}

From source file:com.continuuity.loom.TestHelper.java

public static void finishTask(String loomUrl, FinishTaskRequest finishRequest) throws Exception {
    HttpPost httpPost = new HttpPost(String.format("%s/v1/loom/tasks/finish", loomUrl));
    httpPost.setEntity(new StringEntity(GSON.toJson(finishRequest)));

    CloseableHttpClient httpClient = HttpClients.createDefault();
    CloseableHttpResponse response = httpClient.execute(httpPost);
    try {/*  ww w.jav a 2s.c  o  m*/
        Assert.assertEquals(200, response.getStatusLine().getStatusCode());
    } finally {
        response.close();
    }
}

From source file:com.strato.hidrive.api.connection.httpgateway.request.InputStreamPostRequest.java

@Override
protected HttpRequestBase createHttpRequest(String requestUri, HttpRequestParamsVisitor<?> visitor)
        throws UnsupportedEncodingException {
    HttpPost httpPost = new HttpPost(requestUri + visitor.getHttpRequestParams());
    httpPost.setEntity(new InputStreamEntity(getInputStream(), getStreamLength()));
    return httpPost;
}

From source file:com.survivingwithandroid.jsontutorial.HttpClient.java

public String postJsonData(String data) {

    try {//from   w  w w. j  a v  a  2  s. c  o  m
        StringBuffer buffer = new StringBuffer();
        // Apache HTTP Reqeust
        System.out.println("Sending data..");
        System.out.println("Data: [" + data + "]");
        org.apache.http.client.HttpClient client = new DefaultHttpClient();
        HttpPost post = new HttpPost(URL);
        List<NameValuePair> nvList = new ArrayList<NameValuePair>();
        BasicNameValuePair bnvp = new BasicNameValuePair("json", data);
        // We can add more
        nvList.add(bnvp);
        post.setEntity(new UrlEncodedFormEntity(nvList));

        HttpResponse resp = client.execute(post);
        // We read the response
        InputStream is = resp.getEntity().getContent();
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        StringBuilder str = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
            str.append(line + "\n");
        }
        is.close();
        buffer.append(str.toString());
        // Done!

        return buffer.toString();
    } catch (Throwable t) {
        t.printStackTrace();
    }

    return null;
}

From source file:att.jaxrs.client.Content.java

public static String addContent(Content content) {
    List<NameValuePair> urlParameters = new ArrayList<NameValuePair>();
    urlParameters.add(new BasicNameValuePair("content_id", content.getContent_id().toString()));
    urlParameters.add(new BasicNameValuePair("level", content.getLevel()));
    urlParameters.add(new BasicNameValuePair("presenter", content.getPresenter()));
    urlParameters.add(new BasicNameValuePair("reads", content.getReads()));

    String resultStr = "";

    try {/*from   w  ww. j  a  v a  2 s.c  o  m*/

        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost post = new HttpPost(Constants.INSERT_CONTENT_RESOURCE);
        post.setEntity(new UrlEncodedFormEntity(urlParameters));
        HttpResponse result = httpClient.execute(post);
        System.out.println(Constants.RESPONSE_STATUS_CODE + result);
        resultStr = Util.getStringFromInputStream(result.getEntity().getContent());
        System.out.println(Constants.RESPONSE_BODY + resultStr);

    } catch (Exception e) {
        System.out.println(e.getMessage());

    }
    return resultStr;
}

From source file:org.wso2.iot.refarch.rpi.agent.connector.HttpService.java

public void sendPayload(JSONObject data) throws IOException, ExecutionException, InterruptedException {

    JSONObject dataObj = new JSONObject();
    dataObj.put("data", data);

    HttpClient client = HttpClientBuilder.create().build();
    System.out.println("Created HTTP Client");
    HttpPost post = new HttpPost(address);
    post.setHeader("content-type", "application/json");
    BasicHttpEntity he = new BasicHttpEntity();
    he.setContent(new ByteArrayInputStream(dataObj.toString().getBytes()));
    post.setEntity(he);/*from w  w w  .j av  a 2  s. c om*/
    client.execute(post);
    System.out.println("Payload sent");
}