KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > UnbufferedPost


1 /*
2  * $Header: /home/cvs/jakarta-commons/httpclient/src/examples/UnbufferedPost.java,v 1.2.2.1 2004/02/22 18:21:12 olegk Exp $
3  * $Revision: 1.2.2.1 $
4  * $Date: 2004/02/22 18:21:12 $
5  * ====================================================================
6  *
7  * Copyright 2002-2004 The Apache Software Foundation
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ====================================================================
21  *
22  * This software consists of voluntary contributions made by many
23  * individuals on behalf of the Apache Software Foundation. For more
24  * information on the Apache Software Foundation, please see
25  * <http://www.apache.org/>.
26  *
27  * [Additional notices, if required by prior licensing conditions]
28  *
29  */

30 import java.io.File JavaDoc;
31 import java.io.FileInputStream JavaDoc;
32 import org.apache.commons.httpclient.HttpClient;
33 import org.apache.commons.httpclient.HttpStatus;
34 import org.apache.commons.httpclient.methods.PostMethod;
35
36 /**
37  * Example how to use unbuffered POST request.
38  *
39  * @author Oleg Kalnichevski
40  */

41 public class UnbufferedPost {
42
43   public static void main(String JavaDoc[] args) throws Exception JavaDoc {
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     File JavaDoc file = new File JavaDoc(args[0]);
54     httppost.setRequestBody(new FileInputStream JavaDoc(file));
55     httppost.setRequestContentLength((int)file.length());
56
57     client.executeMethod(httppost);
58
59     if (httppost.getStatusCode() == HttpStatus.SC_OK) {
60         System.out.println(httppost.getResponseBodyAsString());
61     } else {
62       System.out.println("Unexpected failure: " + httppost.getStatusLine().toString());
63     }
64     httppost.releaseConnection();
65   }
66 }
67
Popular Tags