使用node.js HTTP远程客户端请求不会返回任何正文

时间:2022-06-22 14:27:40

I'm using node.js to download a webpage. However, it's not receiving any chunks of data:

我正在使用node.js下载网页。但是,它没有收到任何数据块:

    console.log('preparing request to ' + url)
    u = require('url').parse(url)
    var remote_client = http.createClient(80, u['host']);
    var request = remote_client.request("GET", u['pathname'], {"host": u['host']});
    console.log("request made")

    request.addListener('response', function (response) {
        response.setEncoding('binary') 
        var body = '';

        response.addListener('data', function (chunk) {
            body += chunk;
            console.log('chunk received')
        });
    });

The last console message is "request made". There are no console messages with "chunk received" or the like. Thoughts?

最后一个控制台消息是“请求发出”。没有带有“块接收”等的控制台消息。思考?

2 个解决方案

#1


12  

This is an example which always worked for me:

这是一个总能为我工作的例子:

var sys = require('sys'),
    http = require('http');

var connection = http.createClient(8080, 'localhost'),
    request = connection.request('/');

connection.addListener('error', function(connectionException){
    sys.log(connectionException);
});

request.addListener('response', function(response){
    var data = '';

    response.addListener('data', function(chunk){ 
        data += chunk; 
    });
    response.addListener('end', function(){
        // Do something with data.
    });
});

request.end();

#2


1  

You need to call end() on the request to signal that you are ready to send it. Also you should add a user-agent header to your request. Many web servers look for it.

您需要在请求上调用end()来表示您已准备好发送它。您还应该在请求中添加用户代理标头。许多Web服务器都在寻找它。

console.log('preparing request to ' + url)
u = require('url').parse(url)
var remote_client = http.createClient(80, u['host']);
var request = remote_client.request("GET", u['pathname'], {"host": u['host'],
                                                           "user-agent": "node.js"});
console.log("request made")

request.addListener('response', function (response) {
    response.setEncoding('binary') 
    var body = '';

    response.addListener('data', function (chunk) {
        body += chunk;
        console.log('chunk received')
    });
});

request.end();

#1


12  

This is an example which always worked for me:

这是一个总能为我工作的例子:

var sys = require('sys'),
    http = require('http');

var connection = http.createClient(8080, 'localhost'),
    request = connection.request('/');

connection.addListener('error', function(connectionException){
    sys.log(connectionException);
});

request.addListener('response', function(response){
    var data = '';

    response.addListener('data', function(chunk){ 
        data += chunk; 
    });
    response.addListener('end', function(){
        // Do something with data.
    });
});

request.end();

#2


1  

You need to call end() on the request to signal that you are ready to send it. Also you should add a user-agent header to your request. Many web servers look for it.

您需要在请求上调用end()来表示您已准备好发送它。您还应该在请求中添加用户代理标头。许多Web服务器都在寻找它。

console.log('preparing request to ' + url)
u = require('url').parse(url)
var remote_client = http.createClient(80, u['host']);
var request = remote_client.request("GET", u['pathname'], {"host": u['host'],
                                                           "user-agent": "node.js"});
console.log("request made")

request.addListener('response', function (response) {
    response.setEncoding('binary') 
    var body = '';

    response.addListener('data', function (chunk) {
        body += chunk;
        console.log('chunk received')
    });
});

request.end();