我想在Node.js
中点击API来连接SOAP和HTTPS。当我点击用于执行HTTPS请求的功能的URL时,我使用以下代码与SOAP Server进行连接以执行数据传输。但它显示错误:
{[错误:套接字挂断]代码:'ECONNRESET'}:
exports.check=function(req,res) {
//Add SSL to communicate with SOAP
var sslRootCAs = require('ssl-root-cas/latest')
.inject()
.addFile( __dirname + '/Symantec Class 3 Secure Server CA - G4.cer' );
var body = '<?xml version="1.0" encoding="UTF-8"?>' +
'<!—Worked Example 1: Birth Request -->' +
'<RegBirths SchemaVersion="1.0" ProgramName="CTWSProg" ProgramVersion="1b" RequestTimeStamp="2001-12-17T09:30:47-05:00" >' +
'<Authentication>' +
'<CTS_OL_User Usr="111-111-111" Pwd="m0nster"/>' +
'</Authentication>' +
'<Births TxnId="123889">' +
'<Birth RowNum="1" Etg="UK520202500138" Dob="2004-03-01" Brd="HF" Sex="f" EId="111222333" GdEtg="UK560002400063"' +
'BLoc="20/002/0001" PLoc="01/001/0001" PSLoc="04" IWarn="n"/>' +
'<Birth RowNum="2" Etg="UK240001300777" Dob="2004-03-09" Brd="HF" Sex="f" EId="111222333" GdEtg="UK560002100018"' +
'BLoc="20/002/0001" PLoc="01/001/0001" PSLoc="04" IWarn="n" />' +
'</Births>' +
'</RegBirths>';
var body2 = '<?xml version="1.0" encoding="utf-8"?>'+
'<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">' +
'<soap:Body>' +
'<TransferData xmlns="http://www.defra.gov.uk">' +
'<username>' + username + '</username>' +
'<password>' + password + '</password>' +
'<serviceName>DEFRA-CTWS-FULL-PROVING</serviceName>' +
'<data>'+ body +'</data>' +
'<type>Register_Births_Asynchronous-V1-0</type>' +
'</TransferData>' +
'</soap:Body>' +
'</soap:Envelope>';
var postRequest = {
host : "hostman.com",
path: "/abcxyz.asmx?WSDL",
port: 443,
rejectUnauthorized: true,
method: "POST",
headers: {
'Accept': 'application/xml',
'Content-Type': 'application/xml; charset=UTF-8',
'SOAPAction': "https://www.axys.com/TransferData",
'Content-Length': Buffer.byteLength(body2)
}
};
var buffer = "";
var req = http.request( postRequest, function( res ) {
console.log( res );
var buffer = "";
res.on( "data", function( data ) { buffer = buffer + data; } );
res.on( "end", function( data ) { console.log( buffer ); } );
res.on('error', function (err) { console.log( "----ERROR----" ); console.log(err);});
});
// req.on('error', function(err) {
// console.log( "----ERROR----" ); console.log(err);
// });
// `req`.on('socket', function (socket) {
// console.log(socket)
// socket.setTimeout(3000);
// socket.on('timeout', function() {
// req.abort();
// });
// });
req.on('error', function(err) {
if (err.code === "ECONNRESET") {
console.log("Timeout occurs");
//specific error treatment
}
});
req.write( body );
req.end();
}