18

我正在尝试使用 .js 通过 Node.js 向 Web 服务提交 xml 请求http.request

这是我的代码。我的问题是,data=1我不想将 xml 发布到服务中。

http.request({
   host: 'service.x.yyy.x',
   port: 80,
   path: "/a.asmx?data=1",
   method: 'POST'
}, function(resp) {
   console.log(resp.statusCode);
   if(resp.statusCode) {
        resp.on('data', function (chunk) {
            console.log(chunk);
            str +=  chunk;                  
        });
        resp.on('end', function (chunk) {                           
            console.log(str);            
        });                   
  }
}).end();

何做这个?

4

3 回答 3

26

实际上,Andrey Sidorov提供的链接有助于使其正常工作。这行得通。

var body = '<?xml version="1.0" encoding="utf-8"?>' +
           '<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>......</soap12:Body></soap12:Envelope>';

var postRequest = {
    host: "service.x.yyy.xa.asmx",
    path: "/a.asmx",
    port: 80,
    method: "POST",
    headers: {
        'Cookie': "cookie",
        'Content-Type': 'text/xml',
        'Content-Length': Buffer.byteLength(body)
    }
};

var buffer = "";

var req = http.request( postRequest, function( res )    {

   console.log( res.statusCode );
   var buffer = "";
   res.on( "data", function( data ) { buffer = buffer + data; } );
   res.on( "end", function( data ) { console.log( buffer ); } );

});

req.on('error', function(e) {
    console.log('problem with request: ' + e.message);
});

req.write( body );
req.end();
于 2012-12-24T12:52:03.250 回答
9

http.request返回ClientRequest对象,它也是一个可写流。而不是.end()end(xmlbody).write(xmlbody).end()

于 2012-12-24T08:49:56.800 回答
0
var request = require("request");
request.post({
    rejectUnauthorized: false,
    url: 'URL',
    method: "POST",
    headers: {
        'Content-Type': 'application/xml',
    },
    body: '<XML>'
}, function (error, response, body) {
    if (error) {
        // Handle error
    } else {
        // Handle Response and body
    }
});
于 2021-07-16T03:40:13.390 回答