注册
登录
 文档中心 产品介绍 开发指南 API接口 代码样例 使用帮助
Node.js使用标准库调用Http代理IP的代码样例
站大爷 官方 2024-01-20 1329 浏览

温馨提示:

1.  http,https均适用


标准库(http+url)

const http = require("http");  // 引入内置http模块
const url  = require("url");

// 要访问的目标页面
const targetUrl = "http://example.com";
const urlParsed   = url.parse(targetUrl);

// 代理ip
const proxyIp = "proxyIp";  // 代理服务器ip
const proxyPort = "proxyPort"; // 代理服务器端口

// 用户名密码授权
const username = "username";
const password = "password";
const base64    = new Buffer.from(username + ":" + password).toString("base64");
const options = {
	host    : proxyIp,
	port    : proxyPort,
	path    : targetUrl,
	method  : "GET",
	headers : {
		"Host"                : urlParsed.hostname,
		"Proxy-Authorization" : "Basic " + base64
	}
};

http.request(options,  (res) => {
		console.log("got response: " + res.statusCode);
		// 输出返回内容(使用了gzip压缩)
		if (res.headers['content-encoding'] && res.headers['content-encoding'].indexOf('gzip') != -1) {
			let zlib = require('zlib');
			let unzip = zlib.createGunzip();
			res.pipe(unzip).pipe(process.stdout);
		} else {
			// 输出返回内容(未使用gzip压缩)
			res.pipe(process.stdout);
		}
	})
	.on("error", (err) => {
		console.log(err);
	})
    .end()
;

标准库(http+tls+util)

let http = require('http'); // 引入内置http模块
let tls = require('tls'); // 引入内置tls模块
let util = require('util');

// 用户名密码授权
const username = 'username';
const password = 'password';
const auth = 'Basic ' + new Buffer.from(username + ':' + password).toString('base64');

// 代理服务器ip和端口
let proxy_ip = '159.138.141.125';
let proxy_port = 22916;

// 要访问的主机和路径
let remote_host = 'https://example.com';
let remote_path = '/';

// 发起CONNECT请求
let req = http.request({
	host: proxy_ip,
	port: proxy_port,
	method: 'CONNECT',
	path: util.format('%s:443', remote_host),
	headers: {
		"Host": remote_host,
		"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3100.0 Safari/537.36",
		"Proxy-Authorization": auth,
		"Accept-Encoding": "gzip"   // 使用gzip压缩让数据传输更快
	}
});


req.on('connect', function (res, socket, head) {
	// TLS握手
	let tlsConnection = tls.connect({
		host: remote_host,
		socket: socket
	}, function () {
		// 发起GET请求
		tlsConnection.write(util.format('GET %s HTTP/1.1\r\nHost: %s\r\n\r\n', remote_path, remote_host));
	});

	tlsConnection.on('data', function (data) {
		// 输出响应结果(完整的响应报文串)
		console.log(data.toString());
	});
});

req.end();


立即注册站大爷用户,免费试用全部产品
立即注册站大爷用户,免费试用全部产品