KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > PostSOAP


1 /*
2  * $Header$
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
29 import java.io.File JavaDoc;
30
31 import org.apache.commons.httpclient.HttpClient;
32 import org.apache.commons.httpclient.methods.FileRequestEntity;
33 import org.apache.commons.httpclient.methods.PostMethod;
34 import org.apache.commons.httpclient.methods.RequestEntity;
35
36 /**
37  *
38  * This is a sample application that demonstrates
39  * how to use the Jakarta HttpClient API.
40  *
41  * This application sends an XML document
42  * to a remote web server using HTTP POST
43  *
44  * @author Sean C. Sullivan
45  * @author Ortwin Glueck
46  * @author Oleg Kalnichevski
47  * @author Paul King
48  */

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

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