代码之家  ›  专栏  ›  技术社区  ›  Joliet

webservice拒绝对soap request node.js的访问

  •  0
  • Joliet  · 技术社区  · 6 年前

    我正在尝试从Web服务获取SOAP响应 没有 使用节点中的任何库,但我对我拥有的代码一无所获。可以找到服务的WSDL here . 我已经在soapui中测试了这个请求,并在批处理文件中使用curl。javascript:

    const http = require('http')
    const fs = require('fs')
    
    const xml = fs.readFileSync('latlonzipcode.xml','utf-8')
    
    const options = {
        hostname : 'graphical.weather.gov',
        path : '/xml/SOAP_server/ndfdXMLserver.php',
        method : 'POST',
        headers : {
        'Content-Type' : 'text/xml;charset=utf-8',
        'soapAction' : 'https://graphical.weather.gov/xml/DWMLgen/wsdl/ndfdXML.wsdl#LatLonListZipCode'
        }
    }
    
    var obj = http.request(options,(resp)=>{
        let data = ''
        console.log(resp.statusCode)
        console.log(resp.headers)
        resp.on('data',(chunk)=>{
            data += chunk
        })
        resp.on('end',()=>{
            console.log(data)
        })
    }).on('error',(err)=>{
        console.log("Error: " + err.message)
    })
    
    obj.write(xml)
    obj.end()
    

    SOAP信封/xml文件:

    <soapenv:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ndf="https://graphical.weather.gov/xml/DWMLgen/wsdl/ndfdXML.wsdl">
       <soapenv:Header/>
       <soapenv:Body>
           <ndf:LatLonListZipCode soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
             <zipCodeList xsi:type="xsd:string">90210</zipCodeList>
           </ndf:LatLonListZipCode>
       </soapenv:Body>
    </soapenv:Envelope>
    

    和.bat文件-用于测试:

    :: curl options: -H is for Header --data allows file parameters
    curl -H "soapAction: \"https://graphical.weather.gov/xml/DWMLgen/wsdl/ndfdXML.wsdl#LatLonListCityNames\"" -H "Content-Type: text/xml;charset=UTF-8" --data @latlonzipcode.xml https://graphical.weather.gov/xml/SOAP_server/ndfdXMLserver.php > response.xml
    

    我从服务收到的响应:

    403
    { server: 'AkamaiGHost',
      'mime-version': '1.0',
      'content-type': 'text/html',
      'content-length': '320',
      expires: 'Wed, 18 Jul 2018 14:18:05 GMT',
      date: 'Wed, 18 Jul 2018 14:18:05 GMT',
      connection: 'close' }
    <HTML><HEAD>
    <TITLE>Access Denied</TITLE>
    </HEAD><BODY>
    <H1>Access Denied</H1>
    
    You don't have permission to access "http&#58;&#47;&#47;graphical&#46;weather&#46;gov&#47;xml&#47;SOAP&#95;server&#47;ndfdXMLserver&#46;php" on this server.<P>
    Reference&#32;&#35;18&#46;7fd23017&#46;1531923485&#46;f45e7526
    </BODY>
    </HTML>
    

    批处理文件工作正常。任何帮助都会很好。谢谢。

    更新

    在谷歌搜索后,我发现 this . 所以我加了标题 User-Agent : headerTest 我的选择…最后得到了回应,不幸的是 WSDL。

    1 回复  |  直到 6 年前
        1
  •  0
  •   Joliet    6 年前

    如我上面的更新所述,我最初的问题是 使用 https Web服务正在阻止没有 User-Agent 标题。至于作为响应返回的WSDL文件,我错误地使用了 GET 在我的选项中,忘记更正它。最后,我怀疑在将XML写入主体时发生了什么事情,发现了一个问题-我找到了答案 here . 以下是正确的代码:

    const https = require('https')
    const fs = require('fs')
    
    const xml = fs.readFileSync('latlonzipcode.xml','utf-8')
    
    const options = {
        hostname : 'graphical.weather.gov',
        port : 443,
        path : '/xml/SOAP_server/ndfdXMLserver.php',
        method : 'POST',
        headers : {
           'User-Agent' : 'sampleTest',
           'Content-Type' : 'text/xml;charset=utf-8',
           'soapAction' : 'https://graphical.weather.gov/xml/DWMLgen/wsdl/ndfdXML.wsdl#LatLonListZipCode'
        }
    }
    
    var obj = https.request(options,(resp)=>{
        let data = ''
        fs.writeFile('server.log',resp.statusCode+"\n"+JSON.stringify(resp.headers),(err)=>{
            err ? console.log(err) : console.log('log file written')
        })
        resp.on('data',(chunk)=>{
            data += chunk
        })
        resp.on('end',()=>{
            fs.writeFile('soap-response.xml',data,(err)=>{
                err ? console.log(err) : console.log('data file written')
            })
            console.log(data)
        })
    }).on('error',(err)=>{
        console.log("Error: " + err.message)
    })
    
    /**
     * '.write()' is not being used:
     * obj.write(xml) ? console.log('op success') : console.log('error!')
     * obj.end()
     */
    
    obj.end(xml) ? console.log('op success') : console.log('error!')
    

    node.js终于让我对javascript感到兴奋。谢谢。