Example usage for org.apache.commons.httpclient.methods PostMethod setRequestEntity

List of usage examples for org.apache.commons.httpclient.methods PostMethod setRequestEntity

Introduction

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

Prototype

public void setRequestEntity(RequestEntity paramRequestEntity) 

Source Link

Usage

From source file:UnbufferedPost.java

public static void main(String[] args) throws Exception {
    if (args.length != 1) {
        System.out.println("Usage: ChunkEncodedPost <file>");
        System.out.println("<file> - full path to a file to be posted");
        System.exit(1);//from   w  w w .j a  va  2  s  . c  o  m
    }
    HttpClient client = new HttpClient();

    PostMethod httppost = new PostMethod("http://localhost:8080/httpclienttest/body");

    File file = new File(args[0]);
    httppost.setRequestEntity(new InputStreamRequestEntity(new FileInputStream(file), file.length()));

    try {
        client.executeMethod(httppost);

        if (httppost.getStatusCode() == HttpStatus.SC_OK) {
            System.out.println(httppost.getResponseBodyAsString());
        } else {
            System.out.println("Unexpected failure: " + httppost.getStatusLine().toString());
        }
    } finally {
        httppost.releaseConnection();
    }
}

From source file:ChunkEncodedPost.java

public static void main(String[] args) throws Exception {
    if (args.length != 1) {
        System.out.println("Usage: ChunkEncodedPost <file>");
        System.out.println("<file> - full path to a file to be posted");
        System.exit(1);/*w  w w  .j  a  v  a 2s .  co m*/
    }
    HttpClient client = new HttpClient();

    PostMethod httppost = new PostMethod("http://localhost:8080/httpclienttest/body");

    File file = new File(args[0]);

    httppost.setRequestEntity(new InputStreamRequestEntity(new FileInputStream(file)));
    httppost.setContentChunked(true);

    try {
        client.executeMethod(httppost);

        if (httppost.getStatusCode() == HttpStatus.SC_OK) {
            System.out.println(httppost.getResponseBodyAsString());
        } else {
            System.out.println("Unexpected failure: " + httppost.getStatusLine().toString());
        }
    } finally {
        httppost.releaseConnection();
    }
}

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);
    HttpClient httpclient = new HttpClient();

    try {//from  ww w.j a va 2s  .  c  o m
        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:PostXML.java

/**
 *
 * Usage://from ww  w.java  2 s  .  c  o m
 *          java PostXML http://mywebserver:80/ c:\foo.xml
 *
 *  @param args command line arguments
 *                 Argument 0 is a URL to a web server
 *                 Argument 1 is a local filename
 *
 */
public static void main(String[] args) throws Exception {
    if (args.length != 2) {
        System.out.println(
                "Usage: java -classpath <classpath> [-Dorg.apache.commons.logging.simplelog.defaultlog=<loglevel>] PostXML <url> <filename>]");
        System.out.println("<classpath> - must contain the commons-httpclient.jar and commons-logging.jar");
        System.out.println("<loglevel> - one of error, warn, info, debug, trace");
        System.out.println("<url> - the URL to post the file to");
        System.out.println("<filename> - file to post to the URL");
        System.out.println();
        System.exit(1);
    }
    // Get target URL
    String strURL = args[0];
    // Get file to be posted
    String strXMLFilename = args[1];
    File input = new File(strXMLFilename);
    // Prepare HTTP post
    PostMethod post = new PostMethod(strURL);
    // Request content will be retrieved directly
    // from the input stream
    RequestEntity entity = new FileRequestEntity(input, "text/xml; charset=ISO-8859-1");
    post.setRequestEntity(entity);
    // Get HTTP client
    HttpClient httpclient = new HttpClient();
    // Execute request
    try {
        int result = httpclient.executeMethod(post);
        // Display status code
        System.out.println("Response status code: " + result);
        // Display response
        System.out.println("Response body: ");
        System.out.println(post.getResponseBodyAsString());
    } finally {
        // Release current connection to the connection pool once you are done
        post.releaseConnection();
    }
}

From source file:PostSOAP.java

/**
 *
 * Usage:/*  ww w .j  av a 2  s .  co  m*/
 *          java PostSOAP http://mywebserver:80/ SOAPAction c:\foo.xml
 *
 *  @param args command line arguments
 *                 Argument 0 is a URL to a web server
 *                 Argument 1 is the SOAP Action
 *                 Argument 2 is a local filename
 *
 */
public static void main(String[] args) throws Exception {
    if (args.length != 3) {
        System.out.println(
                "Usage: java -classpath <classpath> [-Dorg.apache.commons.logging.simplelog.defaultlog=<loglevel>] PostSOAP <url> <soapaction> <filename>]");
        System.out.println("<classpath> - must contain the commons-httpclient.jar and commons-logging.jar");
        System.out.println("<loglevel> - one of error, warn, info, debug, trace");
        System.out.println("<url> - the URL to post the file to");
        System.out.println("<soapaction> - the SOAP action header value");
        System.out.println("<filename> - file to post to the URL");
        System.out.println();
        System.exit(1);
    }
    // Get target URL
    String strURL = args[0];
    // Get SOAP action
    String strSoapAction = args[1];
    // Get file to be posted
    String strXMLFilename = args[2];
    File input = new File(strXMLFilename);
    // Prepare HTTP post
    PostMethod post = new PostMethod(strURL);
    // Request content will be retrieved directly
    // from the input stream
    RequestEntity entity = new FileRequestEntity(input, "text/xml; charset=ISO-8859-1");
    post.setRequestEntity(entity);
    // consult documentation for your web service
    post.setRequestHeader("SOAPAction", strSoapAction);
    // Get HTTP client
    HttpClient httpclient = new HttpClient();
    // Execute request
    try {
        int result = httpclient.executeMethod(post);
        // Display status code
        System.out.println("Response status code: " + result);
        // Display response
        System.out.println("Response body: ");
        System.out.println(post.getResponseBodyAsString());
    } finally {
        // Release current connection to the connection pool once you are done
        post.releaseConnection();
    }
}

From source file:com.mycompany.jaxrsexample.client.Client.java

public static void main(String args[]) throws Exception {
    // Sent HTTP GET request to query all students info

    System.out.println("Invoking server through HTTP GET to query all students info");
    URL url = new URL("http://localhost:9000/studentservice/students");
    InputStream in = url.openStream();
    System.out.println(getStringFromInputStream(in));

    // Sent HTTP GET request to query student info
    System.out.println("Sent HTTP GET request to query customer info");
    url = new URL("http://localhost:9000/studentservice/students/1");
    in = url.openStream();//w w  w.  j  av a 2  s. c om
    System.out.println(getStringFromInputStream(in));

    // Sent HTTP PUT request to update student info
    System.out.println("\n");
    System.out.println("Sent HTTP PUT request to update student info");
    Client client = new Client();
    String inputFile = client.getClass().getResource("/update_student.xml").getFile();
    URIResolver resolver = new URIResolver(inputFile);
    File input = new File(resolver.getURI());
    PutMethod put = new PutMethod("http://localhost:9000/studentservice/students");
    RequestEntity entity = new FileRequestEntity(input, "text/xml; charset=ISO-8859-1");
    put.setRequestEntity(entity);
    HttpClient httpclient = new HttpClient();

    try {
        int result = httpclient.executeMethod(put);
        System.out.println("Response status code: " + result);
        System.out.println("Response body: ");
        System.out.println(put.getResponseBodyAsString());
    } finally {
        // Release current connection to the connection pool once you are
        // done
        put.releaseConnection();
    }

    // Sent HTTP POST request to add student
    System.out.println("\n");
    System.out.println("Sent HTTP POST request to add student");
    inputFile = client.getClass().getResource("/add_student.xml").getFile();
    resolver = new URIResolver(inputFile);
    input = new File(resolver.getURI());
    PostMethod post = new PostMethod("http://localhost:9000/studentservice/students");
    post.addRequestHeader("Accept", "text/xml");
    entity = new FileRequestEntity(input, "text/xml; charset=ISO-8859-1");
    post.setRequestEntity(entity);
    httpclient = new HttpClient();

    try {
        int result = httpclient.executeMethod(post);
        System.out.println("Response status code: " + result);
        System.out.println("Response body: ");
        System.out.println(post.getResponseBodyAsString());
    } finally {
        // Release current connection to the connection pool once you are
        // done
        post.releaseConnection();
    }

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

From source file:com.zxm.servicemix.examples.cxf.jaxrs.client.Client.java

public static void main(String args[]) throws Exception {
    // Sent HTTP GET request to query all customer info

    // Sent HTTP GET request to query customer info
    System.out.println("Sent HTTP GET request to query customer info");
    URL url = new URL("http://localhost:8181/cxf/crm/customerservice/customers/123");
    InputStream in = url.openStream();
    System.out.println(getStringFromInputStream(in));

    // Sent HTTP GET request to query sub resource product info
    System.out.println("\n");
    System.out.println("Sent HTTP GET request to query sub resource product info");
    url = new URL("http://localhost:8181/cxf/crm/customerservice/orders/223/products/323");
    in = url.openStream();//from  w  ww.j  a va 2  s. c o  m
    System.out.println(getStringFromInputStream(in));

    // Sent HTTP PUT request to update customer info
    System.out.println("\n");
    System.out.println("Sent HTTP PUT request to update customer info");
    Client client = new Client();
    String inputFile = client.getClass().getResource("update_customer.xml").getFile();
    File input = new File(inputFile);
    PutMethod put = new PutMethod("http://localhost:8181/cxf/crm/customerservice/customers");
    RequestEntity entity = new FileRequestEntity(input, "text/xml; charset=ISO-8859-1");
    put.setRequestEntity(entity);
    HttpClient httpclient = new HttpClient();

    try {
        int result = httpclient.executeMethod(put);
        System.out.println("Response status code: " + result);
        System.out.println("Response body: ");
        System.out.println(put.getResponseBodyAsString());
    } finally {
        // Release current connection to the connection pool once you are
        // done
        put.releaseConnection();
    }

    // Sent HTTP POST request to add customer
    System.out.println("\n");
    System.out.println("Sent HTTP POST request to add customer");
    inputFile = client.getClass().getResource("add_customer.xml").getFile();
    input = new File(inputFile);
    PostMethod post = new PostMethod("http://localhost:8181/cxf/crm/customerservice/customers");
    post.addRequestHeader("Accept", "text/xml");
    entity = new FileRequestEntity(input, "text/xml; charset=ISO-8859-1");
    post.setRequestEntity(entity);
    httpclient = new HttpClient();

    try {
        int result = httpclient.executeMethod(post);
        System.out.println("Response status code: " + result);
        System.out.println("Response body: ");
        System.out.println(post.getResponseBodyAsString());
    } finally {
        // Release current connection to the connection pool once you are
        // done
        post.releaseConnection();
    }

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

From source file:edu.techseekers.training.client.Client.java

public static void main(String args[]) throws Exception {
    // Sent HTTP GET request to query all customer info
    /*// w  ww .  j a  v  a2s  .c o m
     * URL url = new URL("http://localhost:9000/customers");
     * System.out.println("Invoking server through HTTP GET to query all
     * customer info"); InputStream in = url.openStream(); StreamSource
     * source = new StreamSource(in); printSource(source);
     */

    // Sent HTTP GET request to query customer info
    System.out.println("Sent HTTP GET request to query customer info");
    URL url = new URL("http://localhost:9000/customerservice/customers/123");
    InputStream in = url.openStream();
    System.out.println(getStringFromInputStream(in));

    // Sent HTTP GET request to query sub resource product info
    System.out.println("\n");
    System.out.println("Sent HTTP GET request to query sub resource product info");
    url = new URL("http://localhost:9000/customerservice/orders/223/products/323");
    in = url.openStream();
    System.out.println(getStringFromInputStream(in));

    // Sent HTTP PUT request to update customer info
    System.out.println("\n");
    System.out.println("Sent HTTP PUT request to update customer info");
    Client client = new Client();
    String inputFile = client.getClass().getResource("/update_customer.xml").getFile();
    URIResolver resolver = new URIResolver(inputFile);
    File input = new File(resolver.getURI());
    PutMethod put = new PutMethod("http://localhost:9000/customerservice/customers");
    RequestEntity entity = new FileRequestEntity(input, "text/xml; charset=ISO-8859-1");
    put.setRequestEntity(entity);
    HttpClient httpclient = new HttpClient();

    try {
        int result = httpclient.executeMethod(put);
        System.out.println("Response status code: " + result);
        System.out.println("Response body: ");
        System.out.println(put.getResponseBodyAsString());
    } finally {
        // Release current connection to the connection pool once you are
        // done
        put.releaseConnection();
    }

    // Sent HTTP POST request to add customer
    System.out.println("\n");
    System.out.println("Sent HTTP POST request to add customer");
    inputFile = client.getClass().getResource("/add_customer.xml").getFile();
    resolver = new URIResolver(inputFile);
    input = new File(resolver.getURI());
    PostMethod post = new PostMethod("http://localhost:9000/customerservice/customers");
    post.addRequestHeader("Accept", "text/xml");
    entity = new FileRequestEntity(input, "text/xml; charset=ISO-8859-1");
    post.setRequestEntity(entity);
    httpclient = new HttpClient();

    try {
        int result = httpclient.executeMethod(post);
        System.out.println("Response status code: " + result);
        System.out.println("Response body: ");
        System.out.println(post.getResponseBodyAsString());
    } finally {
        // Release current connection to the connection pool once you are
        // done
        post.releaseConnection();
    }

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

From source file:main.java.demo.jaxrs.client.Client.java

public static void main(String args[]) throws Exception {
    // Sent HTTP GET request to query all customer info
    /*/* ww w . j a v a 2  s  . c o  m*/
     * URL url = new URL("http://localhost:9000/customers");
     * System.out.println("Invoking server through HTTP GET to query all
     * customer info"); InputStream in = url.openStream(); StreamSource
     * source = new StreamSource(in); printSource(source);
     */

    // Sent HTTP GET request to query customer info
    System.out.println("Sent HTTP GET request to query customer info");
    URL url = new URL("http://localhost:8989/RestFull/service1/customerservice/customers/123");
    InputStream in = url.openStream();
    System.out.println(getStringFromInputStream(in));

    // Sent HTTP GET request to query sub resource product info
    System.out.println("\n");
    System.out.println("Sent HTTP GET request to query sub resource product info");
    url = new URL("http://localhost:9000/customerservice/orders/223/products/323");
    in = url.openStream();
    System.out.println(getStringFromInputStream(in));

    // Sent HTTP PUT request to update customer info
    System.out.println("\n");
    System.out.println("Sent HTTP PUT request to update customer info");
    Client client = new Client();
    String inputFile = client.getClass().getResource("update_customer.xml").getFile();
    URIResolver resolver = new URIResolver(inputFile);
    File input = new File(resolver.getURI());
    PutMethod put = new PutMethod("http://localhost:9000/customerservice/customers");
    RequestEntity entity = new FileRequestEntity(input, "text/xml; charset=ISO-8859-1");
    put.setRequestEntity(entity);
    HttpClient httpclient = new HttpClient();

    try {
        int result = httpclient.executeMethod(put);
        System.out.println("Response status code: " + result);
        System.out.println("Response body: ");
        System.out.println(put.getResponseBodyAsString());
    } finally {
        // Release current connection to the connection pool once you are
        // done
        put.releaseConnection();
    }

    // Sent HTTP POST request to add customer
    System.out.println("\n");
    System.out.println("Sent HTTP POST request to add customer");
    inputFile = client.getClass().getResource("add_customer.xml").getFile();
    resolver = new URIResolver(inputFile);
    input = new File(resolver.getURI());
    PostMethod post = new PostMethod("http://localhost:9000/customerservice/customers");
    post.addRequestHeader("Accept", "text/xml");
    entity = new FileRequestEntity(input, "text/xml; charset=ISO-8859-1");
    post.setRequestEntity(entity);
    httpclient = new HttpClient();

    try {
        int result = httpclient.executeMethod(post);
        System.out.println("Response status code: " + result);
        System.out.println("Response body: ");
        System.out.println(post.getResponseBodyAsString());
    } finally {
        // Release current connection to the connection pool once you are
        // done
        post.releaseConnection();
    }

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

From source file:main.java.client.Client.java

public static void main(String args[]) throws Exception {
    // Sent HTTP GET request to query all customer info
    /*//from   ww  w . j a v  a2 s.  c om
     * URL url = new URL("http://localhost:9000/customers");
     * System.out.println("Invoking server through HTTP GET to query all
     * customer info"); InputStream in = url.openStream(); StreamSource
     * source = new StreamSource(in); printSource(source);
     */

    // Sent HTTP GET request to query customer info
    System.out.println("Sent HTTP GET request to query customer info");
    URL url = new URL("http://localhost:8080/authenticate/customers/123");
    InputStream in = url.openStream();
    System.out.println(getStringFromInputStream(in));

    // Sent HTTP GET request to query sub resource product info
    System.out.println("\n");
    System.out.println("Sent HTTP GET request to query sub resource product info");
    url = new URL("http://localhost:9000/authenticate/orders/223/products/323");
    in = url.openStream();
    System.out.println(getStringFromInputStream(in));

    // Sent HTTP PUT request to update customer info
    System.out.println("\n");
    System.out.println("Sent HTTP PUT request to update customer info");
    Client client = new Client();
    String inputFile = client.getClass().getResource("/update_customer.xml").getFile();
    URIResolver resolver = new URIResolver(inputFile);
    File input = new File(resolver.getURI());
    PutMethod put = new PutMethod("http://localhost:9000/authenticate/customers");
    RequestEntity entity = new FileRequestEntity(input, "text/xml; charset=ISO-8859-1");
    put.setRequestEntity(entity);
    HttpClient httpclient = new HttpClient();

    try {
        int result = httpclient.executeMethod(put);
        System.out.println("Response status code: " + result);
        System.out.println("Response body: ");
        System.out.println(put.getResponseBodyAsString());
    } finally {
        // Release current connection to the connection pool once you are
        // done
        put.releaseConnection();
    }

    // Sent HTTP POST request to add customer
    System.out.println("\n");
    System.out.println("Sent HTTP POST request to add customer");
    inputFile = client.getClass().getResource("/add_customer.xml").getFile();
    resolver = new URIResolver(inputFile);
    input = new File(resolver.getURI());
    PostMethod post = new PostMethod("http://localhost:9000/authenticate/customers");
    post.addRequestHeader("Accept", "text/xml");
    entity = new FileRequestEntity(input, "text/xml; charset=ISO-8859-1");
    post.setRequestEntity(entity);
    httpclient = new HttpClient();

    try {
        int result = httpclient.executeMethod(post);
        System.out.println("Response status code: " + result);
        System.out.println("Response body: ");
        System.out.println(post.getResponseBodyAsString());
    } finally {
        // Release current connection to the connection pool once you are
        // done
        post.releaseConnection();
    }

    // Sent HTTP POST request to add json customer
    System.out.println("\n");
    System.out.println("Sent HTTP POST request to add json customer");
    String inputFilej = client.getClass().getResource("/add_customer.json").getFile();
    URIResolver resolverj = new URIResolver(inputFilej);
    File inputj = new File(resolverj.getURI());
    PostMethod postj = new PostMethod("http://localhost:9000/authenticate/customersjson");
    postj.addRequestHeader("Accept", "application/json");
    RequestEntity entityj = new FileRequestEntity(inputj, "application/json; charset=ISO-8859-1");
    postj.setRequestEntity(entityj);
    HttpClient httpclientj = new HttpClient();

    try {
        int resultj = httpclientj.executeMethod(postj);
        System.out.println("Response status code json: " + resultj);
        System.out.println("Response body json: ");
        System.out.println(postj.getResponseBodyAsString());
    } finally {
        // Release current connection to the connection pool once you are
        // done
        postj.releaseConnection();
    }
    System.out.println("\n");
    System.exit(0);
}