001: /*
002: * $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 $
003: * $Revision: 480424 $
004: * $Date: 2006-11-29 06:56:49 +0100 (Wed, 29 Nov 2006) $
005: * ====================================================================
006: *
007: * Licensed to the Apache Software Foundation (ASF) under one or more
008: * contributor license agreements. See the NOTICE file distributed with
009: * this work for additional information regarding copyright ownership.
010: * The ASF licenses this file to You under the Apache License, Version 2.0
011: * (the "License"); you may not use this file except in compliance with
012: * the License. You may obtain a copy of the License at
013: *
014: * http://www.apache.org/licenses/LICENSE-2.0
015: *
016: * Unless required by applicable law or agreed to in writing, software
017: * distributed under the License is distributed on an "AS IS" BASIS,
018: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
019: * See the License for the specific language governing permissions and
020: * limitations under the License.
021: * ====================================================================
022: *
023: * This software consists of voluntary contributions made by many
024: * individuals on behalf of the Apache Software Foundation. For more
025: * information on the Apache Software Foundation, please see
026: * <http://www.apache.org/>.
027: *
028: * [Additional notices, if required by prior licensing conditions]
029: *
030: */
031:
032: import java.io.File;
033:
034: import org.apache.commons.httpclient.HttpClient;
035: import org.apache.commons.httpclient.methods.FileRequestEntity;
036: import org.apache.commons.httpclient.methods.PostMethod;
037: import org.apache.commons.httpclient.methods.RequestEntity;
038:
039: /**
040: *
041: * This is a sample application that demonstrates
042: * how to use the Jakarta HttpClient API.
043: *
044: * This application sends an XML document
045: * to a remote web server using HTTP POST
046: *
047: * @author Sean C. Sullivan
048: * @author Ortwin Glueck
049: * @author Oleg Kalnichevski
050: */
051: public class PostXML {
052:
053: /**
054: *
055: * Usage:
056: * java PostXML http://mywebserver:80/ c:\foo.xml
057: *
058: * @param args command line arguments
059: * Argument 0 is a URL to a web server
060: * Argument 1 is a local filename
061: *
062: */
063: public static void main(String[] args) throws Exception {
064: if (args.length != 2) {
065: System.out
066: .println("Usage: java -classpath <classpath> [-Dorg.apache.commons.logging.simplelog.defaultlog=<loglevel>] PostXML <url> <filename>]");
067: System.out
068: .println("<classpath> - must contain the commons-httpclient.jar and commons-logging.jar");
069: System.out
070: .println("<loglevel> - one of error, warn, info, debug, trace");
071: System.out.println("<url> - the URL to post the file to");
072: System.out.println("<filename> - file to post to the URL");
073: System.out.println();
074: System.exit(1);
075: }
076: // Get target URL
077: String strURL = args[0];
078: // Get file to be posted
079: String strXMLFilename = args[1];
080: File input = new File(strXMLFilename);
081: // Prepare HTTP post
082: PostMethod post = new PostMethod(strURL);
083: // Request content will be retrieved directly
084: // from the input stream
085: RequestEntity entity = new FileRequestEntity(input,
086: "text/xml; charset=ISO-8859-1");
087: post.setRequestEntity(entity);
088: // Get HTTP client
089: HttpClient httpclient = new HttpClient();
090: // Execute request
091: try {
092: int result = httpclient.executeMethod(post);
093: // Display status code
094: System.out.println("Response status code: " + result);
095: // Display response
096: System.out.println("Response body: ");
097: System.out.println(post.getResponseBodyAsString());
098: } finally {
099: // Release current connection to the connection pool once you are done
100: post.releaseConnection();
101: }
102: }
103: }
|