如何使用nodejs连接到代理服务器并通过代理进行http.request

How to connect to a proxy server and make an http.request via the proxy using nodejs?

本文关键字:代理 request http nodejs 何使用 连接 代理服务器      更新时间:2023-09-26

我不想让我的服务器代理做任何我想连接到代理并通过它发送http请求的事情。

示例:

proxy.connect(someip:someport,function(){
    console.log('[PM]['+this._account+'] Logging in..');
    var auth_re = /auth'.chatango'.com ?= ?([^;]*)/;
    var data = querystring.stringify({user_id: this._account, password: this._password, storecookie: 'on', checkerrors: 'yes'});
    var options = {hostname: 'chatango.com', port: 80, path: '/login', method: 'POST', headers: {'Connection': 'keep-alive', 'Content-Length': data.length, 'Cache-Control': 'max-age=0', 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8', 'Origin': 'http://chatango.com', 'User-Agent': 'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.104 Safari/537.36', 'Content-Type': 'application/x-www-form-urlencoded', 'Referer': 'http://chatango.com/login', 'Accept-Encoding': 'gzip,deflate', 'Accept-Language': 'en-US,en;q=0.8'}};
    var self = this;
    var req = http.request(options, function(res) {
        res.setEncoding('utf8');
        if (res.headers['set-cookie'][2]) {
            var m = auth_re.exec(res.headers['set-cookie'][2]);
            if (m) return callback(m[1]);
        }
        callback(false);
    }).on('error', function(e) {
        callback(false);
    });
    req.write(data);
    req.end();
});

我不知道它是否会看起来像那样,但我厌倦了看到创建代理的答案我只想连接到一个而不是创建一个

http代理,因为登录页面使用http请求

我找到了我的答案

console.log('[PM]['+this._account+'] Logging in..');
var auth_re = /auth'.chatango'.com ?= ?([^;]*)/;
var data = querystring.stringify({user_id: this._account, password: this._password, storecookie: 'on', checkerrors: 'yes'});
var options = {hostname: 'chatango.com', port: 80, path: '/login', method: 'POST', headers: {'Connection': 'keep-alive', 'Content-Length': data.length, 'Cache-Control': 'max-age=0', 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8', 'Origin': 'http://chatango.com', 'User-Agent': 'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.104 Safari/537.36', 'Content-Type': 'application/x-www-form-urlencoded', 'Referer': 'http://chatango.com/login', 'Accept-Encoding': 'gzip,deflate', 'Accept-Language': 'en-US,en;q=0.8'}};
var self = this;
if(self.proxy.url && self.proxy.port){
    globaltunnel.initialize({
        host: self.proxy.url,
        port: self.proxy.port,
        tunnel: 'neither',
        protocol: 'http'
    });
}
var req = http.request(options, function(res) {
    res.setEncoding('utf8');
    if (res.headers['set-cookie'][2]) {
        var m = auth_re.exec(res.headers['set-cookie'][2]);
        if (m) return callback(m[1]);
    }
    callback(false);
}).on('error', function(e) {
    console.log(e);
    callback(false);
});
req.write(data);
req.end();
if(self.proxy.url && self.proxy.port){
    globaltunnel.end();
}