Example usage for org.apache.commons.httpclient.methods StringRequestEntity StringRequestEntity

List of usage examples for org.apache.commons.httpclient.methods StringRequestEntity StringRequestEntity

Introduction

In this page you can find the example usage for org.apache.commons.httpclient.methods StringRequestEntity StringRequestEntity.

Prototype

public StringRequestEntity(String paramString1, String paramString2, String paramString3)
  throws UnsupportedEncodingException

Source Link

Usage

From source file:com.fusesource.demo.controller.Client.java

public static void main(String args[]) throws Exception {
    Client client = new Client();
    PostMethod post = new PostMethod("http://localhost:" + port + "/controller/process");
    post.addRequestHeader("Accept", "text/xml");
    RequestEntity entity = new StringRequestEntity(DATA, PostMethod.FORM_URL_ENCODED_CONTENT_TYPE, null);
    post.setRequestEntity(entity);/*from ww w. jav  a2  s .co m*/
    HttpClient httpclient = new HttpClient();

    try {
        System.out.println("Sending data:\n" + DATA);
        int result = httpclient.executeMethod(post);
        System.out.println("Response status code: " + result);
        System.out.println("Response body: ");
        System.out.println("Response data:\n" + post.getResponseBodyAsString());
    } finally {
        post.releaseConnection();
    }

    System.out.println("\n");
    System.exit(0);
}

From source file:com.oneapm.base.tools.UrlPostMethod.java

public static void urlPostMethod(String url, String json) {

    HttpClient httpClient = new HttpClient();
    PostMethod method = new PostMethod(url);
    try {/*  w  w w  . ja  v  a2s.  c om*/
        if (json != null && !json.trim().equals("")) {
            RequestEntity requestEntity = new StringRequestEntity(json, "application/x-www-form-urlencoded",
                    "utf-8");
            method.setRequestEntity(requestEntity);
        }
        long startTime = System.currentTimeMillis();
        int x = httpClient.executeMethod(method);
        long elapsedTime = System.currentTimeMillis();

        System.out.println(x + ":" + (elapsedTime - startTime));

    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        method.releaseConnection();
    }
}

From source file:com.pieframework.runtime.utils.CampfireNotifier.java

public static boolean notify(String msg, String url, String user, String password) {
    HttpClient client = new HttpClient();
    PostMethod post = new PostMethod(url);

    // stuff the Authorization request header
    byte[] encodedPassword = (user + ":" + password).getBytes();
    BASE64Encoder encoder = new BASE64Encoder();
    post.setRequestHeader("Authorization", "Basic " + encoder.encode(encodedPassword));

    try {/*from   w  ww .j  ava  2  s .  c o m*/
        RequestEntity entity = new StringRequestEntity("<message><body>" + msg + "</body></message>",
                "application/xml", "UTF-8");
        post.setRequestEntity(entity);
        client.executeMethod(post);
        String responseMsg = "httpStatus: " + post.getStatusCode() + " " + "Content-type: "
                + post.getResponseHeader("Content-Type") + " " + post.getResponseBodyAsString();
        Configuration.log().info(responseMsg);
    } catch (Exception e) {
        e.printStackTrace();
    }

    return true;
}

From source file:com.ebay.pulsar.collector.Simulator.java

private static void sendMessage() throws IOException, JsonProcessingException, JsonGenerationException,
        JsonMappingException, UnsupportedEncodingException, HttpException {
    ObjectMapper mapper = new ObjectMapper();

    Map<String, Object> m = new HashMap<String, Object>();

    m.put("si", "12345");
    m.put("ct", System.currentTimeMillis());

    String payload = mapper.writeValueAsString(m);
    HttpClient client = new HttpClient();
    PostMethod method = new PostMethod("http://localhost:8080/tracking/ingest/PulsarRawEvent");
    //      method.addRequestHeader("Accept-Encoding", "gzip,deflate,sdch");
    method.setRequestEntity(new StringRequestEntity(payload, "application/json", "UTF-8"));
    int status = client.executeMethod(method);
    System.out.println(Arrays.toString(method.getResponseHeaders()));
    System.out.println("Status code: " + status + ", Body: " + method.getResponseBodyAsString());
}

From source file:com.google.appsforyourdomain.provisioning.AppsUtil.java

/**
 * Posts the specified postContent to the urlString and
 * returns a JDOM Document object containing the XML response.
 * // w  w w. j  av  a 2s. co  m
 * @param urlString URL destination
 * @param postContent XML request
 * @return a JDOM Document object containing the XML response
 */
