[ 登录 ] - [ 注册 ] | 免费代理IP 数据无忧_无忧代理IP_DATA5U最新上线 | 代码示例DEMO | IP测试视频 | 用户协议
NodeJS代码调用示例·Https代理(API)

/**
 * 请确保安装了request和bluebird两个模块
 * 安装模块:打开NODEJS-->输入npm install request-->输入npm install bluebird
 **/
 
var request = require("request");
var Promise = require("bluebird");

// 请填写无忧代理订单号
var order = 'please-input-your-order-here';
// 要测试的网址
var targetURL = 'http://pv.sohu.com/cityjson?ie=utf-8';
// 请求超时时间
var timeout = 8000;
// 测试次数
var testTime = 5;
// 间隔多少毫秒调用一次接口
var sleepTime = 5000;

var apiURL = 'http://api.ip.data5u.com/dynamic/get.html?order=' + order + '&sep=3';

console.log('>>>> start test dynamic ip');

function getProxyList() {
    return new Promise((resolve, reject) => {
        var options = {
            method: 'GET',
            url: apiURL,
            gzip: true,
            encoding: null,
            headers: {},
        };

        request(options, function (error, response, body) {
            try {
                if (error) throw error;
                var ret = (body + '').match(/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}:\d{1,5}/g);
                resolve(ret);
            } catch (e) {
                return reject(e);
            }
        });
    });
}

function execute(){
	getProxyList().then(function (proxyList) {
		var targetOptions = {
			method: 'GET',
			url: targetURL,
			timeout: timeout,
			encoding: null,
		};

		proxyList.forEach(function (proxyurl) {
			console.log(`* testing `);
			var startTimestamp = (new Date()).valueOf();
			targetOptions.proxy = 'http://' + proxyurl;
			request(targetOptions, function (error, response, body) {
				try {
					if (error) throw error;
					body = body.toString();
					var endTimestamp = (new Date()).valueOf();
					console.log('  > time ' + (endTimestamp - startTimestamp) + 'ms ' + body);
				} catch (e) {
					console.error(e);
				}
			});
		});
	}).catch(e => {
		console.log(e);
	})
}

// 定时执行
var interval = setInterval(function(){
	if(testTime > 0){
		execute()
	} else {
		clearInterval(interval);
		console.log('<<<< end test dynamic ip');
	}
	testTime = testTime - 1;
}, sleepTime);


NodeJS代码调用示例·Https代理(动态转发)

/**
 * 先运行npm install moment安装moment模块,运行npm install crypto安装crypto模块
 */

var request = require("request");
var moment = require("moment");
var crypto = require('crypto');

var options = {};


var raw = "替换成你的IP提取码:替换成你的动态转发密码";

options.method = "GET";
options.url = "https://www.so.com";
options.headers = {
    "Proxy-Authorization": "Basic " + new Buffer(raw).toString('base64')
};
options.rejectUnauthorized=false;
options.proxy = 'http://tunnel.data5u.com:56789';

var startTimestamp = (new Date()).valueOf();
request(options, function (error, response, body) {
	try {
		if (error) throw error;
		body = body.toString();
		var endTimestamp = (new Date()).valueOf();
		console.log('  > time ' + (endTimestamp - startTimestamp) + 'ms ' + body);
	} catch (e) {
		console.error(e);
	}
});

微信公众号
关注微信公众号