Using node.js HTTP remote client request doesn't return any body

后端 未结 2 2025
无人及你
无人及你 2021-02-05 12:37

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

    console.log(\'preparing request to \' + url)
    u = require(\'url         


        
相关标签:
2条回答
  • 2021-02-05 13:30

    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();
    
    0 讨论(0)
  • 2021-02-05 13:30

    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.

    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();
    
    0 讨论(0)
提交回复
热议问题