1 30 import java.io.File ; 31 import java.io.FileInputStream ; 32 import org.apache.commons.httpclient.HttpClient; 33 import org.apache.commons.httpclient.HttpStatus; 34 import org.apache.commons.httpclient.methods.PostMethod; 35 36 41 public class ChunkEncodedPost { 42 43 public static void main(String [] args) throws Exception { 44 if (args.length != 1) { 45 System.out.println("Usage: ChunkEncodedPost <file>"); 46 System.out.println("<file> - full path to a file to be posted"); 47 System.exit(1); 48 } 49 HttpClient client = new HttpClient(); 50 51 PostMethod httppost = new PostMethod("http://localhost:8080/httpclienttest/body"); 52 53 httppost.setRequestBody(new FileInputStream (new File (args[0]))); 54 httppost.setRequestContentLength(PostMethod.CONTENT_LENGTH_CHUNKED); 55 56 client.executeMethod(httppost); 57 58 if (httppost.getStatusCode() == HttpStatus.SC_OK) { 59 System.out.println(httppost.getResponseBodyAsString()); 60 } else { 61 System.out.println("Unexpected failure: " + httppost.getStatusLine().toString()); 62 } 63 httppost.releaseConnection(); 64 } 65 } 66 | Popular Tags |