public static Document postHttpRequest(String urlString, String postContent) throws AppsForYourDomainException {
    try {

        // Send content
        final HttpClient client = new HttpClient();
        PostMethod method = new PostMethod(urlString);
        StringRequestEntity sre = new StringRequestEntity(postContent, "text/xml", "UTF-8");
        method.setRequestEntity(sre);
        client.executeMethod(method);

        // Get response
        final SAXBuilder builder = new SAXBuilder();
        BufferedReader rd = new BufferedReader(new InputStreamReader(method.getResponseBodyAsStream()));
        final Document doc = builder.build(rd);
        return doc;
    } catch (IOException e) {

        // error in URL Connection or reading response
        throw new ConnectionException(e.getMessage());
    } catch (JDOMException e) {

        // error in converting to JDOM Document
        throw new ParseException(e.getMessage());
    }
}

From source file:net.jadler.JadlerTimeoutIntegrationTest.java

@Test(timeout = 10000L)
public void timeout() throws IOException {
    onRequest().respond().withStatus(201);

    final PostMethod method = new PostMethod("http://localhost:" + port());
    method.setRequestEntity(new StringRequestEntity("postbody", null, null));

    int status = client.executeMethod(method);
    assertThat(status, is(201));/*from   ww  w . j a va2  s.  com*/
}

From source file:com.atlassian.jira.web.action.setup.SetupProductBundleHelper.java

public void authenticateUser(final String username, final String password) {
    final HttpClient httpClient = prepareClient();
    final PostMethod method = new PostMethod(getAuthenticationUrl());

    try {/*from   w w w.ja  v a 2  s  . c  o m*/
        final StringRequestEntity requestEntity = new StringRequestEntity(
                prepareJSON(ImmutableMap.of("username", username, "password", password)), "application/json",
                "UTF-8");

        method.setRequestEntity(requestEntity);

        final int status = httpClient.executeMethod(method);
        if (status == 200) {
            saveCookies(httpClient.getState().getCookies());
        } else {
            log.warn("Problem authenticating user during product bundle license installation, status code: "
                    + status);
        }
    } catch (final IOException e) {
        log.warn("Problem authenticating user during product bundle license installation", e);
    } finally {
        method.releaseConnection();
    }
}

From source file:com.nfl.dm.clubsites.cms.articles.categorization.OpenCalaisRESTPost.java

private Map<String, ArrayList<String>> postString(String input, PostMethod method) throws IOException {
    method.setRequestEntity(new StringRequestEntity(input, null, null));
    return doRequest(input, method);
}

From source file:edu.utk.gsda.CalaisHttpClient.java

public String getAnnotatedResult(String originString) {
    try {//from w w  w.  j av a 2s  .co  m
        client = new HttpClient();
        client.getParams().setParameter("http.useragent", "Calais Rest Client");

        initMethod();

        method.setRequestEntity(new StringRequestEntity(originString, null, null));
        int returnCode = client.executeMethod(method);

        if (returnCode == HttpStatus.SC_OK) {
            BufferedReader reader = new BufferedReader(
                    new InputStreamReader(method.getResponseBodyAsStream(), "UTF-8"));

            StringBuffer stringBuffer = new StringBuffer();
            String line;
            while ((line = reader.readLine()) != null) {
                stringBuffer.append(line);
            }

            reader.close();
            return stringBuffer.toString();
        } else {
            System.err.println("request failed: ");
            System.err.println("Got code: " + returnCode);
            return null;
        }

    } catch (Exception e) {
        e.printStackTrace();
    }

    return null;

}

From source file:be.fedict.eid.pkira.xkmsws.util.HttpUtil.java

public byte[] postMessage(String message) throws XKMSClientException {
    try {/* w ww. j a  v  a 2 s  .c o  m*/
        PostMethod method = new PostMethod(endpointAddress);
        method.setRequestEntity(new StringRequestEntity(message, "text/xml", "utf-8"));
        int resultCode = client.executeMethod(method);
        if (resultCode != 200) {
            throw new XKMSClientException("Error calling XKMS service. Got back status code " + resultCode);
        }

        return method.getResponseBody();
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException(e); // shouldn't happen
    } catch (HttpException e) {
        throw new XKMSClientException("Error calling XKMS service.", e);
    } catch (IOException e) {
        throw new XKMSClientException("Error calling XKMS service.", e);
    }
}