-
产品介绍
-
购买指南
-
使用指南
-
快速入门
-
电脑如何使用代理IP
-
手机如何使用代理IP
-
浏览器如何使用代理IP
-
软件如何使用代理IP
-
代码调用方式示例
-
常见问题
Node语言代码示例
发布时间:2021-12-08 16:15
nodejs http
const http = require("http");
const url = require("url");
const targetURL = url.parse("https://myip.top");
const proxyIp = "您的代理IP";
const proxyPort = 端口号;
const authKey = "请改成您的Key";
const password = "请改成您的AuthPwd";
const base64 = new Buffer.from(authKey + ":" + password).toString("base64");
const options = {
host: proxyIp,
port: proxyPort,
path: targetURL,
method: "GET",
headers: {
"Host": urlParsed.hostname,
"Proxy-Authorization": "Basic " + base64
}
};
http.request(options, function (resp) {
console.log("response status code: " + resp.statusCode);
resp.pipe(process.stdout);
}).on("error", function (err) {
console.log("request failed: " + err);
}).end();
nodejs request
const request = require("request");
const targetUrl = "https://myip.top";
const proxyIp = "您的代理IP";
const proxyPort = 端口号;
const authKey = "请改成您的Key";
const password = "请改成您的AuthPwd";
const proxyUrl = "http://" + authKey + ":" + password + "@" + proxyIp + ":" + proxyPort;
const req = request.defaults({'proxy': proxyUrl});
const options = {
url: targetUrl,
headers: {}
};
req.get(options, function (err, resp, body) {
if (err) {
return console.log(err);
}
console.log("response status code: " + resp.statusCode);
console.log("response body: " + body);
}).on("error", function (err) {
console.log("request failed: " + err);
});
nodejs superagent
const request = require("superagent");
require("superagent-proxy")(request);
const targetUrl = "https://myip.top";
const proxyIp = "您的代理IP";
const proxyPort = 端口号;
const authKey = "请改成您的Key";
const password = "请改成您的AuthPwd";
const proxyUrl = "http://" + authKey + ":" + password + "@" + proxyIp + ":" + proxyPort;
request.get(targetUrl).proxy(proxyUrl).end(function onResponse(err, resp) {
if (err) {
return console.log(err);
}
console.log("response status code: " + resp.statusCode);
console.log("response body: " + resp.text);
});
nodejs axios
const axios = require("axios")
const {HttpsProxyAgent} = require("https-proxy-agent")
const targetUrl = "http://myip.top"
const proxyIp = "您的代理IP"
const proxyPort = 端口号
const authKey = "请改成您的Key"
const password = "请改成您的AuthPwd"
const proxy = new HttpsProxyAgent(`http://${authKey}:${password}@${proxyIp}:${proxyPort}`)
axios.get(targetUrl, {
httpAgent: proxy,
httpsAgent: proxy,
}).then(function (response) {
console.log("response body: " + response.data)
}).catch(function (error) {
console.log("request failed: " + error)
}).finally(function () {
console.log("request finished.")
})
// 如果目标站是HTTPS,则需要使用下面的代码进行代理请求
let httpsProxyAgent = require('https-proxy-agent');
var agent = new httpsProxyAgent(`http://${authKey}:${password}@${proxyIp}:${proxyPort}`);
var config = {
url: "https://myip.top",
httpsAgent: agent
}
axios.request(config)
.then(function(response) {
console.log("response body: " + response.data)
}).catch(function(error) {
console.log("request failed: " + error)
})
相关文章
微信客服