1.RPC方式调用 RPCServiceClient方式不能携带指定参数,下列为ServiceClient方式: public static void main(String[] args) { try { String soapBindingAddress = "http://192.168.10.1:8080/BillService.asmx?wsdl"; ServiceClient se
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>