最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Node.js socket hang up error when connecting to twitter api - Stack Overflow

programmeradmin4浏览0评论

I'm getting socket hang up error , when connecting to twitter streaming api with basic authentication using https. I think the error is occurring when doing the authentication.

var https = require("https");
var options = {
        host: 'stream.twitter',
        path: '/1.1/statuses/filter.json?track=bieber',
        method: 'GET',
        headers: {
                "Authorization": "Basic " + new Buffer('UserName' + ":" + 'Password').toString("base64")
        }
}; //end of options


var request = https.request(options, function(response){
            var body = '';
             console.log('STATUS: ' + response.statusCode);
             console.log('HEADERS: ' + JSON.stringify(response.headers));

            response.on("data", function(chunk){
                    var tweet = JSON.parse(chunk);
                    console.log("Tweet " + tweet.text);
            });  // end of data

            response.on("end", function(){
                console.log("Disconnected!!");
            }); // end of end

            response.on("error", function(){
                console.log("error occured");
            }); // end of error
}); // end of request

request.on("error",function(error){
        console.log("Error: " + error.message);
                }); // end of error

When i try to access this url it works fine.

https://username:[email protected]/1.1/statuses/filter.json?track=twitter

I'm getting socket hang up error , when connecting to twitter streaming api with basic authentication using https. I think the error is occurring when doing the authentication.

var https = require("https");
var options = {
        host: 'stream.twitter.',
        path: '/1.1/statuses/filter.json?track=bieber',
        method: 'GET',
        headers: {
                "Authorization": "Basic " + new Buffer('UserName' + ":" + 'Password').toString("base64")
        }
}; //end of options


var request = https.request(options, function(response){
            var body = '';
             console.log('STATUS: ' + response.statusCode);
             console.log('HEADERS: ' + JSON.stringify(response.headers));

            response.on("data", function(chunk){
                    var tweet = JSON.parse(chunk);
                    console.log("Tweet " + tweet.text);
            });  // end of data

            response.on("end", function(){
                console.log("Disconnected!!");
            }); // end of end

            response.on("error", function(){
                console.log("error occured");
            }); // end of error
}); // end of request

request.on("error",function(error){
        console.log("Error: " + error.message);
                }); // end of error

When i try to access this url it works fine.

https://username:[email protected]/1.1/statuses/filter.json?track=twitter
Share Improve this question edited Feb 24, 2013 at 9:44 simplyblue asked Feb 23, 2013 at 7:02 simplybluesimplyblue 2,3479 gold badges42 silver badges67 bronze badges 1
  • 1 This error has been included in NodeJS 0.8.20 (as you can see in raw.github./joyent/node/v0.8.20/ChangeLog). It occurs when socket connection ends. I have experimented this error with code that works perfectly with previous Node versions. – sgmonda Commented Feb 24, 2013 at 11:37
Add a ment  | 

2 Answers 2

Reset to default 5

Change https.request(options, function(response){ } to https.get(options, function(response){ } Thanks to Darrenlooby on nodejs IRC for pointing it out.

You can use https.request, but make sure you add:

request.end();

where request is the object returned from https.request

See http://nodejs/api/https.html#https_https_request_options_callback for an example.

I had the same issue and this fixes it :)

发布评论

评论列表(0)

  1. 暂无评论