错误:第一个标记之前的非空格,但在调用SOAP服务时没有十六进制的字节顺序标记

时间:2017-01-06 15:10:16

标签: node.js express soap node-soap

我正在调用使用Node-Soap创建的soap服务,这里是WSDL。

     <?xml version="1.0" encoding="UTF-8"?>
        <definitions xmlns="http://schemas.xmlsoap.org/wsdl/"
                     xmlns:xs="http://www.w3.org/2001/XMLSchema"
                     xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
                     xmlns:tns="http://www.oorsprong.org/websamples.countryinfo"
                     name="CourseInfoService"
                     targetNamespace="http://www.example.org/websamples.CourseInfoService">
            <types>
                <xs:complexType name="ArrayOftCourses">
                    <xs:sequence>
                        <xs:element name="tCourse" type="tns:tCourse" minOccurs="0" maxOccurs="unbounded" nillable="true"/>
                    </xs:sequence>
                </xs:complexType>


                <xs:element name="ListCoursesRequest">
                    <xs:complexType>
                        <xs:sequence/>
                    </xs:complexType>
                </xs:element>
                <xs:element name="ListCoursesResponse">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element name="ListCoursesResult" type="tns:ArrayOftCourses"/>
                        </xs:sequence>
                    </xs:complexType>
                </xs:element>
    </types>
 <message name="ListCoursesRequest">
        <part name="parameters" element="tns:ListCoursesRequest"/>
    </message>
    <message name="ListCoursesResponse">
        <part name="parameters" element="tns:ListCoursesResponse"/>
    </message>
<portType name="CourseInfoSoapType">
        <operation name="ListCourses">
            <documentation>Returns a list of continents ordered by name.</documentation>
            <input message="tns:ListCoursesRequest"/>
            <output message="tns:SearchCoursesResponse"/>
        </operation>
</portType>
<binding name="CountryInfoServiceSoapBinding" type="tns:CourseInfoSoapType">
        <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>

        <operation name="ListCourses">
            <soap:operation soapAction="" style="document"/>
            <input>
                <soap:body use="literal"/>
            </input>
            <output>
                <soap:body use="literal"/>
            </output>
        </operation>
</binding>
<service name="CourseInfoService">
        <documentation>Stub doc</documentation>
        <port name="CountryInfoServiceSoap" binding="tns:CountryInfoServiceSoapBinding">
            <soap:address location="http://webservices.oorsprong.org/websamples.countryinfo/CountryInfoService.wso"/>
        </port>
    </service>
</definitions>

我在十六进制编辑器中检查了Byte-Order-Mark,并在字符串本身上使用了.replace('\ufeff', '')。 用于创建soap“route”的代码如下所示:

module.exports = function(app){
    const   soap = require('soap'),
            xml = require('fs').readFileSync('./routes/soapService.wsdl', 'utf8').replace('\ufeff', '');

    const   service = {
        CountryInfoServiceSoapBinding: {
            CourseInfoSoapType: {
                getCourses: function(args) {
                    return {
                        courses: [{
                            name: 'Hello',
                            credits: 123,
                            duration: 24
                        }, {
                            name: 'World',
                            credits: 456,
                            duration: 36
                        },
                    ]};
                },
                searchCourses: function(args) {
                    return {
                        courses: [{
                            name: 'Hello',
                            credits: 123,
                            duration: 24
                        }, {
                            name: 'World',
                            credits: 456,
                            duration: 36
                        }]

                    };
                },
                addCourse: function(args) {
                    return {
                        courses: 'Success!'
                    };
                },
            }
        }
    };

    soap.listen(app, '/wsdl', service, xml);
    console.log('Soap api on: /wsdl');
};

课程数据的请求如下:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:pm="http://www.getpostman.com/">
<soapenv:Header></soapenv:Header>
<soapenv:Body>
    <pm:ListCourses>
    </pm:ListCourses>
</soapenv:Body>
</soapenv:Envelope>

收到的回复是:

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"  xmlns:tns="http://www.oorsprong.org/websamples.countryinfo">
    <soap:Header>
        <o:Security soap:mustUnderstand="1" xmlns:o="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:u="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
            <u:Timestamp u:Id="_0">
                <u:Created>2017-01-06T15:07:48Z</u:Created>
                <u:Expires>2017-01-06T15:17:48Z</u:Expires>
            </u:Timestamp>
        </o:Security>
    </soap:Header>
    <soap:Body>
        <soap:Fault>
            <faultcode>500</faultcode>
            <faultstring>Invalid XML</faultstring>
            <detail>Error: Non-whitespace before first tag.
Line: 0
Column: 1
Char: [</detail>
        </soap:Fault>
    </soap:Body>
</soap:Envelope>

如果有人能指出我的方向很好,那就很棒。

1 个答案:

答案 0 :(得分:0)

您应该启动http服务器,然后将soap.listen连接到它。

 server = http.createServer(app).listen(8888, function(){
        var xml = require('fs').readFileSync('./apis/hello.wsdl', 'utf8');
         soap.listen(server, '/service', soapTest, xml);        
    });

这里的app是我的快递实例。