node.js 发送HTTP消息时报错,非常诡异,球破!
发布于 11 年前 作者 imaxlove 8077 次浏览 最后一次编辑是 8 年前

正常往sina发送HTTP请求,但是会不时报如下错误, POST: api.weibo.com:443 /2/statuses/repost.json access_token=2.00rhElVDGI631Ee549f6f303v86EQB&id=3534310755900989&status=imax_check

Error: socket hang up at SecurePair.error (tls.js:948:15) at EncryptedStream.CryptoStream._done (tls.js:432:17) at EncryptedStream.CryptoStream._pull (tls.js:577:12) at SecurePair.cycle (tls.js:866:20) at EncryptedStream.CryptoStream.end (tls.js:405:13) at Socket.onend (stream.js:66:10) at Socket.EventEmitter.emit (events.js:123:20) at TCP.onread (net.js:417:51) 有人知道啥原因不?

10 回复

不贴代码的话我想你只应该到新浪 API 的论坛问…

data = ‘access_token=2.00rhElVDGI631Ee549f6f303v86EQB&id=3534310755900989&status=imax_check’ var options = { hostname: ‘api.weibo.com’, port: 443, path: ‘/2/statuses/repost.json’, method: ‘POST’ };

var req = http.request(options, function(res) { console.log('STATUS: ’ + res.statusCode); }); });

req.on(‘error’, function(e) { console.log('problem with request: ’ + e.message); });

// write data to request body req.write(‘data\n’); req.end();

除了一层括号有错外(我手动去掉了)… 我运行是成功的, 这不是你出错的代码对吧

var http = require('http');

var options = {
  hostname: 'api.weibo.com',
  port: 443,
  path: '/2/statuses/repost.json',
  method: 'POST'
};

var req = http.request(options, function(res) {
  console.log('STATUS: ' + res.statusCode);
});

req.on('error', function(e) {
  console.log('problem with request: ' + e.message);
});

// write data to request body
req.write('data\n');
req.end();

出错的代码和这个一模一样,我这边只有一个环境会不时的抱着个错,我看过源码包,貌似是ssl什么地方出错了,手动运行怎么都不能重现,但是就会时不时的发生这种错。。。十分无奈。

或许是新浪服务器端的不作为吧。

我的运行结果: $ node test.js STATUS: 411

443不该是用https模块来进行访问吗?

我的关于443的回复怎莫不见了?

Jackson 对 443 的建议是对的。可是我改用 https 得到相同的结果。

$ node test.js 
STATUS: 411

411错误码是你post时http头content-length不对,你代码里没有发这个header,基本上4xx错误码都是客户端请求错误引起的

回到顶部