加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

Axis、Axis2调用webservice

发布时间:2020-12-17 01:13:18 所属栏目:安全 来源:网络整理
导读:1.下载axis-bin-1_4.zip、axis2-1.5.5-bin.zip。 2.新建java project,然后在java build path 的libraries中add external jars,将axis-bin-1_4与axis2-1.5.5-bin解压缩后的lib目录中的jar添加进项目 3.编码调用webservice import java.lang.reflect.Array;

1.下载axis-bin-1_4.zip、axis2-1.5.5-bin.zip。

2.新建java project,然后在java build path 的libraries中add external jars,将axis-bin-1_4与axis2-1.5.5-bin解压缩后的lib目录中的jar添加进项目

3.编码调用webservice


import java.lang.reflect.Array;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.security.MessageDigest;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;
import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;
import org.apache.axiom.soap.SOAP11Constants;
import org.apache.axis2.AxisFault;
import org.apache.axis2.Constants;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;


private static EndpointReference targetEPR = new EndpointReference("http://www.test.com:967/Test.asmx?WSDL");//接口WebService地址

try{

OMFactory fac = OMAbstractFactory.getOMFactory();

OMNamespace omNs = fac.createOMNamespace("http://www.testnamespace.net/","tns");//命名空间

OMElement method = fac.createOMElement("methodname",omNs);//方法名称


//请求参数设置
Options options = new Options();
options.setTo(targetEPR);//设定webservice地址
options.setTransportInProtocol(Constants.TRANSPORT_HTTP);//设定传输协议
options.setSoapVersionURI(SOAP12Constants.SOAP_ENVELOPE_NAMESPACE_URI);//设定SOAP版本soap1.2


//客户端绑定参数设置
ServiceClient sender = new ServiceClient();
sender.setOptions(options);


//设定访问的接口方法

OMElement method2 = fac.createOMElement("calledmethodname",omNs);//要调用的接口方法名称
OMElement value1 = fac.createOMElement("EnterArgument",omNs);//方法的第一个参数名称
value1.addChild(fac.createOMText(value1,"参数值1"));//设定参数的值
method2.addChild(value1);//方法设置参数

//设定其他方法参数,针对参数是数组的情况如何处理?可以考虑为参数添加child,

//............


OMElement result2 = sender.sendReceive(method2);//调用接口方法
System.out.println(result2);//打印接口返回结果

//处理result2


}

?catch (Exception e) { ??????????? e.printStackTrace(); }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读