Hi experts.

I have a bit of a problem. I'm not really into the web service scene but I know basic / intermediate Java. Thus my lack of experience in using Java to query web service. I'm using Java 6JDK. Hope some1 here will be kind enough to help

I have to query a web service that must will accept a XML document containing a few parameters (all in String format). The web service then gets the results and passes back a XML document containing the requested data.

My problem is 1stly I don't really know what java classes to use to call a web service and to receive it's response. I have been testing with the 'javax.xml.soap' package and have created the code below. But I keep getting errors. I think something is wrong with the XML but I'm not sure.

2ndly the only web service I have found that I can test with (free on the net) is 'http://www.mssoapinterop.org/asmx/simple.asmx'. My code below tries to connect to this web services 'echoString' method. If possible does anyone know of any really simple web services that I can use to practice my web queries on that take a XML document and return data in xml?

If anyone can help me out here it would be fantastic. I'm really willing to learn but I'm finding it hard to start

Thank you


My code (Best saved in file 'WebServiceInteraction.java' and then viewed):
Code:
/* WebServiceInteraction.java
 * Created on 1 March 2007, 12:36
 */

import javax.xml.soap.SOAPConnectionFactory;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.SOAPConnection;
import javax.xml.soap.SOAPMessage;
import javax.xml.soap.SOAPPart;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPBodyElement;
import javax.xml.soap.SOAPException;
import java.net.URL;
import java.net.MalformedURLException;
import org.w3c.dom.Document;

/**
 * This class controls the interaction with a web service using SOAP interaction.
 * It allows a XML query to be executed to a web service. It also allows
 * the XML responce to be parsed.
 */
public class WebServiceInteraction {
    SOAPConnection con;
    SOAPMessage message;
    SOAPMessage returnMessage;
    URL webService;


    /** Creates a new instance of WebServiceInteraction */
    public WebServiceInteraction(String url) {
        try {
																				// Create a soapConnection
            SOAPConnectionFactory soapFactory = SOAPConnectionFactory.newInstance();
            con = soapFactory.createConnection();

            MessageFactory messageFactory = MessageFactory.newInstance();		// Create a soapMessage
            message = messageFactory.createMessage();

            returnMessage = null;
            webService = new URL(url);
        } catch(MalformedURLException mURLe) {
 			System.out.println("Error: Invalid web service URL.\n" + mURLe);
        } catch(Exception e) {
 			System.out.println("Error: Could not create web service connection.\n" + e);
        }
    }


    /** Calls a web service with the data provided as an argument. */
    public String createCall(String XMLcall) {
        try {
																				// Get Soap objects needed to build call.
            SOAPPart soapPart = message.getSOAPPart();
            SOAPEnvelope soapEnvelope = soapPart.getEnvelope();
            SOAPBody soapBody = soapEnvelope.getBody();
            SOAPBodyElement bodyElement = soapBody.addBodyElement(soapEnvelope.createName("MCALookup"));

            bodyElement.addTextNode(XMLcall);									// Attached XML call information.

            returnMessage = con.call(message, webService);						// Send call to web service.

            if(returnMessage != null) {
                return getMCA();
            }
        } catch(SOAPException soape) {
 			System.out.println("Error: Error when calling web serivce.\n" + soape);
        } catch(Exception e) {
 			System.out.println("Error: Could not call web service.\n" + e);
        }
        return null;
    }


    /** Returns the return data from the web service. */
    private String getMCA() {
        try {
            																	// Get XML return.
            Document doc = returnMessage.getSOAPBody().extractContentAsDocument();
            if(doc != null) {
	            return "Doc = " + doc.toString();								// Returns a valid reply was obtained
	        } else {
	            return "Doc = null";											// Returns a invalid reply was obtained
			}
        } catch(SOAPException soape) {
 			System.out.println("Error: Error when parsing return from web service.\n" + soape);
        } catch(Exception e) {
 			System.out.println("Error: Could not parse return from web service.\n" + e);
        }
        return null;
    }


    /** Shutsdown the current connection. */
    public void shutdownServer() {
        try {
            con.close();
        } catch(Exception e) { }
        con = null;
        message = null;
        returnMessage = null;
    }


    /** Last method called to distruct a object of a class. */
    protected void finalize() {
        shutdownServer();
    }


    /** This class can be run to test a web service call.
     * Note this is for testing purposes only. This method should be overloaded
     * in actual usage.
     */
    public static void main(String[] args) {
		/* Change 'webURL' value to web service URL. */
		String webURL = "http://www.mssoapinterop.org/asmx/simple.asmx";

		/* Change 'xmlCallString' value to XML that must be sent to web service (input). */
		String xmlCallString = "POST /asmx/simple.asmx HTTP/1.1 Host: www.mssoapinterop.org Content-Type: text/xml; charset=utf-8 Content-Length: 9 SOAPAction: 'http://soapinterop.org/'  <?xml version='1.0' encoding='utf-8'?> <soap:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:soapenc='http://schemas.xmlsoap.org/soap/encoding/' xmlns:tns='http://soapinterop.org/' xmlns:types='http://soapinterop.org/encodedTypes' xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'>   <soap:Body soap:encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'>      <tns:echoString>       <inputString xsi:type='xsd:string'>HelloDuke</inputString>     </tns:echoString>   </soap:Body> </soap:Envelope>";
        WebServiceInteraction webInteraction = new WebServiceInteraction(webURL);	// Create an object of the WebServiceInteraction class.
																					// Generate a call to web service and print the returned result.
		System.out.println("Return value = " + webInteraction.createCall(xmlCallString));

        System.gc();
    }
}