KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > PostXML


1 /*
2  * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//httpclient/src/examples/PostXML.java,v 1.13 2004/05/12 20:43:53 olegk Exp $
3  * $Revision: 480424 $
4  * $Date: 2006-11-29 05:56:49 +0000 (Wed, 29 Nov 2006) $
5  * ====================================================================
6  *
7  * Licensed to the Apache Software Foundation (ASF) under one or more
8  * contributor license agreements. See the NOTICE file distributed with
9  * 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
32 import java.io.File JavaDoc;
33
34 import org.apache.commons.httpclient.HttpClient;
35 import org.apache.commons.httpclient.methods.FileRequestEntity;
36 import org.apache.commons.httpclient.methods.PostMethod;
37 import org.apache.commons.httpclient.methods.RequestEntity;
38
39 /**
40  *
41  * This is a sample application that demonstrates
42  * how to use the Jakarta HttpClient API.
43  *
44  * This application sends an XML document
45  * to a remote web server using HTTP POST
46  *
47  * @author Sean C. Sullivan
48  * @author Ortwin Glueck
49  * @author Oleg Kalnichevski
50  */

51 public class PostXML {
52
53     /**
54      *
55      * Usage:
56      * java PostXML http://mywebserver:80/ c:\foo.xml
57      *
58      * @param args command line arguments
59      * Argument 0 is a URL to a web server
60      * Argument 1 is a local filename
61      *
62      */

63     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
64         if (args.length != 2) {
65             System.out.println("Usage: java -classpath <classpath> [-Dorg.apache.commons.logging.simplelog.defaultlog=<loglevel>] PostXML <url> <filename>]");
66             System.out.println("<classpath> - must contain the commons-httpclient.jar and commons-logging.jar");
67             System.out.println("<loglevel> - one of error, warn, info, debug, trace");
68             System.out.println("<url> - the URL to post the file to");
69             System.out.println("<filename> - file to post to the URL");
70             System.out.println();
71             System.exit(1);
72         }
73         // Get target URL
74
String JavaDoc strURL = args[0];
75         // Get file to be posted
76
String JavaDoc strXMLFilename = args[1];
77         File JavaDoc input = new File JavaDoc(strXMLFilename);
78         // Prepare HTTP post
79
PostMethod post = new PostMethod(strURL);
80         // Request content will be retrieved directly
81
// from the input stream
82
RequestEntity entity = new FileRequestEntity(input, "text/xml; charset=ISO-8859-1");
83         post.setRequestEntity(entity);
84         // Get HTTP client
85
HttpClient httpclient = new HttpClient();
86         // Execute request
87
try {
88             int result = httpclient.executeMethod(post);
89             // Display status code
90
System.out.println("Response status code: " + result);
91             // Display response
92
System.out.println("Response body: ");
93             System.out.println(post.getResponseBodyAsString());
94         } finally {
95             // Release current connection to the connection pool once you are done
96
post.releaseConnection();
97         }
98     }
99 }
100
Popular Tags