Axis接口工具类



import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.XMLType;

import javax.xml.namespace.QName;
import javax.xml.rpc.ParameterMode;

/**
 * Axis发送请求工具类
 */
public class AxisUtils{

	/**
	 * axis方式调用接口
	 * @return_type:String		返回类型
	 * @param url				地址
	 * @param targetNamespace	命名空间
	 * @param methodName		调用方法名
	 * @param paramName			参数名
	 * @param paramValue		参数值
	 * @param outTime			超时时间(单位毫秒)
	 */
	public static String axisCall(String url,String targetNamespace, String methodName, String paramName, String paramValue,Integer outTime) throws Exception {
		Service service = new Service();
		Object[] object = new Object[1];  
		object[0] = paramValue;//Object是用来存储方法的参数
		String result = "no result!"; 
		try {
			//创建服务方法的调用者对象call,设置call对象的属性
			Call call = (Call) service.createCall();
			call.setTargetEndpointAddress(url);// 设置服务终端地址
			//设置NameSpace和方法
			QName opAddEntry = new QName(targetNamespace, methodName);
			//请求对象设置QName对象
			call.setOperationName(opAddEntry);
			if(null != outTime){
				//超时时间控制
				call.setTimeout(outTime);//单位是毫秒
			}
			//设置参数名:  
			call.addParameter(paramName, // 参数名  ‘arg0’
					XMLType.XSD_STRING,// 参数类型:String  
					ParameterMode.IN);// 参数模式:'IN' or 'OUT'
			//设置返回值类型:  
			call.setReturnType(XMLType.XSD_STRING);// 返回值类型:String  
			call.setEncodingStyle("UTF-8");
			//Basic Auto 认证
			//call.getMessageContext().setUsername("test");
			//call.getMessageContext().setPassword("test");
			System.out.println("\n"+"参数:"+"\n"+paramValue);
			result = (String) call.invoke(object);// 远程调用  
			System.out.println("\n"+"结果: "+"\n" + result);
			System.out.println("调用结束");
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("\n"+"结果: "+"\n" + result);
			System.out.println("调用结束");
			throw new Exception("调用接口报错:"+e.getMessage());
		}
		return result;
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值