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.nominanuda.web.http.SerializeDeserializeTest.java

@Test
public void testRequest() throws IOException, HttpException {
    HttpPost req = new HttpPost(REQ_URI);
    req.addHeader("h1", "v1");
    req.setEntity(new StringEntity(PAYLOAD, ContentType.create("text/plain", "UTF-8")));
    byte[] serialized = HTTP.serialize(req);
    //System.err.println(new String(serialized, "UTF-8"));
    HttpPost m = (HttpPost) HTTP.deserialize(new ByteArrayInputStream(serialized));
    //System.err.println(new String(HTTP.serialize(m), "UTF-8"));
    assertEquals(REQ_URI, m.getRequestLine().getUri());
    assertEquals(PAYLOAD.getBytes(HttpProtocol.CS_UTF_8).length,
            ((ByteArrayEntity) m.getEntity()).getContentLength());
    assertEquals("v1", m.getFirstHeader("h1").getValue());
}

From source file:de.avanux.android.livetracker2.HttpUtil.java

public static String post(String url, Map<String, String> httpParameters)
        throws ClientProtocolException, IOException {
    Log.d(TAG, "HTTP POST " + url);
    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(httpParameters.size());
    Set<String> httpParameterKeys = httpParameters.keySet();
    for (String httpParameterKey : httpParameterKeys) {
        nameValuePairs.add(new BasicNameValuePair(httpParameterKey, httpParameters.get(httpParameterKey)));
    }/* ww w. j av a2 s . c  om*/

    HttpPost method = new HttpPost(url);
    method.setEntity(new UrlEncodedFormEntity(nameValuePairs));
    HttpResponse response = executeMethod(method);
    return getResponseAsString(response);
}

From source file:bova.rtmapi.RestClient.java

JSONResponse execute() throws ServerException, RtmApiException, IOException {
    // Execute HTTP Post Request
    HttpClient httpclient = new DefaultHttpClient();
    URI uri;//from ww w  . j  a  va 2 s .c  om
    try {
        uri = new URI(this.request.getUrl());
        HttpPost httppost = new HttpPost(uri);
        HttpResponse response = httpclient.execute(httppost);
        // Get string from response
        InputStream is = response.getEntity().getContent();
        try {
            StringBuilder sb = new StringBuilder();
            BufferedReader r = new BufferedReader(new InputStreamReader(new DoneHandlerInputStream(is)));
            for (String line = r.readLine(); line != null; line = r.readLine()) {
                sb.append(line);
            }
            // get JSON Response from string and return it
            return new JSONResponse(sb.toString());
        } finally {
            is.close();
        }
    } catch (URISyntaxException e) {
        throw new RtmApiException(e.getMessage());
    } catch (ClientProtocolException e) {
        throw new RtmApiException(e.getMessage());
    }
}

From source file:org.jenkinsci.plugins.fabric8.support.HubotClient.java

public static String notify(TaskListener listener, String room, String message) throws java.io.IOException {
    log(listener, "Hubot sending to room " + room + " => " + message);
    if (Strings.isNullOrBlank(room)) {
        log(listener, "you must specify a room!");
        return null;
    }//w  ww .  ja  va 2 s  .  co  m
    if (Strings.isNullOrBlank(message)) {
        log(listener, "you must specify a message!");
        return null;
    }
    HttpHost host = RestClient.getHttpHost(listener, "hubot");
    if (host != null) {
        String roomWithSlugPrefix = room;
        if (roomWithSlugPrefix.startsWith("#")) {
            roomWithSlugPrefix = roomWithSlugPrefix.substring(1);
        }
        if (!roomWithSlugPrefix.startsWith(ROOM_SLUG_PREFIX)) {
            roomWithSlugPrefix = ROOM_SLUG_PREFIX + roomWithSlugPrefix;
        }
        HttpPost post = new HttpPost("/hubot/notify/" + roomWithSlugPrefix);
        log(listener, "Posting to " + host.toString() + " url: " + post.getURI());

        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
        nameValuePairs.add(new BasicNameValuePair("message", message));
        post.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        CloseableHttpResponse response = RestClient.executeRequest(host, post);
        String result = RestClient.parse(response);
        log(listener, "Result: " + result);
        return result;
    } else {
        log(listener, "No service found!");
        return null;
    }
}

From source file:com.nibss.util.Request.java

