soap調(diào)用webservice步驟

node soap模塊

一、瀏覽器查看需要調(diào)用的接口
    1、使用瀏覽器打開要調(diào)用的接口地址,后綴需要加上wsdl
    2、找到需要調(diào)用的接口方法及入?yún)⒏袷饺缦?
image.png
二、可以使用soapUI工具進(jìn)行調(diào)用測(cè)試服務(wù)是否正常(入?yún)⒏袷接煞?wù)商提供)
    1、soapUi加載wsdl地址
    2、找到需要調(diào)用的方法
    3、<![CDATA[]]>若節(jié)點(diǎn)數(shù)據(jù)中存在特殊字符可以使用
image.png

image.png

三、組織xml數(shù)據(jù)


image.png

四、使用soap模塊進(jìn)行調(diào)用
  1、若不存在soap模塊需要使用進(jìn)行安裝
          cnpm install soap
  2、引用模塊
          let soap = require('soap')
  3、組織xml數(shù)據(jù),注意此處的xml = soapUi中的<tem:xml>
          let data = {
            xml: <Request>
                <Common>
                    <DistrictCode></DistrictCode>
                    <ClientIp></ClientIp>
                </Common>
                <Recipes>
                    <Recipe>
                        <RecipeId></RecipeId>
                    </Recipe>
                </Recipes>
            </Request>
        }
   4、soap建立連接
  // forceSoap12Headers 為設(shè)置合適的請(qǐng)求頭
  soap.createClient(wsdl地址, {forceSoap12Headers: false}, function (err, client) {
     //調(diào)用要調(diào)用的接口方法名,傳入組織好的xml數(shù)據(jù)
     client['submitRecipe'](data, function (err, result) {
        //獲取到返回參數(shù)后使用xml2js進(jìn)行數(shù)據(jù)格式化
        //explicitArray 解決xml2js會(huì)默認(rèn)會(huì)把子子節(jié)點(diǎn)的值變?yōu)橐粋€(gè)數(shù)組
      xml2js.parseString(result, {explicitArray: false},(err,result) =>{
              //此處的result已經(jīng)是json格式
              logger.info(result);
        })
     })
 })

5、整體代碼

   let soap = require('soap')
   let xml2js = require('xml2js')
   let data = {
            xml:
            <Request>
                <Common>
                    <DistrictCode></DistrictCode>
                    <ClientIp></ClientIp>
                </Common>
                <Recipes>
                    <Recipe>
                        <RecipeId></RecipeId>
                    </Recipe>
                </Recipes>
            </Request>
        }
 return new Promise(function (resolve ,reject){
soap.createClient('wsdl', {forceSoap12Headers: false}, function (err, client) {
    if(err){
          reject(err);
    }else{
          client['submitRecipe'](data, function (err, result) {
               if(err){
                    reject(err)
               }else{
                    logger.info('--------xxxxxxxxx---------',result);
 xml2js.parseString(result.submitRecipeResult, {explicitArray: false},(err,result) =>{
           if(err){
                    logger.error(err);
           }else{
                    logger.info('***********XML*************',result);
           }
           });
                    resolve(result);    
           }
         });
        }
       });
     });
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容