Node.js soap库创建客户端遇AxiosError: socket hang up求助
Node.js TS环境下soap库调用SOAP服务报错AxiosError: socket hang up
问题代码
const soap = require('soap'); const url = `http://xxx.xxx.1.101:8888/bmss/wd/SystemService?wsdl` soap.createClient(url,(err:any, client:any) => { if (err) { console.error('Error creating SOAP client:', err); return; } });
问题现象
调用createClient时抛出AxiosError: socket hang up错误,错误代码为ECONNRESET。
- 直接通过GET请求访问以下两个接口在Postman中均正常:
http://xxx.xxx.1.101:8888/bmss/wd/SystemService?wsdlhttp://xxx.xxx.1.101:8888/bmss/wd/SystemService?wsd=1
- 使用
easy-request-soap库可以正常调用服务,但希望使用soap库的client.myFunction方式开发,设置超时也无法解决问题。
完整错误栈
error creating SOAP client: AxiosError: socket hang up at connResetException (node:internal/errors:787:14) at Socket.socketOnEnd (node:_http_client:519:23) at Socket.emit (node:events:526:35) at Socket.emit (node:domain:488:12) at endReadableNT (node:internal/streams/readable:1589:12) at processTicksAndRejections (node:internal/process/task_queues:82:21) { code: 'ECONNRESET', config: { timeout: 0, xsrfCookieName: 'XSRF-TOKEN', xsrfHeaderName: 'X-XSRF-TOKEN', maxContentLength: -1, maxBodyLength: -1, env: { FormData: [Function] }, validateStatus: null, headers: { Accept: 'text/html,application/xhtml+xml,application/xml,text/xml;q=0.9,*/*;q=0.8', 'User-Agent': 'node-soap/1.0.0', 'Accept-Encoding': 'none', 'Accept-Charset': 'utf-8', Connection: 'close', Host: 'xxx.xx.xx.xx:8888' }, url: 'http://xxx.xx.xx.xx:8888/bmss/ws/SystemService?xsd=1', method: 'get', data: null }, request: <ref *1> Writable { _writableState: WritableState { highWaterMark: 16384, length: 0, corked: 0, onwrite: [Function: bound onwrite], writelen: 0, bufferedIndex: 0, pendingcb: 0, [Symbol(kState)]: 34357356, [Symbol(kBufferedValue)]: null }, _events: [Object: null prototype] { response: [Function: handleResponse], error: [Function: handleRequestError], socket: [Function: handleRequestSocket] }, _eventsCount: 3, _maxListeners: undefined, _options: { maxRedirects: 21, maxBodyLength: 10485760, protocol: 'http:', path: '/bms/ws/SystemService?xsd=1', method: 'GET', headers: [Object], agent: undefined, agents: [Object], auth: undefined, hostname: '***', port: '*', nativeProtocols: [Object], pathname: '/bmss/ws/SystemService', search: '?xsd=1' }, _ended: true, _ending: true, _redirectCount: 0, _redirects: [], _requestBodyLength: 0, _requestBodyBuffers: [], _onNativeResponse: [Function (anonymous)], _currentRequest: ClientRequest { _events: [Object: null prototype], _eventsCount: 7, _maxListeners: undefined, outputData: [], outputSize: 0, _contentLength: 0, _hasBody: true, _trailer: '', finished: true, _headerSent: true, _closed: false, socket: [Socket], _header: 'GET /bmss/ws/SystemService?xsd=1 HTTP/1.1\r\n' + 'Accept: text/html,application/xhtml+xml,application/xml,text/xml;q=0.9,*/*;q=0.8\r\n' + 'User-Agent: node-soap/1.0.0\r\n' + 'Accept-Encoding: none\r\n' + 'Accept-Charset: utf-8\r\n' + 'Connection: close\r\n' + 'Host: ***\r\n' + '\r\n', _keepAliveTimeout: 0, _onPendingData: [Function: nop], agent: [Agent], socketPath: undefined, method: 'GET', maxHeaderSize: undefined, insecureHTTPParser: undefined, joinDuplicateHeaders: undefined, path: '/bmss/ws/SystemService?xsd=1', _ended: false, res: null, aborted: false, timeoutCb: [Function: emitRequestTimeout], upgradeOrConnect: false, parser: null, maxHeadersCount: null, reusedSocket: true, host: '****', protocol: 'http:', _redirectable: [Circular *1], [Symbol(kCapture)]: false, [Symbol(kBytesWritten)]: 0, [Symbol(kNeedDrain)]: false, [Symbol(corked)]: 0, }, _currentUrl: 'http://***/bmss/ws/SystemService?xsd=1', [Symbol(kCapture)]: false } }
排查与解决建议
检查路径拼写一致性
从错误栈可见,soap库实际请求的路径是/bmss/ws/SystemService?xsd=1,但你提供的可用接口路径是/bmss/wd/SystemService?wsdl,注意ws和wd的差异。可能是SOAP库解析WSDL时自动生成的XSD请求路径错误,可尝试手动指定WSDL内容而非URL:const axios = require('axios'); const soap = require('soap'); async function createClient() { const wsdlContent = await axios.get('http://xxx.xxx.1.101:8888/bmss/wd/SystemService?wsdl'); const client = await soap.createClientAsync(wsdlContent.data); // 后续调用client.myFunction }修改HTTP客户端配置
soap库底层使用axios,可自定义axios配置解决连接问题:const soap = require('soap'); const url = 'http://xxx.xxx.1.101:8888/bmss/wd/SystemService?wsdl'; const axiosConfig = { timeout: 30000, headers: { 'Connection': 'keep-alive', 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36' }, httpAgent: new require('http').Agent({ keepAlive: true }) }; soap.createClient(url, { axios: axiosConfig }, (err, client) => { if (err) { console.error('Error creating SOAP client:', err); return; } // 使用client调用服务 });降级或升级soap库版本
部分版本的soap库存在axios兼容性问题,可尝试切换版本,比如降级到0.45.0或升级到最新稳定版:npm uninstall soap npm install soap@0.45.0禁用XSD自动获取
如果服务端不需要额外获取XSD文件,可通过配置禁用soap库的XSD解析:soap.createClient(url, { disableCache: true, ignoreBaseNameSpace: true }, (err, client) => { // ... });
内容的提问来源于stack exchange,提问作者이창민
相关产品推荐
相关产品推荐

