webService客户端工具类

package com.dg11185.zhjy.commonUtils;


import javax.xml.namespace.QName;


import org.apache.axis2.AxisFault;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.rpc.client.RPCServiceClient;
import org.apache.log4j.Logger;


/*******************************************************************************
 * web Service 客户端工具类
 * 
 ******************************************************************************/
public class WebServiceClient {

	private static final Logger log = Logger.getLogger(WebServiceClient.class);


	
	/**
	 * WebService客户端请求
	 * 
	 * @param wsURL
	 * @param nameSpace
	 * @param mehtod
	 * @param xml
	 ***/
	public static String findWebSerivceResultAsString(String wsURL,
			String nameSpace, String method, String xml){
		log.info("-------------findWebSerivceResultAsString-->method:"+method+",URL:"+wsURL);
		log.info("-------------findWebSerivceResultAsString-->xml:"+xml);
		return findWebSerivceResultAsString(wsURL, nameSpace, method, new Object[]{xml});
	}
	
	/**
	 * WebService客户端请求
	 * 
	 * @param wsURL
	 * @param nameSpace
	 * @param mehtod
	 * @param args
	 ***/
	public static String findWebSerivceResultAsString(String wsURL,
			String nameSpace, String method, Object[] args) {
		long startTime = System.currentTimeMillis();
		try {
			RPCServiceClient client = new RPCServiceClient();
			Options options = client.getOptions();
			// 设置调用WebService的URL
			EndpointReference epf = new EndpointReference(wsURL);
			//设置超时
			options.setTimeOutInMilliSeconds(Long.parseLong("1000*8"));
			options.setTo(epf);
			//查看wsdl的空间名targetNamespace
			QName qname = new QName(nameSpace, method);
			Object[] result = client.invokeBlocking(qname, args,
					new Class[] { String.class });
			if (null != result && result.length > 0) {
				String rs = (String) result[0];
				log.info("---------------->result:"+rs);
				return rs;
			}
		} catch (AxisFault e) {
			e.printStackTrace();
			log.error("--------------Exception-->msg:"+e.getMessage());
		}finally{
			log.info("--------------useTimes:"+(System.currentTimeMillis()-startTime));
		}
		return null;
	}
}


你可能感兴趣的:(webService客户端工具类)