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

温馨提示:

1.  API提取链接为站大爷控制台实例管理里生成,请勿直接复制样例使用


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

let api_url = 'http://www.***.com/ShortProxy/GetIP/?api=1234567890&akey=8a17ca305f683620&count=1&timespan=3&type=1';  // 要访问的目标网页

// 采用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);
})


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