Nodejs:应用标头并获得响应

时间:2011-08-16 10:12:53

标签: http node.js http-headers response

我有以下GET请求:

GET http://www.google.ie/ HTTP/1.1
Host: www.google.ie
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:5.0) Gecko/20100101 Firefox/5.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip, deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Proxy-Connection: keep-alive
Cookie: PREF=ID=0000043ea43e2426:U=204008a193b06a93:FF=0:TM=1310983818:LM=1310983985:S=HhQ3hzHoRpfrsFN4; NID=50=bT7R608p1asdflr9QiJ_cY80WjaFZ6cB-IJGLT6rpSdiH6bQwnxAEDGTJ1k4K3-A4Y6327iyepbXL6d3fnomtBcWXPQ7A5Px1zckZGBoo8gtMrixSGneodtc7IIaxSu; SID=DQAAALcAAACa0eOu2S9ezDasdfx32stdYzKQQCc7Q4dcYucZkXOaQkXKmfkr0iMlPQZkwy4PlQLzZsiO_5_lLDclyBDJsJIKU0my000owlYMX14K22pBopTN1EUlOrJ7LIkwhznasdfBleSojFfhMbn0BoYM1WAzwnpMAttoAuzG0bZXcScgZkDizC2FUHXVV3-eHZPrS2ncychNguPNZ_M9V_oEtoqJUmqasdf_kaKTOM2KnT0P5wMswKru8_KrkwK6iCc7ag; HSID=A78ACtAr9H6MYp-dn
Cache-Control: max-age=0

我想在node.js中获得响应。有人可以指出我的方向是正确的吗?

非常感谢,

2 个答案:

答案 0 :(得分:2)

开始的地方是http.request的{​​{3}}。

答案 1 :(得分:1)

是代理吗?如果是这样,那么你可以使用这样的代理:

var net = require('net');

// Create TCP-server
var server = net.createServer( function(soc){ // soc is socket generated by net.Server

  // Incoming request processing
  soc.on('data', function(data){


    // Create new socket
    var client = net.Socket();

    // Get host from HTTP headers
    var re = /[^\n]+(\r\n|\r|\n)host:\w*([^:\r\n]+)/i;
    var host = data.toString('utf-8').match(re);

    // Pause soc for inner socket connection
    soc.pause();

    // Connect to Node.js application
    client.connect(80, host);

    client.on('connect', function()
    {
      // Write request to your node.js application
      client.write(data.toString('utf-8'));
    });

    client.on('data', function(cdata)
    {
      // Return socket to live
      soc.resume();

      // Write client data to browser
      soc.write(cdata.toString('utf-8'));
      soc.pipe(soc);
      soc.end();
    });

    client.on('end', function(){
      client.destroy();
    });
  });
}
);

server.on('error', function (err){
  // Error processing i just pass whole object
  console.log(err);
});

server.listen(8088);
console.log('Server is listening %d\n', 8088);