java调用soap接口

参考:
java调用webservice接口的几种方法
2021/6/25 SOAP学习-WebService全套实战教程

1、使用wsdl2java把WSDL文件转成本地类

参考 例子
2021/6/25 SOAP学习-WebService全套实战教程

public static void way1(){

        /**
         * 1、wsdl网址
         * http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl
         *
         * 2、在需要放反编译代码的位置输入命令
         * wsimport -s . http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl
         *
         * 3、找标签 service portType operation
         * <wsdl:service name="MobileCodeWS"></wsdl:service> 找到服务类 MobileCodeWS
         * <wsdl:portType name="MobileCodeWSSoap"> 找到接口实现类 MobileCodeWSSoap
         * <wsdl:operation name="getDatabaseInfo"> 找到具体方法 getDatabaseInfo
         *
         * 4、开始以下代码
         */
        try {
            cn.com.webxml.MobileCodeWS mobileCodeWS = new cn.com.webxml.MobileCodeWS();
            cn.com.webxml.MobileCodeWSSoap port=mobileCodeWS.getPort(cn.com.webxml.MobileCodeWSSoap.class);
            String query=port.getMobileCodeInfo("18888888888",null);
            System.out.println(query);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

2、直接AXIS调用远程的web service

例子的网址是不行的,换一个可能就行了

try {
            /**
             * http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl
             * MobileCodeWS
             * MobileCodeWSSoap
             * getMobileCodeInfo
             * 18888888888
             */
            String endpoint = "http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl";//?wsdl
            //直接引用远程的wsdl文件
            //以下都是套路
            Service service = new Service();
            Call call = (Call) service.createCall();
            call.setTargetEndpointAddress(new URL(endpoint));
            call.setOperationName(new QName("getMobileCodeInfo"));//WSDL里面描述的方法名称
            // call.addParameter("getMobileCodeInfo", org.apache.axis.encoding.XMLType.XSD_DATE,
            //         javax.xml.rpc.ParameterMode.IN);//接口的参数
            // call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);//设置返回类型
            call.addParameter( "mobileCode", org.apache.axis.encoding.XMLType.XSD_STRING, ParameterMode.IN );//调用方法参数1
            call.addParameter( "userID", org.apache.axis.encoding.XMLType.XSD_STRING, ParameterMode.IN );//调用方法参数2
            call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);//设置返回类型

            call.setSOAPActionURI("http://WebXml.com.cn/"+"getMobileCodeInfo");//命名空间+调用方法名
            String temp = "18888888888";
            String result = (String) call.invoke(new Object[]{temp,""});//给方法传递参数,并且调用方法
            System.out.println("result is " + result);
        } catch (Exception e) {
            System.err.println(e.toString());
        }

3、直接SOAP调用远程的webservice

参考
java调用webservice接口的几种方法
我没试成功,先不深究了

import org.apache.soap.util.xml.*;
import org.apache.soap.*;
import org.apache.soap.rpc.*;
import java.io.*;
import java.net.*;
import java.util.Vector;

public class caService {
    public static String getService(String user) {
        URL url = null;
        try {
            url = new URL("http://192.168.0.100:8080/ca3/services/caSynrochnized");
        } catch (MalformedURLException mue) {
            return mue.getMessage();
        }
        
        // This is the main SOAP object
        Call soapCall = new Call();
        
        // Use SOAP encoding
        soapCall.setEncodingStyleURI(Constants.NS_URI_SOAP_ENC);
        
        // This is the remote object we're asking for the price
        soapCall.setTargetObjectURI("urn:xmethods-caSynrochnized");
        
        // This is the name of the method on the above object
        soapCall.setMethodName("getUser");
        
        // We need to send the ISBN number as an input parameter to the method
        Vector soapParams = new Vector();
        
        // name, type, value, encoding style
        Parameter isbnParam = new Parameter("userName", String.class, user, null);
        soapParams.addElement(isbnParam);
        soapCall.setParams(soapParams);
        try {
            // Invoke the remote method on the object
            Response soapResponse = soapCall.invoke(url, "");
            
            // Check to see if there is an error, return "N/A"
            if (soapResponse.generatedFault()) {
                Fault fault = soapResponse.getFault();
                String f = fault.getFaultString();
                return f;
            } else {
                // read result
                Parameter soapResult = soapResponse.getReturnValue();
                
                // get a string from the result
                return soapResult.getValue().toString();
            }
        } catch (SOAPException se) {
            return se.getMessage();
        }
    }
}

4、用http调用

xxx

5、异常

5.1、webService 报aultString: org.xml.sax.SAXException: Bad envelope tag: definitions
参考:https://blog.csdn.net/wangjinwei6912/article/details/8512598

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用 axios 发送 POST 请求来调用 Java WebService SOAP 接口。首先需要了解 SOAP 协议及其请求格式,然后可以按照以下步骤进行调用。 1. 安装 axios 可以使用 npm 安装 axios: ``` npm install axios --save ``` 2. 构造 SOAP 请求 构造 SOAP 请求的格式如下: ```xml <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ser="http://service.example.com"> <soapenv:Header/> <soapenv:Body> <ser:methodName> <!--Optional:--> <arg0>?</arg0> <!--Optional:--> <arg1>?</arg1> </ser:methodName> </soapenv:Body> </soapenv:Envelope> ``` 其中,`ser:methodName` 为要调用Java WebService 方法名,`arg0`、`arg1` 等为方法参数,可以根据实际情况进行修改。 3. 发送 SOAP 请求 使用 axios 发送 SOAP 请求的代码如下: ```javascript import axios from 'axios' const url = 'http://example.com/soap/service' // WebService 地址 const action = 'http://service.example.com/methodName' // SOAPAction const xml = ` <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ser="http://service.example.com"> <soapenv:Header/> <soapenv:Body> <ser:methodName> <!--Optional:--> <arg0>?</arg0> <!--Optional:--> <arg1>?</arg1> </ser:methodName> </soapenv:Body> </soapenv:Envelope> ` // SOAP 请求体 axios.post(url, xml, { headers: { 'Content-Type': 'text/xml;charset=UTF-8', 'SOAPAction': action } }).then(response => { console.log(response.data) }).catch(error => { console.log(error) }) ``` 其中,`url` 为 WebService 地址,`action` 为 SOAPAction,需要根据实际情况进行修改。`xml` 为 SOAP 请求体,需要根据实际情况进行修改。在发送请求时,需要指定 `Content-Type` 为 `text/xml;charset=UTF-8`,并在请求头中加入 `SOAPAction`。 4. 解析响应 Java WebService 返回的响应也是 SOAP 格式的,需要进行解析。可以使用第三方库 `xml2js` 将响应转换为 JavaScript 对象,代码如下: ```javascript import axios from 'axios' import { parseString } from 'xml2js' const url = 'http://example.com/soap/service' // WebService 地址 const action = 'http://service.example.com/methodName' // SOAPAction const xml = ` <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ser="http://service.example.com"> <soapenv:Header/> <soapenv:Body> <ser:methodName> <!--Optional:--> <arg0>?</arg0> <!--Optional:--> <arg1>?</arg1> </ser:methodName> </soapenv:Body> </soapenv:Envelope> ` // SOAP 请求体 axios.post(url, xml, { headers: { 'Content-Type': 'text/xml;charset=UTF-8', 'SOAPAction': action } }).then(response => { parseString(response.data, (error, result) => { if (error) { console.log(error) } else { console.log(result) } }) }).catch(error => { console.log(error) }) ``` 在响应中,可以通过 `result.Body` 获取到 Java WebService 返回的实际内容,需要根据实际情况进行解析。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值