You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by ol...@apache.org on 2003/02/02 01:04:15 UTC

cvs commit: jakarta-commons/httpclient/src/examples UnbufferedPost.java ChunkEncodedPost.java

olegk       2003/02/01 16:04:15

  Modified:    httpclient/src/examples ChunkEncodedPost.java
  Added:       httpclient/src/examples UnbufferedPost.java
  Log:
  - Added a new example how to use unbuffered POST without chunked encoding
  - Chunk-encoded POST example improved
  
  Submitted by Oleg Kalnichevski
  
  Revision  Changes    Path
  1.3       +12 -13    jakarta-commons/httpclient/src/examples/ChunkEncodedPost.java
  
  Index: ChunkEncodedPost.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/httpclient/src/examples/ChunkEncodedPost.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- ChunkEncodedPost.java	23 Jan 2003 22:47:42 -0000	1.2
  +++ ChunkEncodedPost.java	2 Feb 2003 00:04:15 -0000	1.3
  @@ -59,32 +59,31 @@
    * [Additional notices, if required by prior licensing conditions]
    *
    */
  -
  +import java.io.File;
  +import java.io.FileInputStream;
   import org.apache.commons.httpclient.HttpClient;
   import org.apache.commons.httpclient.HttpStatus;
   import org.apache.commons.httpclient.methods.PostMethod;
   
   /**
  - * Example how to use unbuffered POST request.
  + * Example how to use unbuffered chunk-encoded POST request.
    *
    * @author Oleg Kalnichevski
    */
   public class ChunkEncodedPost {
   
     public static void main(String[] args) throws Exception {
  -    String body = "Hi there!!!";
  -    if (args.length > 0)  {
  -      body = args[0];
  +    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);
       }
  -
       HttpClient client = new HttpClient();
   
       PostMethod httppost = new PostMethod("http://localhost:8080/httpclienttest/body");
  -
       httppost.setUseDisk(false);
  -    // Normally you would use setRequestBody(InputStream) here
  -    // if you have unbuffered data
  -    httppost.setRequestBody(body);
  +
  +    httppost.setRequestBody(new FileInputStream(new File(args[0])));
       httppost.setRequestContentLength(PostMethod.CONTENT_LENGTH_CHUNKED);
   
       client.executeMethod(httppost);
  
  
  
  1.1                  jakarta-commons/httpclient/src/examples/UnbufferedPost.java
  
  Index: UnbufferedPost.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-commons/httpclient/src/examples/UnbufferedPost.java,v 1.1 2003/02/02 00:04:15 olegk Exp $
   * $Revision: 1.1 $
   * $Date: 2003/02/02 00:04:15 $
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2002-2003 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "HttpClient", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   * [Additional notices, if required by prior licensing conditions]
   *
   */
  import java.io.File;
  import java.io.FileInputStream;
  import org.apache.commons.httpclient.HttpClient;
  import org.apache.commons.httpclient.HttpStatus;
  import org.apache.commons.httpclient.methods.PostMethod;
  
  /**
   * Example how to use unbuffered POST request.
   *
   * @author Oleg Kalnichevski
   */
  public class UnbufferedPost {
  
    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);
      }
      HttpClient client = new HttpClient();
  
      PostMethod httppost = new PostMethod("http://localhost:8080/httpclienttest/body");
      httppost.setUseDisk(false);
  
      File file = new File(args[0]);
      httppost.setRequestBody(new FileInputStream(file));
      httppost.setRequestContentLength((int)file.length());
  
      client.executeMethod(httppost);
  
      if (httppost.getStatusCode() == HttpStatus.SC_OK) {
          System.out.println(httppost.getResponseBodyAsString());
      } else {
        System.out.println("Unexpected failure: " + httppost.getStatusLine().toString());
      }
      httppost.releaseConnection();
    }
  }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org