public void post(String url, List<NameValuePair> list) throws Exception {
    CloseableHttpClient httpclient = HttpClients.createDefault();
    try {/*  www  .  j a  v  a2  s. c  o  m*/
        HttpPost httpPost = new HttpPost(url);

        httpPost.setEntity(new UrlEncodedFormEntity(list));
        CloseableHttpResponse response2 = httpclient.execute(httpPost);
        try {
            System.out.println(response2.getStatusLine().getStatusCode());
            HttpEntity entity2 = response2.getEntity();

            EntityUtils.consume(entity2);
        } finally {
            response2.close();
        }
    } finally {
        httpclient.close();
    }
}

From source file:im.dadoo.logger.client.impl.DefaultLoggerClient.java

public DefaultLoggerClient(String host) {
    this.httpClient = HttpAsyncClients.createDefault();
    this.post = new HttpPost(host);
    this.post.setHeader(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
    this.mapper = new ObjectMapper();
}

From source file:com.msopentech.odatajclient.engine.client.http.DefaultHttpUriRequestFactory.java

@Override
public HttpUriRequest createHttpUriRequest(final HttpMethod method, final URI uri) {
    HttpUriRequest result;//from  ww  w.  java  2s . c  o m

    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:hpcc.hut.edu.vn.ocr.ocrserviceconnector.HpccOcrServiceConnector.java

public static String postToOcrService(byte[] data, int size, int imgw, int imgh, String lang, int psm,
        boolean isPrePostProcess) {
    System.out.println("Sent data: w = " + imgw + ", h = " + imgh + ", psm = " + psm + ", process: "
            + isPrePostProcess + ", with lang: " + lang);
    String result = "";

    try {//ww w .j  a v a 2  s.com
        HttpParams httpParamenters = new BasicHttpParams();
        HttpConnectionParams.setConnectionTimeout(httpParamenters, 30000);
        DefaultHttpClient httpClient = new DefaultHttpClient(httpParamenters);
        HttpPost postRequest = new HttpPost(HOST);

        ByteArrayBody bab = new ByteArrayBody(data, "input.jpg");
        MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
        reqEntity.addPart("image", bab);
        reqEntity.addPart("size", new StringBody("" + size)); // size to
        // check if
        // decompress
        // fail
        reqEntity.addPart("width", new StringBody("" + imgw));
        reqEntity.addPart("height", new StringBody("" + imgh));
        reqEntity.addPart("lang", new StringBody(lang));
        reqEntity.addPart("psm", new StringBody("" + psm));
        reqEntity.addPart("process", new StringBody("" + isPrePostProcess));

        postRequest.setEntity(reqEntity);

        HttpResponse response = httpClient.execute(postRequest);
        BufferedReader reader = new BufferedReader(
                new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
        String sResponse;
        StringBuilder s = new StringBuilder();
        while ((sResponse = reader.readLine()) != null) {
            s = s.append(sResponse);
        }
        result = s.toString();
        System.out.println("result in Json: " + result);
    } catch (Exception e) {
        // handle exception here
        Log.e(e.getClass().getName(), e.getMessage());
        return null;
    }
    return result;

}

From source file:juzu.plugin.jackson.AbstractJacksonRequestTestCase.java

@Test
public void testRequest() throws Exception {
    HttpPost post = new HttpPost(applicationURL() + "/post");
    post.setEntity(new StringEntity("{\"foo\":\"bar\"}", ContentType.APPLICATION_JSON));
    HttpClient client = HttpClientBuilder.create().build();
    payload = null;//from   ww w.j av a2  s . c  om
    client.execute(post);
}

From source file:org.dataconservancy.ui.it.support.ListProjectActivityRequest.java

public HttpPost asHttpPost(String projectId) {

    HttpPost post = null;//from  ww w  .  j  a  v  a 2 s. c  o m
    try {
        post = new HttpPost(urlConfig.getListProjectCollectionsUrl().toURI());
    } catch (URISyntaxException e) {
        throw new RuntimeException(e.getMessage(), e);
    }

    List<NameValuePair> params = new ArrayList<NameValuePair>();
    params.add(new BasicNameValuePair("currentProjectId", projectId));
    params.add(new BasicNameValuePair(STRIPES_EVENT, "List Project Activity"));

    HttpEntity entity = null;
    try {
        entity = new UrlEncodedFormEntity(params, "UTF-8");
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException(e.getMessage(), e);
    }

    post.setEntity(entity);

    return post;
}