“套接字挂断"请求期间出错 [英] "Socket hang up" error during request

查看:50
本文介绍了“套接字挂断"请求期间出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试通过 node.js 版本 0.8.14 的 http 模块向某个站点(不是我自己的站点)发出 GET 请求.这是我的代码(CoffeeScript):

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):

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' }.如果我评论错误处理程序,我的应用程序将完成以下错误:

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:

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?

推荐答案

我终于发现了问题并找到了解决方案.问题是我使用代理服务器连接到互联网.这是工作代码:

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!

这篇关于“套接字挂断"请求期间出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