node.js 请求期间出现“套接字挂断”错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18867185/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
"Socket hang up" error during request
提问by Dmitriy
I try to make a GET request to some site (not mine own site) via http module of node.js version 0.8.14. Here is my code (CoffeeScript):
我尝试通过 node.js 版本 0.8.14 的 http 模块向某个站点(不是我自己的站点)发出 GET 请求。这是我的代码(CoffeeScript):
options =
host: 'www.ya.ru'
method: 'GET'
req = http.request options, (res) ->
output = ''
console.log 'STATUS: ' + res.statusCode
res.on 'data', (chunk) ->
console.log 'A new chunk: ', chunk
output += chunk
res.on 'end', () ->
console.log output
console.log 'End GET Request'
req.on 'error', (err) ->
console.log 'Error: ', err
req.end()
I get the following error during this operation: { [Error: socket hang up] code: 'ECONNRESET' }. If I comment the error handler my application is finished with the following error:
我在此操作期间收到以下错误:{ [错误:套接字挂断] 代码:'ECONNRESET' }。如果我评论错误处理程序,我的应用程序将完成以下错误:
events.js:48
throw arguments[1]; // Unhandled 'error' event
^
Error: socket hang up
at createHangUpError (http.js:1091:15)
at Socket.onend (http.js:1154:27)
at TCP.onread (net.js:363:26)
I try to find out solution on the internet but still hasn't found them. How to solve this issue?
我试图在互联网上找到解决方案,但仍然没有找到。如何解决这个问题?
采纳答案by Dmitriy
I've finally detected the problem and found out the solution. The problem was that I use a proxy server to connect to the internet. Here is the working code:
我终于发现了问题并找到了解决方案。问题是我使用代理服务器连接到互联网。这是工作代码:
options =
hostname: 'myproxy.ru'
path: 'http://www.ya.ru'
port: 3128
headers: {
Host: "www.ya.ru"
}
req = http.request options, (res) ->
output = ''
console.log 'STATUS: ' + res.statusCode
res.on 'data', (chunk) ->
console.log 'A new chunk: ', chunk
output += chunk
res.on 'end', () ->
console.log output
console.log 'End GET Request'
req.on 'error', (err) ->
console.log 'Error: ', err
req.end()
Thank you all for helps and suggestions!
谢谢大家的帮助和建议!
回答by user568109
You have to end the request. Add this at the end of your script:
您必须结束请求。在脚本的末尾添加:
req.end()
回答by Jonathan Lonowski
When using http.request(), you have to at some point call request.end().
使用时http.request(),您必须在某些时候调用request.end()。
req = http.request options, (res) ->
# ...
req.on 'error', # ...
req.end() # <---
Until then, the requestis left open to allow for writing a body. And, the error is because the server will eventually consider the connection to have timed out and will close it.
在此之前,request保持打开状态以允许写入 body。而且,错误是因为服务器最终会认为连接已超时并将关闭它。
Alternatively, you can also use http.get()with GETrequests, which will call .end()automatically since GETrequests aren't normally expected to have a body.
或者,您也可以使用http.get()with GETrequests,它会.end()自动调用,因为GET请求通常不会有 body。
回答by Aviram Netanel
in my case it was the 'Content-Length'header - I took it out and it's fine now...
在我的情况下,它是“内容长度”标题 - 我把它拿出来,现在很好......
code:
代码:
function sendRequest(data)
{
var options = {
hostname: host,
path: reqPath,
port: port,
method: method,
headers: {
'Content-Length': '100'
}
var req = http.request(options, callback);
req.end();
};
after removing the line: 'Content-Length': '100'it sorted out.
删除行后:'Content-Length': '100'它整理出来。
回答by Rajat Talwar
I found this to occur in one more case where I was sending empty body like - '{}' in a delete operation called from internframework for testing; instead I used null to send as value of body parameter while making the request through
我发现在另一种情况下会发生这种情况,我在从实习框架调用的删除操作中发送空正文,例如 - '{}' 以进行测试;相反,我在发出请求时使用 null 作为 body 参数的值发送
回答by Sushil
When upgrading from 0.10.33 to 0.12 of nodejs, this error was hit.
nodejs从0.10.33升级到0.12时,遇到这个错误。
In my case, there was a body (json) for the delete request. Earlier, node client was setting - 'transfer-encoding' as chunked - by default when 'content-length' is not set. It seems in recent version - node client stopped setting transfer-encoding by default.
就我而言,删除请求有一个正文(json)。早些时候,节点客户端设置 - 'transfer-encoding' 作为分块 - 默认情况下,当 'content-length' 未设置时。似乎在最近的版本中 - 默认情况下,节点客户端停止设置传输编码。
Fix was to set it in the request.
修复是在请求中设置它。
回答by LeOn - Han Li
In my case, after upgrading to node 8.0.0, the post does not work anymore. adding Content-Lengthto header does not help. Have to add 'Connection': 'keep-alive'to the headerinstead to get this error away.
就我而言,升级到 后node 8.0.0,该帖子不再起作用。添加Content-Length到标题没有帮助。必须添加'Connection': 'keep-alive'到标题中才能消除此错误。
let postOptions = {
method: 'POST',
form: form,
url: finalUrl,
followRedirect: false,
headers:{
'Connection': 'keep-alive'
}
};
request(postOptions, handleSAMLResponse);

