Axis调用Web Service助手类

需要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;
	}
}

你可能感兴趣的:(apache,xml,Web,.net)