需要axis.jar,commons-discovery-1.0.jar,commons-logging-1.0.4.jar,wsdl4j-1.6.jar
import javax.xml.namespace.QName;
import javax.xml.rpc.ParameterMode;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.XMLType;
public class WebServiceHelper {
public static String callWS(String url,String method,String returnType,String[] params,String[] types,String[] values,String targetNamespace) throws Exception{
Service service = new Service();
Call call = (Call) service.createCall();
call.setTargetEndpointAddress(new java.net.URL(url));
call.setOperationName(new QName(targetNamespace,method));//要访问的方法
for(int i = 0; i < params.length; i++){
call.addParameter(new QName(targetNamespace, params[i]),getWSType(types[i]), ParameterMode.IN);
}
call.setReturnType(getWSType(returnType)); //设置返回值类型
String i=(String)call.invoke(values);
return i;
}
private static QName getWSType(String typeStr){
QName type = null;
if("String".equalsIgnoreCase(typeStr)){
type = XMLType.XSD_STRING;
}else if("Boolean".equalsIgnoreCase(typeStr)){
type = XMLType.XSD_BOOLEAN;
}else if("Int".equalsIgnoreCase(typeStr)){
type = XMLType.XSD_INT;
}else{
type = XMLType.XSD_STRING;
}
return type;
}
}
本文提供了一个使用Java调用WebService的示例代码。该示例展示了如何通过Axis库设置服务URL、操作名称及参数等,实现跨系统的数据交互。
983

被折叠的 条评论
为什么被折叠?



