在Nodejs中读取原始http消息

use*_*246 6 javascript sockets http httpresponse node.js

我正在使用http.request函数发送一个http请求,我想读取整个http响应,如文本; 也就是原始的http协议文本.可能吗?我写了下面的代码,但它没有用.

// Set up the request
console.log('Sending request');
var post_req = http.request(post_options, function(res) {
    res.setEncoding('utf8');
    console.log('Response statusCode: ' + res.statusCode);
//    res.on('data', function (chunk) {
//        console.log('Response: ' + chunk);
//    });
//    res.on('end', function() {});
});

post_req.on('socket', function (socket) {
    var response = "";
    socket.on('data', function(chunk){
    console.log(chunk);
    });
});

// post the data
post_req.write(post_data);
post_req.end();
Run Code Online (Sandbox Code Playgroud)

小智 0

假设您的环境允许使用此类工具,您可以运行 HTTP 调试代理,例如 Fiddler http://www.fiddler2.com/,它使您能够检查 HTTP 调用和响应。