Before porting the web service module into a J2EE application, firstly lets look at how I get a stock quote in a normal Java concole.
As I will continue on application of WSDL on my next post ... .
Below is the full generic code to demostrate on calling a WSDL:
// // XMethods sample client for the Stock Quote service //
import java.io.*; import java.net.*; import java.util.*; import org.apache.soap.util.xml.*; import org.apache.soap.*; import org.apache.soap.rpc.*;
public class StockQuoteClient{
public static float getQuote (URL url, String symbol) throws Exception {
Call call = new Call ();
// Service uses standard SOAP encoding String encodingStyleURI = Constants.NS_URI_SOAP_ENC; call.setEncodingStyleURI(encodingStyleURI);
// Set service locator parameters call.setTargetObjectURI ("urn:xmethods-delayed-quotes"); call.setMethodName ("getQuote");
// Create input parameter vector Vector params = new Vector (); params.addElement (new Parameter("symbol", String.class, symbol, null)); call.setParams (params);
// Invoke the service .... Response resp = call.invoke (url,"");
// ... and evaluate the response if (resp.generatedFault ()) { throw new Exception(); } else { // Call was successful. Extract response parameter and return result Parameter result = resp.getReturnValue (); Float rate=(Float) result.getValue(); return rate.floatValue(); } }
// Driver to illustrate service invocation public static void main(String[] args) { try { URL url=new URL("http://services.xmethods.net:80/soap"); String symbol= args[0]; float quote = getQuote(url,symbol); System.out.println(quote); } catch (Exception e) {e.printStackTrace();} } } |
No comments:
Post a Comment