使用Node.js通过API接口获取代理IP的代码样例

温馨提示:
1. API提取链接为ipip9控制台实例管理里生成,请勿直接复制样例使用

参考样例

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

let api_url = 'https://17178.org/xc.php?userpass=xxx:xxx&type=sticky&protocol=socks5&quantity=10&format=us.ipip3.com:port:login:password&session_ttl=30';  // 要访问的目标网页

// 采用gzip压缩, 使速度更快
let options = {
	"headers" : {
		"Accept-Encoding": "gzip"
	}
};

// 发起请求
http.get(api_url, options, (res) => {

	// 若有gzip压缩, 则解压缩再输出
	if (res.headers['content-encoding'] && res.headers['content-encoding'].indexOf('gzip') != -1) {
		let zlib = require('zlib');
		let gunzipStream = zlib.createGunzip();
		res.pipe(gunzipStream).pipe(process.stdout);
	} else {
		// 无gzip压缩,直接输出
		res.pipe(process.stdout);
	}

}).on("error", (err) => {
	// 错误处理
	console.log("Error: " + err.message);
})