001: /*
002: * $Header$
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:
029: import java.io.File;
030:
031: import org.apache.commons.httpclient.HttpClient;
032: import org.apache.commons.httpclient.methods.FileRequestEntity;
033: import org.apache.commons.httpclient.methods.PostMethod;
034: import org.apache.commons.httpclient.methods.RequestEntity;
035:
036: /**
037: *
038: * This is a sample application that demonstrates
039: * how to use the Jakarta HttpClient API.
040: *
041: * This application sends an XML document
042: * to a remote web server using HTTP POST
043: *
044: * @author Sean C. Sullivan
045: * @author Ortwin Glueck
046: * @author Oleg Kalnichevski
047: * @author Paul King
048: */
049: public class PostSOAP {
050:
051: /**
052: *
053: * Usage:
054: * java PostSOAP http://mywebserver:80/ SOAPAction c:\foo.xml
055: *
056: * @param args command line arguments
057: * Argument 0 is a URL to a web server
058: * Argument 1 is the SOAP Action
059: * Argument 2 is a local filename
060: *
061: */
062: public static void main(String[] args) throws Exception {
063: if (args.length != 3) {
064: System.out
065: .println("Usage: java -classpath <classpath> [-Dorg.apache.commons.logging.simplelog.defaultlog=<loglevel>] PostSOAP <url> <soapaction> <filename>]");
066: System.out
067: .println("<classpath> - must contain the commons-httpclient.jar and commons-logging.jar");
068: System.out
069: .println("<loglevel> - one of error, warn, info, debug, trace");
070: System.out.println("<url> - the URL to post the file to");
071: System.out
072: .println("<soapaction> - the SOAP action header value");
073: System.out.println("<filename> - file to post to the URL");
074: System.out.println();
075: System.exit(1);
076: }
077: // Get target URL
078: String strURL = args[0];
079: // Get SOAP action
080: String strSoapAction = args[1];
081: // Get file to be posted
082: String strXMLFilename = args[2];
083: File input = new File(strXMLFilename);
084: // Prepare HTTP post
085: PostMethod post = new PostMethod(strURL);
086: // Request content will be retrieved directly
087: // from the input stream
088: RequestEntity entity = new FileRequestEntity(input,
089: "text/xml; charset=ISO-8859-1");
090: post.setRequestEntity(entity);
091: // consult documentation for your web service
092: post.setRequestHeader("SOAPAction", strSoapAction);
093: // Get HTTP client
094: HttpClient httpclient = new HttpClient();
095: // Execute request
096: try {
097: int result = httpclient.executeMethod(post);
098: // Display status code
099: System.out.println("Response status code: " + result);
100: // Display response
101: System.out.println("Response body: ");
102: System.out.println(post.getResponseBodyAsString());
103: } finally {
104: // Release current connection to the connection pool once you are done
105: post.releaseConnection();
106: }
107: }
108: }
|