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

WebService客户端Axis2调用

发布时间:2020-12-16 22:07:49 所属栏目:安全 来源:网络整理
导读:1.RPC方式调用 RPCServiceClient方式不能携带指定参数,下列为ServiceClient方式: public static void main(String[] args) { try { String soapBindingAddress = "http://192.168.10.1:8080/BillService.asmx?wsdl"; ServiceClient sender = new ServiceCl

1.RPC方式调用

RPCServiceClient方式不能携带指定参数,下列为ServiceClient方式:

public static void main(String[] args) {
        try {
            String soapBindingAddress = "http://192.168.10.1:8080/BillService.asmx?wsdl";

            ServiceClient sender = new ServiceClient();
            EndpointReference endpointReference = new EndpointReference(soapBindingAddress);
            Options options = new Options();
<span style="white-space:pre">	</span>    // 命名空间加方法名
            options.setAction("http://tempuri.org/PuBillVouchInterface");
            options.setTo(endpointReference);
            sender.setOptions(options);

            OMFactory fac = OMAbstractFactory.getOMFactory();
            // 设置命名空间
            OMNamespace omNs = fac.createOMNamespace("http://tempuri.org/","PuBillVouchInterface");
            OMElement data = fac.createOMElement("PuBillVouchInterface",omNs);

            
            String dataXml = "<?xml version='1.0' encoding='UTF-8'?>" +
                    "<DataTable>" +
                        "<DataRow>" +
                            "<bill_no>AB002324E1</bill_no>" +
                            "<ddate>2016-7-22</ddate>" +
                            "<cBillType>2</cBillType>" +
                            "<DataDetails>" +
                                "<iquantity>1</iquantity>" +
                                "<iprice>12.2</iprice>" +
                                "<isum>12.2</isum>" +
                            "</DataDetails>" +
                            "<DataDetails>" +
                                "<iquantity>12</iquantity>" +
                                "<iprice>30</iprice>" +
                                "<isum>360</isum>" +
                            "</DataDetails>" +
                        "</DataRow>" +
                    "</DataTable>";

            String md5 = DigestUtils.md5Hex("md5key" + dataXml).toUpperCase();

            // 对应参数的节点
            String[] strs = new String[]{"dataXml","certificateHeader"};
            // 参数值
            String[] val = new String[]{dataXml,md5};
            for (int i = 0; i < strs.length; i++) {
                OMElement inner = fac.createOMElement(strs[i],omNs);
                inner.setText(val[i]);
                data.addChild(inner);
            }
            // 发送数据,返回结果
            OMElement result = sender.sendReceive(data);
            System.out.println(result.toString());
            System.out.println(result.getFirstElement().getText());
        } catch (AxisFault ex) {
            ex.printStackTrace();
        }
    }

2.借助idea生成类文件进行调用

首先下载axis2包文件,地址:http://axis.apache.org/axis2/java/core/download.cgi?

解压后配置如下:



在项目某目录下,右键-->WebService-->Generate Java Code From Wsdl,按提示配置生成文件即可。生成YBServiceCallbackHandler.java、YBServiceStub两个文件

调用示例如下:

public static void main(String[] args) {
        try {
            String dataXml = "<?xml version='1.0' encoding='UTF-8'?>" +
                    "<DataTable>" +
                        "<DataRow>" +
                            "<bill_no>1322k322g2301</bill_no>" +
                            "<ddate>2016-7-8</ddate>"+
                        "</DataRow>" +
                    "</DataTable>";

            String md5 = DigestUtils.md5Hex("md5key" + dataXml).toUpperCase();

            YBServiceStub stub = new YBServiceStub("http://192.168.10.1:8080/BillService.asmx?wsdl");
            YBServiceStub.PuBillVouchInterface request = new YBServiceStub.PuBillVouchInterface();

            request.setCertificateHeader(md5);
            request.setDataXml(dataXml);
            YBServiceStub.PuBillVouchInterfaceResponse rs = stub.puBillVouchInterface(request);
            System.out.println(rs.getPuBillVouchInterfaceResult());

        } catch (Exception axisFault) {
            axisFault.printStackTrace();
        }
    }


需要Axis2相关依赖:

<properties>
    <axis2.version>1.6.2</axis2.version>
</properties>

<!-- axis2 -->
<dependency>
<groupId>org.apache.axis2</groupId>
    <artifactId>axis2</artifactId>
    <version>${axis2.version}</version>
</dependency>

<dependency>
<groupId>org.apache.axis2</groupId>
    <artifactId>axis2-transport-http</artifactId>
    <version>${axis2.version}</version>
</dependency>

<dependency>
<groupId>org.apache.axis2</groupId>
    <artifactId>axis2-transport-local</artifactId>
    <version>${axis2.version}</version>
</dependency>

<dependency>
    <groupId>org.apache.xmlbeans</groupId>
    <artifactId>xmlbeans</artifactId>
    <version>2.4.0</version>
</dependency>

(编辑:李大同)

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

    推荐文章
      热点阅读