Subversion Repositories javautils

Rev

Blame | Last modification | View Log | RSS feed

  1. /*
  2.  * ====================================================================
  3.  *
  4.  *  Licensed to the Apache Software Foundation (ASF) under one or more
  5.  *  contributor license agreements.  See the NOTICE file distributed with
  6.  *  this work for additional information regarding copyright ownership.
  7.  *  The ASF licenses this file to You under the Apache License, Version 2.0
  8.  *  (the "License"); you may not use this file except in compliance with
  9.  *  the License.  You may obtain a copy of the License at
  10.  *
  11.  *      http://www.apache.org/licenses/LICENSE-2.0
  12.  *
  13.  *  Unless required by applicable law or agreed to in writing, software
  14.  *  distributed under the License is distributed on an "AS IS" BASIS,
  15.  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16.  *  See the License for the specific language governing permissions and
  17.  *  limitations under the License.
  18.  * ====================================================================
  19.  *
  20.  * This software consists of voluntary contributions made by many
  21.  * individuals on behalf of the Apache Software Foundation.  For more
  22.  * information on the Apache Software Foundation, please see
  23.  * <http://www.apache.org/>.
  24.  *
  25.  */
  26.  
  27. package org.apache.http.examples.client;
  28.  
  29. import java.io.File;
  30. import java.io.FileInputStream;
  31.  
  32. import org.apache.http.HttpEntity;
  33. import org.apache.http.HttpResponse;
  34. import org.apache.http.client.HttpClient;
  35. import org.apache.http.client.methods.HttpPost;
  36. import org.apache.http.entity.InputStreamEntity;
  37. import org.apache.http.impl.client.DefaultHttpClient;
  38.  
  39. /**
  40.  * Example how to use unbuffered chunk-encoded POST request.
  41.  */
  42. public class ClientChunkEncodedPost {
  43.  
  44.     public static void main(String[] args) throws Exception {
  45.         if (args.length != 1)  {
  46.             System.out.println("File path not given");
  47.             System.exit(1);
  48.         }
  49.         HttpClient httpclient = new DefaultHttpClient();
  50.  
  51.         HttpPost httppost = new HttpPost("http://localhost:8080" +
  52.                 "/servlets-examples/servlet/RequestInfoExample");
  53.  
  54.         File file = new File(args[0]);
  55.  
  56.         InputStreamEntity reqEntity = new InputStreamEntity(
  57.                 new FileInputStream(file), -1);
  58.         reqEntity.setContentType("binary/octet-stream");
  59.         reqEntity.setChunked(true);
  60.         // It may be more appropriate to use FileEntity class in this particular
  61.         // instance but we are using a more generic InputStreamEntity to demonstrate
  62.         // the capability to stream out data from any arbitrary source
  63.         //
  64.         // FileEntity entity = new FileEntity(file, "binary/octet-stream");
  65.        
  66.         httppost.setEntity(reqEntity);
  67.        
  68.         System.out.println("executing request " + httppost.getRequestLine());
  69.         HttpResponse response = httpclient.execute(httppost);
  70.         HttpEntity resEntity = response.getEntity();
  71.  
  72.         System.out.println("----------------------------------------");
  73.         System.out.println(response.getStatusLine());
  74.         if (resEntity != null) {
  75.             System.out.println("Response content length: " + resEntity.getContentLength());
  76.             System.out.println("Chunked?: " + resEntity.isChunked());
  77.         }
  78.         if (resEntity != null) {
  79.             resEntity.consumeContent();
  80.         }
  81.  
  82.         // When HttpClient instance is no longer needed,
  83.         // shut down the connection manager to ensure
  84.         // immediate deallocation of all system resources
  85.         httpclient.getConnectionManager().shutdown();        
  86.     }
  87.    
  88. }
  89.