Nodejs
const http = require("http");
const url = require("url");
// 要访问的目标页面
const targetUrl = "http://httpbin.org/ip";
const urlParsed = url.parse(targetUrl);
// 代理服务器(产品官网 www.duoip.cn)
const proxyHost = "tunnel.hahado.cn";
const proxyPort = "36600";
// 生成一个随机 proxy tunnel
var seed = 1;
function random() {
var x = Math.sin(seed++) * 10000;
return x - Math.floor(x);
}
const tunnel = random()*100;
// 代理验证信息
const proxyUser = "username";
const proxyPass = "password";
const base64 = new Buffer.from(proxyUser + ":" + proxyPass).toString("base64");
const options = {
host: proxyHost,
port: proxyPort,
path: targetUrl,
method: "GET",
headers: {
"Host": urlParsed.hostname,
"Proxy-Tunnel": tunnel,
"Proxy-Authorization" : "Basic " + base64
}
};
http.request(options, function (res) {
console.log("got response: " + res.statusCode);
res.pipe(process.stdout);
}).on("error", function (err) {
console.log(err);
}).end();
const https = require("https");
const url = require("url");
const httpsProxyAgent = require('https-proxy-agent');
// 要访问的目标页面
const targetUrl = "https://httpbin.org/ip";
const urlParsed = url.parse(targetUrl);
// 代理服务器(产品官网 www.duoip.cn)
const proxyHost = "tunnel.hahado.cn";
const proxyPort = "31111";
// 代理验证信息
const proxyUser = "username";
const proxyPass = "password";
var options = urlParsed;
var agent = new httpsProxyAgent("http://" + proxyUser + ":" + proxyPass + "@" + proxyHost + ":" + proxyPort);
options.agent = agent;
https.request(options, function (res) {
console.log("got response: " + res.statusCode);
res.pipe(process.stdout);
}).on("error", function (err) {
console.log(err);
}).end();
const https = require("https");
const url = require("url");
const httpsProxyAgent = require('https-proxy-agent');
// 要访问的目标页面
const targetUrl = "https://httpbin.org/ip";
const urlParsed = url.parse(targetUrl);
// 代理服务器(产品官网 www.duopip.cn)
const proxyHost = "tunnel.hahado.cn";
const proxyPort = "31111";
// 代理验证信息
const proxyUser = "username";
const proxyPass = "password";
var options = urlParsed;
const proxy_url = "http://" + proxyUser + ":" + proxyPass + "@" + proxyHost + ":" + proxyPort;
var agent_options = url.parse(proxy_url);
agent_options.headers = { "Proxy-Tunnel" : "1" };
var agent = new httpsProxyAgent(agent_options);
options.agent = agent;
for(var i=0;i<10;i++){
https.request(options, function (res) {
console.log("got response: " + res.statusCode);
res.pipe(process.stdout);
}).on("error", function (err) {
console.log(err);
}).end();
}
const request = require("request");
// 要访问的目标页面
const targetUrl = "http://httpbin.org/ip";
// 代理服务器(产品官网 www.duoip.cn)
const proxyHost = "tunnel.hahado.cn";
const proxyPort = "31111";
// 代理验证信息
const proxyUser = "username";
const proxyPass = "password";
const proxyUrl = "http://" + proxyUser + ":" + proxyPass + "@" + proxyHost + ":" + proxyPort;
const proxiedRequest = request.defaults({'proxy': proxyUrl});
const options = {
url : targetUrl,
headers : {
}
};
proxiedRequest
.get(options, function (err, res, body) {
console.log("got response: " + res.statusCode);
})
.on("error", function (err) {
console.log(err);
})
;
const request = require("superagent");
require("superagent-proxy")(request);
// 要访问的目标页面
const targetUrl = "http://httpbin.org/ip";
// 代理服务器(产品官网 www.duoip.cn)
const proxyHost = "tunnel.hahado.cn";
const proxyPort = 31111;
// 代理验证信息
const proxyUser = "username";
const proxyPass = "password";
const proxyUrl = "http://" + proxyUser + ":" + proxyPass + "@" + proxyHost + ":" + proxyPort;
request
.get(targetUrl)
.proxy(proxyUrl)
.end(function onResponse(err, res) {
if (err) {
return console.log(err);
}
console.log(res.status, res.headers);
console.log(res.text);
})
;
const axios = require('axios');
// 要访问的目标页面
const targetUrl = "http://httpbin.org/ip";
const targetHttpsUrl = "https://httpbin.org/ip";
// 代理服务器(产品官网 www.duoip.cn)
const proxyHost = "tunnel.hahado.cn";
const proxyPort = 31111;
// 代理验证信息
const proxyUser = "username";
const proxyPass = "password";
var proxy = {
host: proxyHost,
port: proxyPort,
auth: {
username: proxyUser,
password: proxyPass
}
};
axios.get(targetUrl,{proxy:proxy})
.then(function (response) {
// handle success
console.log(response.data);
})
.catch(function (error) {
// handle error
console.log(error);
})
.finally(function () {
// always executed
});
// 目标为https网站 axios库支持有bug,不推荐使用
// 具体参看 https://github.com/axios/axios/issues/4531
最后修改时间: 2 个月前