node.js HTTP 请求跑着跑着就没有响应了,又遇到的没?
发布于 11 年前 作者 imaxlove 12176 次浏览 最后一次编辑是 8 年前

var https = require(‘https’);

var options = { host: ‘open.t.qq.com’, port: 443, path: ‘/api/t/re_add’, method: ‘POST’ };

var post_data = ‘oauth_consumer_key=801243043&access_token=273f8fbb38254ef6f5519a2b88aa1bba&openid=a87980a620fdd44b54aa84a029b18654&clientip=127.0.0.1&oauth_version=2.a&scope=all&format=json&reid=273009086234102&content=imax_checkfHQsnMQaq’

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

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

// write data to request body req.write(post_data); req.end();

代码如上所示, 定时并发调用这个去腾讯请求数据,一开始请求返回没有问题,但是过一段时间之后就,data事件就再也收不到消息了。有人遇到过没,也没有error事件。不知道是没法出去还是没响应。在linux下同一个环境curl发同样的请求却能够返回正确结果,重启下也能返回结果,但过一段时间又会出现同样的问题。 tencent开放平台的很多接口都有这个问题。 球大神破解。

6 回复

@Jackson 能不能帮忙分析下呢,实在是搞不明白。谢谢了。

说明一下跑的环境,怎么跑的

明显是开发平台的对于采集的一种防御?

var https = require('https');
................
var req = http.request(options, function(res) {

上面2句对不上,究竟用的http协议还是https协议。另外,程序生成http请求不符合http规范,header缺少Content-Type,最好也指定Content-Length。修改如下

var https = require('https');
var post_data = 'oauth_consumer_key=801243043&access_token=273f8fbb38254ef6f5519a2b88aa1bba&openid=a87980a620fdd44b54aa84a029b18654&clientip=127.0.0.1&oauth_version=2.a&scope=all&format=json&reid=273009086234102&content=imax_checkfHQsnMQaq'
var options = {
    host: 'open.t.qq.com',
    port: 443,
    path: '/api/t/re_add',
    method: 'POST',
    headers:{
        'Content-Type':'application/x-www-form-urlencoded',
        'Content-Length':post_data.length
    }
};

var req = https.request(options, function(res) {
    console.log('STATUS: ' + res.statusCode);
    res.on('data', function (chunk) {
        console.log('BODY: ' + chunk);
    });
});

req.on('error', function(e) {
    console.log('problem with request: ' + e.stack);
    res.end('error');
});
req.write(post_data);
req.end();

输出

STATUS: 200
BODY: {"data":{"id":"270003082809897","time":1363962771},"errcode":0,"msg":"ok","ret":0,"seqid":5858208050260679447}

我也遇到了这个问题,我是跟tomcat交互 大概发了3000多个请求之后就挂了

可以关注一下,tcp连接情况。 其它我提两个建议: 1、对req设置一个超时时间,避免长时间占用连接资源; 2、处理uncaughtException,避免有未捕获的异常导致nodejs进程over。

回到顶部