Sunday, June 19, 2016

SOAP and Axis WebService Client

import java.net.URL;
import java.util.Vector;
import org.apache.soap.Constants;
import org.apache.soap.rpc.Call;
import org.apache.soap.rpc.Parameter;
import org.apache.soap.rpc.Response;

public class test {
  public static void main( String[] args ) throws Exception {
   // soap service endpoint
    URL url = new URL( "http://localhost:8080/soap/servlet/rpcrouter" );
    // create a call
   Call call = new Call();
    // Service uses standard SOAP encoding
    call.setEncodingStyleURI( Constants.NS_URI_SOAP_ENC );
    // Set service locator parameters
    call.setTargetObjectURI( "urn:ex" );
    call.setMethodName( "rstring" );
    // Create input parameter vector
    Vector params = new Vector();
    params.addElement( new Parameter( "s", String.class, "hai da...", null ) );
    call.setParams( params );
    // Invoke the service, note that an empty SOAPActionURI of
    // "" indicates that intent of the SOAP request is taken to
    // be the request URI
    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();
      System.out.println( "temperature is -> " + result.getValue() );
    }
  }
}



import java.net.URL;
import java.util.Vector;
import org.apache.soap.Constants;
import org.apache.soap.Fault;
import org.apache.soap.rpc.Call;
import org.apache.soap.rpc.Parameter;
import org.apache.soap.rpc.Response;


public class test2{

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

System.out.println("hi how are you..");

Call call= new Call();
call.setEncodingStyleURI(Constants.NS_URI_SOAP_ENC);
call.setTargetObjectURI("urn:ex1");

Parameter param1=new Parameter("s",String.class,"99",null);

Vector paramlist=new Vector();
paramlist.addElement(param1);

call.setMethodName("convert");
call.setParams(paramlist);

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

Response result= call.invoke(url,"");

if(result.generatedFault()){
Fault f= result.getFault();
System.out.println(f.getFaultCode());
System.out.println(f.getFaultString());

}
else{

Parameter p=result.getReturnValue();
System.out.println(p.getValue());

}


}

}

/*
 * (#) ArithClient.java  Mar 19, 2012
 *
 */

import org.apache.axiom.om.*;
import org.apache.axis2.client.*;
import org.apache.axis2.addressing.*;

public class ArithClient {

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

    String wsdlDocument = "http://localhost:8080/axis2/services/arith?wsdl";
    EndpointReference targetEPR = new EndpointReference(wsdlDocument);
    OMFactory fac = OMAbstractFactory.getOMFactory();
    OMNamespace omNs = fac.createOMNamespace("http://arith", "arith");
    OMElement method = fac.createOMElement("add", omNs);

    OMElement paramOne = fac.createOMElement("a", omNs);
    paramOne.setText("12");
    method.addChild(paramOne);
    OMElement paramTwo = fac.createOMElement("b", omNs);
    paramTwo.setText("10");
    method.addChild(paramTwo);

    ServiceClient client = new ServiceClient();
    Options opts = new Options();
    opts.setTo(targetEPR);
    client.setOptions(opts);

    OMElement res = client.sendReceive(method);
    String result = res.getFirstElement().getText();

    System.out.println("Result: " + result);
  }
}

No comments:

Post a Comment

உப்பு மாங்காய்

சுருக்குப்பை கிழவி. சுருக்கங்கள் சூழ் கிழவி. பார்க்கும் போதெல்லாம் கூடையுடனே குடியிருப்பாள். கூடை நிறைய குட்டி குட்டி மாங்காய்கள். வெட்டிக்க...