axis调用webservice服务

1.axis1 调用

    **首先要引入jar:
    <dependency>
		<groupId>axis</groupId>
		<artifactId>axis</artifactId>
		<version>1.4</version>
	</dependency>** 


 =================================================================================================================
    /**
	 * axis1请求webservice接口
	 * @param wsdlUrl                        wsdl服务地址 
	 * @param targetNameSpace       命名空间
	 * @param parameName              参数名称
	 * @param soapAction                  调用方法
	 * @param parameValue               参数值   注意参数名称和值要一一对应
	 * @return
	 */
	 public static String  postByAxis(String wsdlUrl,String targetNameSpace,String method,String[]paramName,Object[]parameValue) {
		    String  returnMsg = "";
		    String returnXml =  "";
			try {
				
				 Service service = new Service();
				 Call call = (Call) service.createCall();
				 call.setTargetEndpointAddress(new java.net.URL(wsdlUrl));
				 //方法名称
				 call.setOperationName(new QName(targetNameSpace,method));
				 
				 //参数名称
				 for (String param : paramName) {
					 call.addParameter(new QName(targetNameSpace,param),XMLType.XSD_STRING, ParameterMode.IN);
				 }
				 
				//启用soap
				call.setUseSOAPAction(true);
				//返回参数的类型(不能用Array,否则报错)
				call.setReturnType(XMLType.XSD_STRING); 
				//接口中调用方法的soapAction
				call.setSOAPActionURI(targetNameSpace+"/"+method); 
				SOAPService soap = new SOAPService();
				//接口中定义的webservice名称
				soap.setName("VmsSdkWebService");
				call.setSOAPService(soap);
				returnMsg = (String) call.invoke(parameValue);
				
				LOGGER.info("returnMsg==>"+returnMsg);
				//去掉转义字符
		        returnXml = StringEscapeUtils.unescapeHtml4(returnMsg); 
		        System.out.println("returnMsg==convert====>"+returnXml);
		        //获取返回结果
				 
			} catch (MalformedURLException e) {
				e.printStackTrace();
			} catch (ServiceException e) {
				e.printStackTrace();
			} catch (RemoteException e) {
				e.printStackTrace();
			}
		 return returnXml;
	 } 

================================================================================================
Axis2 调用

axis2-adb-1.6.2.jar;
axis2-kernel-1.6.2.jar
axis2-transport-http-1.6.2.jar
axis2-transport-local-1.6.2.jar

导入的类:

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

================================================================================================

  /**
	 * Axis2请求webservice接口
	 * @param wsdlUrl
	 * @param targetNameSpace
	 * @param parameName
	 * @param soapAction
	 * @param parameValue
	 * @return
	 */
	 public static String  excute_axis_2(String wsdlUrl,String targetNameSpace,String method,Object[]parameValue) {
		    RPCServiceClient client;
		    Options options;
		    String results ="";
			try {
				    client = new RPCServiceClient();
				    options = client.getOptions();
			        options.setTo(new EndpointReference(wsdlUrl));
			        options.setTimeOutInMilliSeconds(1000 * 60 * 5);// 毫秒单位
			        options.setAction(method);
			        
			        Object[] response = client.invokeBlocking(new QName(targetNameSpace, method), parameValue, new Class[]{String.class});
			        results = (String) response[0];
			        
			} catch (AxisFault e) {
				e.printStackTrace();
			}
			return results;
	 }

这里的方法axis2 较于 axis可以不用传字段名称,只需要按接口入参顺序设置入参的值。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值