Java Reference
In-Depth Information
NOTE
This example uses a publicly available free web service available at
http://webservicex.net
.
The web
services on the site are implemented using Microsoft.NET. There are a few things that are done
slightly differently on their side of the WSDL that have a subtle effect on how you must create your
requests. For example, in Java, you may be used to specifying namespaces with a prefix such as
“tns:”. But if you read the WebserviceX schema, its
GetQuoteResponse
is specified without a pre-
fix, like this:
<GetQuoteResponse xmlns="http://www.webserviceX.NET/">
You have to keep that in mind when creating your
QName
, and do so like this:
QName q = new QName("http://www.webserviceX.NET/","GetQuote");
If you do specify a prefix, you'll get an error back in the response.
The web service you're going to invoke is available at
http://www.webservicex.net/WCF/Ser-
viceDetails.aspx?SID=19
if you'd like to read the WSDL and schema. It asks for a ticker sym-
bol and returns some data about the stock in response. The code in
Example 5-7
shows how
to build the complete client. Here you just build a SAAJ client, specify the
SOAPAction
as
required, and send your request for information about the JAVA symbol.
Example5-7.Adding a SOAPAction header as required by this .NET web service
package addSoapAction;
import java.net.MalformedURLException;
import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.SOAPMessage;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPBodyElement;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPException;
import javax.xml.ws.Dispatch;
import javax.xml.ws.Service;
/**
* Shows how to add a MIME header, of which SOAPAction is one.
*/
public class AddSoapAction {
private static final String WSDL =
"http://www.webservicex.net/stockquote.asmx?wsdl";