search

Home  >  Q&A  >  body text

node.js - nodejs http.get如何使用proxy代理

在使用request模块的时候

var targetOptions = {
        method: 'GET',
        url: 'http://url',
        timeout: 8000,
        encoding: null,
    };
targetOptions.proxy = 'http://213.183.252.156:8081'; //代理服务器
request(targetOptions, function (error, req,body) {
})

可以使用代理进行HTTP请求

在HTTP模块中 http.get
是否也可以达到如上的效果?

大家讲道理大家讲道理2786 days ago503

reply all(1)I'll reply

  • 大家讲道理

    大家讲道理2017-04-17 15:58:53

    Yes, refer to the code

    var http = require('http');
    
    var options = {
        hostname : '213.183.252.156',
        port     : 8081,
        path     : 'imququ.com:80',
        method     : 'CONNECT'
    };
    
    var req = http.request(options);
    
    req.on('connect', function(res, socket) {
        socket.write('GET / HTTP/1.1\r\n' +
                     'Host: imququ.com\r\n' +
                     'Connection: Close\r\n' +
                     '\r\n');
    
        socket.on('data', function(chunk) {
            console.log(chunk.toString());
        });
    
        socket.on('end', function() {
            console.log('socket end.');
        });
    });
    
    req.end();

    For details, please see imququ’s summary of relevant knowledge about http proxy, it’s very well explained
    https://imququ.com/post/web-p...

    reply
    0
  • Cancelreply