java代码调用webservice接口 附调用接口工具

import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.rmi.RemoteException;

import javax.xml.rpc.ParameterMode;
import javax.xml.rpc.ServiceException;


import com.alibaba.fastjson.JSONObject;
import org.apache.axis.AxisFault;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.XMLType;

public class test{
	/**
	 * 接口地址
	 */
	private static final String address = "http://192.168.1.1:8080//services/WebService";

	/**
	 * targetNamespace
	 */
	private static final String targetNamespace = "http://localhost/services/HoauWorkflowService";
	/**
	 * @throws UnsupportedEncodingException
	 *
	 * 根据 接口方法和参数调用HR系统WebService
	 * @param method	方法名
	 * @param params	JSON
	 * @return
	 * @throws
	 */
	public static String getWebservice(String method,String params) throws UnsupportedEncodingException{
		String result = "";
		JSONObject resultObj = new JSONObject();
		try {
			//校验方法名
			URL url = new URL(address);
			Service service = new Service();
			Call call = (Call)service.createCall();
			call.setTargetEndpointAddress(url);
			call.setUseSOAPAction(true);
			call.setSOAPActionURI("");
			call.setEncodingStyle("UTF-8");//编码格式
			call.setTimeout(10000);//超时设置
			call.setProperty(org.apache.axis.client.Call.SEND_TYPE_ATTR, Boolean.FALSE);
			call.setProperty(org.apache.axis.AxisEngine.PROP_DOMULTIREFS, Boolean.FALSE);
			call.setSOAPVersion(org.apache.axis.soap.SOAPConstants.SOAP11_CONSTANTS);
			call.setOperationName(new javax.xml.namespace.QName(targetNamespace, method));
			call.addParameter("json", XMLType.XSD_STRING, ParameterMode.IN);
			call.setReturnType(XMLType.XSD_STRING);

			result = (String)call.invoke(new Object[]{params});
			System.out.println("+++调用接口返回的数据++++:"+result);
		} catch (ServiceException e) {
			resultObj.put("result", "1");
			resultObj.put("message", "接口访问异常!");
			return resultObj.toString();
		}catch(AxisFault e1){
			resultObj.put("result", "1");
			resultObj.put("message", "接口访问异常!");
			return resultObj.toString();
		}catch (RemoteException e) {
			resultObj.put("result", "1");
			resultObj.put("message", "接口访问异常!");
			return resultObj.toString();
		}catch(MalformedURLException e1){
			resultObj.put("result", "1");
			resultObj.put("message", "地址转换异常!");
			return resultObj.toString();
		}

		return result;
	}

}

调用方法实例test.getWebservice(“getData”, param)
接口方法名:getData,入参:param

附上调用webservice接口工具
storm
网址:https://pan.baidu.com/s/1QdjM2DZ8o5bIkdfD-WFRCw
提取码:hnn1

[使用参考]

https://blog.csdn.net/sunxiaopengsun/article/details/60137091

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值