不同方式调用webservice接口

package cn.mr.rw.service.orderSynchronize.emos.failureOrder;



import java.net.MalformedURLException;

import java.rmi.RemoteException;

import java.util.Date;



import javax.xml.namespace.QName;

import javax.xml.rpc.ServiceException;



import org.apache.axis.AxisFault;

import org.apache.axis.client.Call;

import org.apache.axis.client.Service;

import org.apache.axis2.addressing.EndpointReference;

import org.apache.axis2.client.Options;

import org.apache.axis2.rpc.client.RPCServiceClient;

import org.apache.commons.lang3.StringUtils;

import org.apache.commons.logging.LogFactory;



import cn.mr.common.exception.InvokeWebServiceException;

import cn.mr.rw.system.core.utils.LogUtils;



/**

 * WebService调用接口,无需事务控制。


 * 2013-11-7 下午6:38:12

 */

public class InvokeWebService {

	

	private static final org.apache.commons.logging.Log log = LogFactory.getLog(InvokeWebService.class);

	

	/**

	 * 使用拼装好的接口报文,调用指定WSDL路径的指定方法。该客户端使用AXIS1实现。


	 * @param xmlRequest 接口报文字符串。

	 * @param url 服务器端WSDL路径

	 * @param methodName 服务器端方法名

	 * @return 响应报文字符串。

	 * @throws InvokeWebServiceException

	 */

	public String callAxis(String xmlRequest, String url, String methodName) throws InvokeWebServiceException{

		String resObj = "";

		try {

			if(StringUtils.isBlank(url)){

				throw new InvokeWebServiceException("请提供访问WebService接口方法的URL");

			}

			if(StringUtils.isBlank(methodName)){

				throw new InvokeWebServiceException("请提供要调用的WebService接口方法!");

			}

			Service service = new Service();

			Call call = (Call) service.createCall();

            call.setTargetEndpointAddress(new java.net.URL(url));

            call.setOperationName(methodName);

			call.addParameter("xml",org.apache.axis.encoding.XMLType.XSD_STRING,javax.xml.rpc.ParameterMode.IN); 

			call.setUseSOAPAction(true); 

			call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);

			Object[] obj = new Object[] {xmlRequest};

			resObj = (String) call.invoke(obj);

		} catch (ServiceException e) {

			e.printStackTrace();

			throw new InvokeWebServiceException("创建服务出错!");

		} catch (AxisFault e) {

			e.printStackTrace();

			throw new InvokeWebServiceException("网络异常连接超时!");

		} catch (RemoteException e) {

		    e.printStackTrace();

		    throw new InvokeWebServiceException("调用远程方法出错!");

		} catch(MalformedURLException e){

		    e.printStackTrace();

            throw new InvokeWebServiceException("不能解析的URL!");

		} catch (Exception e) {

		    throw new InvokeWebServiceException("调用Webservice接口方法出现未知异常!");

		}

