Client Code
The complete client code is shown
in Example 5-12. The goal of the client code is to
call the remote getCounter( )
method and retrieve
the current count value.
package com.ecerami.soap; /** * A Sample SOAP Client * Retrieves Current Counter value from CounterService * Illustrates Session v. Application Scope */ import java.util.*; import java.net.*; import org.apache.soap.*; import org.apache.soap.rpc.*; public class CounterClient {private Call call; // Reusable Call Object
/** * Static Main method */ public static void main (String[] args) { System.out.println ("Session/Application Counter: SOAP Client"); CounterClient counterClient = new CounterClient( ); counterClient.process( ); } /** * Constructor * Create reusable Call object */public CounterClient ( ) {
call = new Call( );
}
/** * Start counting */ public void process ( ) { try { for (int i=0; i<5; i++) { int counter = getCounter ( ); System.out.println ("Counter: "+counter); } } catch (CounterException e) { System.err.println (e); } catch (SOAPException e) { System.err.println (e); } catch (MalformedURLException e) { System.err.println (e); } } /** * getCounter Method */ public int getCounter ( ) throws SOAPException, MalformedURLException, CounterException { // Set Encoding Style to standard SOAP encoding call.setEncodingStyleURI(Constants.NS_URI_SOAP_ENC); // Set Object URI and Method Name call.setTargetObjectURI ("urn:examples:counterservice"); call.setMethodName ("getCounter"); ...
Get Web Services Essentials now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.