soap方式调用
import org.apache.soap.util.xml.*;
import org.apache.soap.*;
import org.apache.soap.rpc.*;
import java.io.*;
import java.net.*;
import java.util.Vector;
public class caService{
public static String getService(String user) {
URL url = null;
try {
url=new URL("http://192.168.0.100:8080/ca3/services/caSynrochnized");
} catch (MalformedURLException mue) {
return mue.getMessage();
}
// This is the main SOAP object
Call soapCall = new Call();
// Use SOAP encoding
soapCall.setEncodingStyleURI(Constants.NS_URI_SOAP_ENC);
// This is the remote object we're asking for the price
soapCall.setTargetObjectURI("urn:xmethods-caSynrochnized");
// This is the name of the method on the above object
soapCall.setMethodName("getUser");
// We need to send the ISBN number as an input parameter to the method
Vector soapParams = new Vector();
// name, type, value, encoding style
Parameter isbnParam = new Parameter("userName", String.class, user, null);
soapParams.addElement(isbnParam);
soapCall.setParams(soapParams);
try {
// Invoke the remote method on the object
Response soapResponse = soapCall.invoke(url,"");
// Check to see if there is an error, return "N/A"
if (soapResponse.generatedFault()) {
Fault fault = soapResponse.getFault();
String f = fault.getFaultString();
return f;
} else {
// read result
Parameter soapResult = soapResponse.getReturnValue ();
// get a string from the result
return soapResult.getValue().toString();
}
} catch (SOAPException se) {
return se.getMessage();
}
}
}
返回一维数组时
Parameter soapResult = soapResponse.getReturnValue();
String[] temp = (String[])soapResult.getValue();
调用ASP.Net生成的webservice
private String HelloWorld(String uri, String u) {
try {
SOAPMappingRegistry smr = new SOAPMappingRegistry();
StringDeserializer sd = new StringDeserializer();
ArraySerializer arraySer = new ArraySerializer();
BeanSerializer beanSer = new BeanSerializer();
smr.mapTypes(Constants.NS_URI_SOAP_ENC, new QName(
"http://tempuri.org/", "HelloWorldResult"), String.class,
null, sd);
smr.mapTypes(Constants.NS_URI_SOAP_ENC, new QName(
"http://tempuri.org/", "temp"), String.class,
beanSer, beanSer);
URL url = new URL(uri);
Call call = new Call();
call.setSOAPMappingRegistry(smr);
call.setTargetObjectURI("urn:xmethods-Service1");
call.setMethodName("HelloWorld");
call.setEncodingStyleURI(Constants.NS_URI_SOAP_ENC);
Vector soapParams = new Vector();
soapParams.addElement(new Parameter("temp", String.class, u, null));
call.setParams(soapParams);
Response soapResponse = call.invoke(url,"http://tempuri.org/HelloWorld");
if (soapResponse.generatedFault()) {
Fault fault = soapResponse.getFault();
System.out.println(fault);
} else {
Parameter soapResult = soapResponse.getReturnValue();
Object obj = soapResult.getValue();
System.out.println("===" + obj);
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}