		return resObj;

	

	}

	

	/**

	 * 用多个参数调用webservice。

     * 使用拼装好的接口报文,调用指定WSDL路径的指定方法。该客户端使用AXIS1实现。



	 * @param xmlRequest

	 * @param url

	 * @param methodName

	 * @return

	 * @throws InvokeWebServiceException

	 */

	public String callAxisWithMultiParam(Object[] xmlRequest, String url, String namespace, String methodName) throws InvokeWebServiceException{

        String resObj = "";

        try {

            if(StringUtils.isBlank(url)){

                throw new InvokeWebServiceException("请提供访问WebService接口方法的URL");

            }

            if(StringUtils.isBlank(methodName)){

                throw new InvokeWebServiceException("请提供要调用的WebService接口方法!");

            }

            Service service = new Service();

            Call call = (Call) service.createCall();

            call.setTargetEndpointAddress(new java.net.URL(url));

            call.setOperationName(new QName(namespace, methodName));

            for(int i = 0; i < xmlRequest.length; i++){

                call.addParameter("xml",org.apache.axis.encoding.XMLType.XSD_STRING,javax.xml.rpc.ParameterMode.IN);

            }

            call.setUseSOAPAction(true); 

            call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);

            resObj = (String) call.invoke(xmlRequest);

        } catch (ServiceException e) {

            e.printStackTrace();

            throw new InvokeWebServiceException("创建服务出错!");

        } catch (AxisFault e) {

            e.printStackTrace();

            throw new InvokeWebServiceException("网络异常连接超时!");

        } catch (RemoteException e) {

            e.printStackTrace();

            throw new InvokeWebServiceException("调用远程方法出错!");

        } catch(MalformedURLException e){

            e.printStackTrace();

            throw new InvokeWebServiceException("不能解析的URL!");

        } catch (Exception e) {

            throw new InvokeWebServiceException("调用Webservice接口方法出现未知异常!");

        }

        return resObj;

    }

	

	/**

     * 如果服务器端是用Axis2实现的,客户端用Axis可能需要很长时间


     * @param wsdlUrl

     * @param nameSpace

     * @param method

     * @param param

     * @return

     * @throws AxisFault

     * @throws org.apache.axis2.AxisFault

     * @throws InvokeWebServiceException 

     */

    public String invokeAxis2MultiParam(String wsdlUrl, String nameSpace, String method, Object[] xmlRequest) throws InvokeWebServiceException {

        String response = "";

        try {

            RPCServiceClient client;

            client = new RPCServiceClient();

            Options options = client.getOptions();

            options.setTo(new EndpointReference(wsdlUrl));

            Object[] result;

            Date d1 = new Date();

            log.error("开始调用" + method + ",请求参数为" + xmlRequest);

            result = client.invokeBlocking(new QName(nameSpace, method), xmlRequest, new Class[]{String.class });

            response = (String) result[0];

            log.error("结束调用" + method +",耗时" + (new Date().getTime() - d1.getTime())/1000 + "秒,返回结果为" + response);

        } catch (org.apache.axis2.AxisFault e) {

            LogUtils.failureOrderClientLog.error(e.getMessage(), e);

            throw new InvokeWebServiceException("网络异常连接超时!");

        }

        

        return response;

    }

	

	/**

     * 如果服务器端是用Axis2实现的,客户端用Axis可能需要很长时间


     * @param wsdlUrl

     * @param nameSpace

     * @param method

     * @param param

     * @return

     * @throws AxisFault

     * @throws org.apache.axis2.AxisFault

     */

    public String invokeAxis2(String wsdlUrl, String nameSpace, String method, String param, Long timeOut) 

        throws InvokeWebServiceException {

        try {

            RPCServiceClient client;

            client = new RPCServiceClient();

            Options options = client.getOptions();

            options.setTo(new EndpointReference(wsdlUrl));

            if(timeOut != null && timeOut.longValue() > 0) {

                options.setTimeOutInMilliSeconds(timeOut);

            }

            Object[] result;

//            Date d1 = new Date();

//            log.error("开始调用" + method + ",请求参数为" + param);

            result = client.invokeBlocking(new QName(nameSpace, method), new Object[] {param}, new Class[]{String.class });

            String response = (String) result[0];

//            log.error("结束调用" + method +",耗时" + (new Date().getTime() - d1.getTime())/1000 + "秒,返回结果为" + response);

            return response;

        } catch (Exception e) {

            throw new InvokeWebServiceException("调用Axis2的服务器端失败", e);

        }

    }

    

    /**

     * 该构造方法针对java 客户端调用.net服务端时,必须传递namespace和soapURI参数

     * 使用拼装好的接口报文,调用指定WSDL路径的指定方法。该客户端使用AXIS1实现。


     * @param xmlRequest 接口报文字符串。

     * @param url 服务器端WSDL路径

     * @param methodName 服务器端方法名

     * @return 响应报文字符串。

     * @throws InvokeWebServiceException

     */

    public String callAxis(String namespace, String xmlRequest, String url, String methodName, String soapURI) throws InvokeWebServiceException{

        String resObj = "";

        try {

            if(StringUtils.isBlank(url)){

                throw new InvokeWebServiceException("请提供访问WebService接口方法的URL");

            }

            if(StringUtils.isBlank(methodName)){

                throw new InvokeWebServiceException("请提供要调用的WebService接口方法!");

            }

            if (StringUtils.isBlank(namespace)) {

                throw new InvokeWebServiceException("请提供访问WebService接口命名空间的namespace!");

            }

            if (StringUtils.isBlank(soapURI)) {

                throw new InvokeWebServiceException("请提供访问WebService接口参数soapURI!");

            }

            Service service = new Service();

            Call call = (Call) service.createCall();

            call.setTargetEndpointAddress(new java.net.URL(url));

            call.setOperationName(new QName(namespace, methodName));

            call.addParameter(new QName(namespace,"xml"), org.apache.axis.encoding.XMLType.XSD_STRING, javax.xml.rpc.ParameterMode.IN); 

            call.setUseSOAPAction(true); 

            call.setSOAPActionURI(soapURI);

            call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);

            Object[] obj = new Object[] {xmlRequest};

            resObj = (String) call.invoke(obj);

        } catch (ServiceException e) {

            e.printStackTrace();

            throw new InvokeWebServiceException("创建服务出错!");

        } catch (AxisFault e) {

            e.printStackTrace();

            throw new InvokeWebServiceException("网络异常连接超时!");

        } catch (RemoteException e) {

            e.printStackTrace();

            throw new InvokeWebServiceException("调用远程方法出错!");

        } catch(MalformedURLException e){

            e.printStackTrace();

            throw new InvokeWebServiceException("不能解析的URL!");

        } catch (Exception e) {

            throw new InvokeWebServiceException("调用Webservice接口方法出现未知异常!");

        }

        return resObj;

    

    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值