You need to specify SOAPContext in the list of arguments. I took an example
from
http://www-106.ibm.com/developerworks/library/ws-peer2/
and added session control to it.

SERVER SIDE METHOD

public float getRate( org.apache.soap.rpc.SOAPContext cnt, String country1,
String country2 ) throws SOAPException
    {
    HttpServletRequest request = (HttpServletRequest)
cnt.getPropert(Constants.BAG_HTTPSERVLETREQUEST );
    HttpSession session = (HttpSession)
cnt.getProperty(Constants.BAG_HTTPSESSION);

    String host = request.getServerName();
    int port = request.getServerPort();
    String protocol = request.getScheme();
    String wsuri = request.getRequestURI();
    String sess = session.getId();
    return 123.4F;
    }

CLIENT SIDE
As you will see here, the method called is getRate, since its called only
with 2 parameters. Since it will not match to our method on the server side
that requires 3 parameters, ApacheSOAP will automatically attempt to call
the method again with SOAPContext as a first argument in the list. Bingo, we
are in.

public class Client
   {
   public static void main( String[] args ) throws Exception
     {

     URL url = new URL( "http://localhost:8080/soap/servlet/rpcrouter"; );
     String urn = "urn:demo1:exchange";

     Call call = new Call(); // prepare the service invocation
     call.setTargetObjectURI( urn );
     call.setMethodName( "getRate" );
     call.setEncodingStyleURI( Constants.NS_URI_SOAP_ENC );
     SOAPHTTPConnection shc = new SOAPHTTPConnection ();
     shc.setMaintainSession (true);
     call.setSOAPTransport(shc);

     Vector params = new Vector();
     params.addElement( new Parameter( "country1", String.class, "USA",
null ) );
     params.addElement( new Parameter( "country2", String.class, "Japan",
null ) );
     call.setParams( params );

     try
       {
       System.out.println( "invoke service\n" + "  URL= " + url + "\n  URN
=" + urn );
       Response response = call.invoke( url, "" ); // invoke the service
       if( !response.generatedFault() )
         {
         Parameter result = response.getReturnValue(); // response was OK
         System.out.println( "Result= " + result.getValue() );
         }
       else
         {
         Fault f = response.getFault(); // an error occurred
         System.err.println( "Fault= " + f.getFaultCode() + ", " +
           f.getFaultString() );
         }
       }
     catch( SOAPException e ) // call could not be sent properly
       {
       System.err.println( "SOAPException= " + e.getFaultCode() + ", " +
         e.getMessage() );
       }
     }
   }

Sincerely,
Oleg Timofeyev
____________________________________________

Reply via email to