博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
nodejs 使用代理发送http/https请求
阅读量:4987 次
发布时间:2019-06-12

本文共 4638 字,大约阅读时间需要 15 分钟。

http的比较简单:

var Http = require('http'); var req = Http.request(    {        host: '192.168.5.8', // 代理 IP        port: 3128, // 代理端口        method: 'GET',        path: 'http://baidu.com' // 要访问的url    },    function(res)    {        res.on('data', function(data)        {            console.log(data.toString());        });    }); req.end();

 

 

https的要复杂一些: https://gist.github.com/matthias-christen/6beb3b4dda26bd6a221d

var Util = require('util');var Https = require('https');var Tls = require('tls'); function HttpsProxyAgent(options){    Https.Agent.call(this, options);     this.proxyHost = options.proxyHost;    this.proxyPort = options.proxyPort;     this.createConnection = function(opts, callback)    {        // do a CONNECT request        var req = Http.request({            host: options.proxyHost,            port: options.proxyPort,            method: 'CONNECT',            path: opts.host + ':' + opts.port,            headers: {                host: opts.host            }        });         req.on('connect', function(res, socket, head)        {            var cts = Tls.connect(                {                    host: opts.host,                    socket: socket                },                function()                {                    callback(false, cts);                }            );        });         req.on('error', function(err)        {            callback(err, null);        });         req.end();    }} Util.inherits(HttpsProxyAgent, Https.Agent); // Almost verbatim copy of http.Agent.addRequestHttpsProxyAgent.prototype.addRequest = function(req, host, port, localAddress){    var name = host + ':' + port;    if (localAddress)        name += ':' + localAddress;     if (!this.sockets[name])        this.sockets[name] = [];     if (this.sockets[name].length < this.maxSockets)    {        // if we are under maxSockets create a new one.        this.createSocket(name, host, port, localAddress, req, function(socket)        {            req.onSocket(socket);        });    }    else    {        // we are over limit so we'll add it to the queue.        if (!this.requests[name])            this.requests[name] = [];        this.requests[name].push(req);    }}; // Almost verbatim copy of http.Agent.createSocketHttpsProxyAgent.prototype.createSocket = function(name, host, port, localAddress, req, callback){    var self = this;    var options = Util._extend({}, self.options);    options.port = port;    options.host = host;    options.localAddress = localAddress;     options.servername = host;    if (req)    {        var hostHeader = req.getHeader('host');        if (hostHeader)            options.servername = hostHeader.replace(/:.*$/, '');    }     self.createConnection(options, function(err, s)    {        if (err)        {            err.message += ' while connecting to HTTP(S) proxy server ' + self.proxyHost + ':' + self.proxyPort;             if (req)                req.emit('error', err);            else                throw err;             return;        }         if (!self.sockets[name])            self.sockets[name] = [];         self.sockets[name].push(s);         var onFree = function()        {            self.emit('free', s, host, port, localAddress);        };         var onClose = function(err)        {            // this is the only place where sockets get removed from the Agent.            // if you want to remove a socket from the pool, just close it.            // all socket errors end in a close event anyway.            self.removeSocket(s, name, host, port, localAddress);        };         var onRemove = function()        {            // we need this function for cases like HTTP 'upgrade'            // (defined by WebSockets) where we need to remove a socket from the pool            // because it'll be locked up indefinitely            self.removeSocket(s, name, host, port, localAddress);            s.removeListener('close', onClose);            s.removeListener('free', onFree);            s.removeListener('agentRemove', onRemove);        };         s.on('free', onFree);        s.on('close', onClose);        s.on('agentRemove', onRemove);         callback(s);  });};

 

var HttpsProxyAgent = require("./HttpsProxyAgent");var Https = require('https');var agent = new HttpsProxyAgent({    proxyHost: '210.42.41.8',    proxyPort: 8090});Https.request(    {        host: 'github.com',        port: 443,        method: 'GET',        path: '/',        agent: agent    },    function(res)    {        res.on('data', function(data)        {            console.log(data.toString());        });    }).end();

 

转载于:https://www.cnblogs.com/ference/p/4014210.html

你可能感兴趣的文章
Python 3 函数分类
查看>>
通过.frm表结构和.ibd文件恢复数据
查看>>
R语言之——字符串处理函数
查看>>
架构师速成5.1-小学gtd进阶
查看>>
Spring-aop(一)
查看>>
ucos在xp平台下开发环境搭建
查看>>
python基础入门while循环 格式化 编码初识
查看>>
cmake方式使用vlfeat
查看>>
windows下用纯C实现一个简陋的imshow:基于GDI
查看>>
struts2 自定义类型转换器
查看>>
cocos2d-x xna在有vs2012和vs2010的情况下的环境部署
查看>>
43-安装 Docker Machine
查看>>
c++学习(三):表达式和语句
查看>>
laravel框架基础知识总结
查看>>
nginx: 响应体太大
查看>>
字符串反混淆实战 Dotfuscator 4.9 字符串加密技术应对策略
查看>>
单例模式
查看>>
Robotium源码分析之Instrumentation进阶
查看>>
Android 交错 GridView
查看>>
(2)把BlackBerry作为插件安装到已有的Eclipse中
查看>>