01: /*
02: * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//httpclient/src/examples/UnbufferedPost.java,v 1.5 2004/05/12 20:43:53 olegk Exp $
03: * $Revision: 480424 $
04: * $Date: 2006-11-29 06:56:49 +0100 (Wed, 29 Nov 2006) $
05: * ====================================================================
06: *
07: * Licensed to the Apache Software Foundation (ASF) under one or more
08: * contributor license agreements. See the NOTICE file distributed with
09: * this work for additional information regarding copyright ownership.
10: * The ASF licenses this file to You under the Apache License, Version 2.0
11: * (the "License"); you may not use this file except in compliance with
12: * the License. You may obtain a copy of the License at
13: *
14: * http://www.apache.org/licenses/LICENSE-2.0
15: *
16: * Unless required by applicable law or agreed to in writing, software
17: * distributed under the License is distributed on an "AS IS" BASIS,
18: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19: * See the License for the specific language governing permissions and
20: * limitations under the License.
21: * ====================================================================
22: *
23: * This software consists of voluntary contributions made by many
24: * individuals on behalf of the Apache Software Foundation. For more
25: * information on the Apache Software Foundation, please see
26: * <http://www.apache.org/>.
27: *
28: * [Additional notices, if required by prior licensing conditions]
29: *
30: */
31: import java.io.File;
32: import java.io.FileInputStream;
33: import org.apache.commons.httpclient.HttpClient;
34: import org.apache.commons.httpclient.HttpStatus;
35: import org.apache.commons.httpclient.methods.InputStreamRequestEntity;
36: import org.apache.commons.httpclient.methods.PostMethod;
37:
38: /**
39: * Example how to use unbuffered POST request.
40: *
41: * @author Oleg Kalnichevski
42: */
43: public class UnbufferedPost {
44:
45: public static void main(String[] args) throws Exception {
46: if (args.length != 1) {
47: System.out.println("Usage: ChunkEncodedPost <file>");
48: System.out
49: .println("<file> - full path to a file to be posted");
50: System.exit(1);
51: }
52: HttpClient client = new HttpClient();
53:
54: PostMethod httppost = new PostMethod(
55: "http://localhost:8080/httpclienttest/body");
56:
57: File file = new File(args[0]);
58: httppost.setRequestEntity(new InputStreamRequestEntity(
59: new FileInputStream(file), file.length()));
60:
61: try {
62: client.executeMethod(httppost);
63:
64: if (httppost.getStatusCode() == HttpStatus.SC_OK) {
65: System.out.println(httppost.getResponseBodyAsString());
66: } else {
67: System.out.println("Unexpected failure: "
68: + httppost.getStatusLine().toString());
69: }
70: } finally {
71: httppost.releaseConnection();
72: }
73: }
74: }
|