当我发送一个包含大量数据文件的表单时,socket会被远程服务器突然关闭。这可能就是为什么我得到了一个错误的ECONNRESET。如何解决?
我的nodejs代码:
Promise((resolve, reject) => {
let sendOption = {
method: 'post',
host: host,
port: port,
path: path,
headers: form.getHeaders(),
timeout: options.maxTimeout ? 1 * 60 * 60 * 1000 : 2 * 60 * 1000,
}
if (options.userName && options.passWord) {
let auth = new Buffer(options.userName + ':' + options.passWord).toString('base64');
sendOption.Authorization = 'Basic ' + auth;
}
let request = http.request(sendOption, (res) => {
let body = ''
res.on('data', function (chunk) {
body += chunk;
});
res.on('end', () => {
resolve(JSON.parse(body))
})
});
request.on('error', (err) => reject(err));
request.write(form.getBuffer());
request.end();
})完全错误:
2020-02-17 19:09:25,570 ERROR 18664 [-/::1/-/14867ms POST /thirdPartUpload] nodejs.ECONNRESETError: socket hang up
at connResetException (internal/errors.js:570:14)
at Socket.socketOnEnd (_http_client.js:440:23)
at Socket.emit (events.js:215:7)
at endReadableNT (_stream_readable.js:1184:12)
at processTicksAndRejections (internal/process/task_queues.js:80:21)
code: "ECONNRESET"
name: "ECONNRESETError"
pid: 18664
hostname: PC-HZ20139584
enter code here发布于 2020-02-17 20:13:45
POST请求需要Content-Length:标头声明其主体的长度。在本例中,您使用request.write(form.getBuffer());发送帖子的正文
在服务器看来,缺少或错误的Content-Length:标头可能会显示为试图利用漏洞进行攻击。因此,服务器对这些请求关上了门(突然关闭了连接),这在您的客户端显示为ECONNRESET。
试试这样的东西。
const buff = form.getBuffer();
request.setHeader('Content-Length', buff.length);
request.write(buff);https://stackoverflow.com/questions/60261639
复制相似问题