js调用wsdl


WSDL(webservice描述语言)是一种使用 XML 编写的文档,这种文档可描述某个 Web Service。webservice需要使用soap协议(一种基于xml描述的协议)进行通信。

可以通过wsdl查看接口方法,详细以文档示例的报文为准。http://ws.webxml.com.cn/WebServices/WeatherWS.asmx?WSDL为例 查看 wsdl:portType 中的 operation

<wsdl:operation name="getWeather">
<wsdl:documentation xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"><br /><h3>获得天气预报数据</h3><p>输入参数:城市/地区ID或名称,返回数据:一维字符串数组。</p><br /></wsdl:documentation>
<wsdl:input message="tns:getWeatherSoapIn"/>
<wsdl:output message="tns:getWeatherSoapOut"/>
</wsdl:operation>

有以下使用方式

1. fetch调用

fetch("http://www.0755tt.com/services/ResultService?wsdl", {
  headers: {
    accept:
      "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
    "accept-language": "en,zh;q=0.9,zh-CN;q=0.8,zh-TW;q=0.7,ja;q=0.6",
    "cache-control": "no-cache",
    pragma: "no-cache",
    "upgrade-insecure-requests": "1",
  },
  referrerPolicy: "strict-origin-when-cross-origin",
  body:
    '<?xml version="1.0" encoding="utf-8"?>' +
    '<soap:Envelope xmlns:xsi="http://www.w3.org/2003/XMLSchema-instance" ' +
    'xmlns:web="http://webservice.sztce.apusic.com/"  ' +
    'xmlns:xsd="http://www.w3.org/2003/XMLSchema" ' +
    'xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">' +
    "<soap:Body>" +
    "<web:query>" + //方法
    "<idCardNo>" +  //参数
    "440301197804200945" +
    "</idCardNo>" +
    "</web:query>" +
    "</soap:Body>" +
    "</soap:Envelope>",
  method: "POST",
  mode: "cors",
  credentials: "include",
});

2. axios调用

import axios from "axios";

var xml =
  '<?xml version="1.0" encoding="utf-8"?>' +
  '<soap:Envelope xmlns:xsi="http://www.w3.org/2003/XMLSchema-instance" ' +
  'xmlns:web="http://webservice.sztce.apusic.com/"  ' +
  'xmlns:xsd="http://www.w3.org/2003/XMLSchema" ' +
  'xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">' +
  "<soap:Body>" +
  "<web:query>" +
  "<idCardNo>" +
  "440301197804200945" +
  "</idCardNo>" +
  "</web:query>" +
  "</soap:Body>" +
  "</soap:Envelope>";

var config = {
  headers: {
    "Content-Type": "text/xml;charset=utf-8", //不输入utf-8可能不能正常访问
  },
};

axios
  .post("http://www.0755tt.com/services/ResultService?wsdl", xml, config)
  .then((res) => {
    //有效数据
    let data = {};

    //正则
    let reg = /<return>(.*?)<\/return>/g;

    //原始的xml内容
    let xmlStr = res.data;

    //匹配出json字符串部分
    xmlStr.replace(reg, (match, vmKey) => {
      //有效数据
      data = JSON.parse(vmKey);
    });

    console.log(data);
  })
  .catch((err) => {
    console.log(err);
  });

3. 其它的soap版本

 let theCityCode = '';
 let theUserID = '';
 var xml = `
 <soap12:Envelope xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" 
 xmlns:xsd = "http://www.w3.org/2001/XMLSchema" 
 xmlns:soap12 = "http://www.w3.org/2003/05/soap-envelope">
         <soap12:Body>
             <getWeather xmlns="http://WebXml.com.cn/">
                 <theCityCode>${theCityCode}</theCityCode>
                 <theUserID>${theUserID}</theUserID>
             </getWeather>
         </soap12:Body>
</soap12:Envelope>`

var config = {
     headers: {
         "Content-Type": "text/xml;charset=utf-8", //不输入utf-8可能不能正常访问
     },
 };

 axios.post('http://ws.webxml.com.cn/WebServices/WeatherWS.asmx?WSDL',
     xml,
     config
 ).then(res => {
     console.log(res);
 }).catch(err => { console.log(err) });

https://blog.csdn.net/weixin_42058472/article/details/109676281 原生访问

https://www.5axxw.com/questions/content/m12mm1 axios访问

https://juejin.cn/post/6844903537629986824 wsdl

https://juejin.cn/post/6844903577127747598 webservice

https://blog.csdn.net/q913777031/article/details/110185443 WebServices 与 Web API 的区别


文章作者: iamfugui
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 iamfugui !
评论
  目录