Java Reference
In-Depth Information
Let's now start to fill in the createSOAPRequest method. You can start by creating an empty SOAP
message, so that your code now looks like the following:
import javax.xml.namespace.QName;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.MimeHeaders;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPBodyElement;
import javax.xml.soap.SOAPConnection;
import javax.xml.soap.SOAPConnectionFactory;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPMessage;
public class ClientWithoutWSDL {
private final static String SERVICE_HOST = " http://www.webserviceX.NET/";
private final static String SERVICE_METHOD = "GetQuote";
private final static String SERVICE_ENDPOINT = "stockquote.asmx";
private final static String STOCK_SYMBOL = "IBM";
public static void main(String args[]) {
try {
// Create a new SOAP connection
SOAPConnectionFactory soapConnectionFactory =
SOAPConnectionFactory.newInstance();
SOAPConnection soapConnection = soapConnectionFactory.createConnection();
// Send a SOAP Message to SOAP server
// Send this message to http://www.webserviceX.NET/stockquote.asmx
SOAPMessage soapResponse = soapConnection.call(
createSOAPRequest(STOCK_SYMBOL),
SERVICE_HOST + SERVICE_ENDPOINT);
// Close the connection
soapConnection.close();
} catch (Exception e) {
System.err.println("Fatal error occurred");
e.printStackTrace();
}
}
private static SOAPMessage createSOAPRequest(String stockSymbol)
throws Exception {
// Construct a new SOAP request message
MessageFactory messageFactory = MessageFactory.newInstance();
SOAPMessage soapMessage = messageFactory.createMessage();
// Construct the SOAP "body" with the method arguments
soapMessage.saveChanges();
// Print out the request message:
System.out.println("Sending SOAP request:");
soapMessage.writeTo(System.out);
System.out.printf("%n");
Search WWH ::




Custom Search