Node语言代码示例
发布时间:2023-03-27 21:37
nodejs http
const http = require("http");
const url = require("url");
const targetURL = url.parse("https://ip.cn/api/index?ip=&type=0");
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://ip.cn/api/index?ip=&type=0";
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://ip.cn/api/index?ip=&type=0";
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://ip.cn/api/index?ip=&type=0"
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://ip.cn/api/index?ip=&type=0",
httpsAgent: agent
}
axios.request(config)
.then(function(response) {
console.log("response body: " + response.data)
}).catch(function(error) {
console.log("request failed: " + error)
})
相关文章