我尝试通过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()
在此操作过程中,我收到以下错误:{[错误:套接字挂起]代码:“ 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)
我尝试在互联网上找到解决方案,但仍然没有找到它们。如何解决这个问题?
我终于找到了问题,并找到了解决方案。问题是我使用代理服务器连接到Internet。这是工作代码:
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()
谢谢大家的帮助和建议!
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句