在Java代码中用Axis2调用DotNet语言的WebService程序

阅读更多
package com.shihuan.ymail.common.utils.wsutil;

import java.util.List;

import javax.xml.namespace.QName;

import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;
import org.apache.axiom.soap.SOAP11Constants;
import org.apache.axis2.AxisFault;
import org.apache.axis2.Constants;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.rpc.client.RPCServiceClient;

import com.shihuan.mypro.common.utils.exception.ParamException;

public class WsClientUtil {
	
	/**
	 * 功能: 调用某个DotNet语言的WebService的服务
	 * @param serverurl 访问WebService服务端的Url地址  (例如: http://app.shihuan.com.cn/MailWebService/MailWebService.asmx)
	 * @param soapAction WebService服务端的soapAction (例如: http://192.168.0.1/MailWebService/MailWebService.asmx/AccessCheck)
	 * @param omNamespace WebService服务端的命名空间  (例如: http://192.168.0.1/MailWebService/MailWebService.asmx)
	 * @param methodname WebService服务端的某个方法名字  (例如: AccessCheck)
	 * @param listParams WebService服务端要接受的参数
	 * @return result OMElement类型在xml格式内容
	 * @throws AxisFault  
	 * @throws ParamException 
	 */
	public static OMElement getDotNetResult(String serverurl, String soapAction, String omNamespace, String methodname, List listParams) throws AxisFault, ParamException {
		
		if("".equals(serverurl) || "".equals(soapAction) || "".equals(omNamespace) || "".equals(methodname) || listParams==null){
			throw new ParamException(String.format("调用WebService服务的参数不合法! serverurl=%1$s, soapAction=%2$s, omNamespace=%3$s, methodname=%4$s", serverurl, soapAction, omNamespace, methodname));
		}
		
		RPCServiceClient serviceClient;
		try {
			serviceClient = new RPCServiceClient();
			
			EndpointReference targetEPR = new EndpointReference(serverurl);
			Options options = new Options();
	        options.setSoapVersionURI(SOAP11Constants.SOAP_ENVELOPE_NAMESPACE_URI);
	        options.setAction(soapAction);
	        options.setTo(targetEPR);
	        //options.setProperty(propertyKey, property)
	        // enabling MTOM in the client side
	        // options.setProperty(Constants.Configuration.ENABLE_MTOM, Constants.VALUE_TRUE);
	        options.setTransportInProtocol(Constants.TRANSPORT_HTTP);
	        serviceClient.setOptions(options);
	        
	        OMFactory fac = OMAbstractFactory.getOMFactory();
	        OMNamespace omNs = fac.createOMNamespace(omNamespace, "");
	        OMElement data = fac.createOMElement(methodname, omNs);
	        
	        OMElement tmpObj = null;
	        for (String[] arrs : listParams) {
	        	tmpObj = fac.createOMElement(arrs[0], omNs);
	        	tmpObj.setText(arrs[1]);
	        	data.addChild(tmpObj);
			}
	        
	        OMElement result = serviceClient.sendReceive(data);
	        
//	        System.out.println(result);
	        
	        return result;
	        
		} catch (AxisFault e) {
			throw e;
		}
        
    }
	
	
	public static void main(String[] args) {
		try {
			List listParams = new ArrayList();
			String[] p1 = new String[]{"userName", "easeye"};
			listParams.add(p1);
			String[] p2 = new String[]{"password", "123456"};
			listParams.add(p2);
			
			WsClientUtil.getDotNetResult("http://app.shihuan.com.cn/MailWebService/MailWebService.asmx", "http://192.168.0.1/MailWebService/MailWebService.asmx/AccessCheck", "http://192.168.0.1/MailWebService/MailWebService.asmx", "AccessCheck", listParams);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
}

你可能感兴趣的:(axis2,DotNet,webservice)