Node语言代码示例
发布时间:2022-07-03 00:49
nodejs http
const http = require("http");
const url = require("url");
-
const targetURL = url.parse("http://myip.ipip.net");
const proxyIp = "219.151.125.106";
const proxyPort = "31615";
const authKey = "895314XY";
const password = "24D6YB309ZCB";
-
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 = "http://myip.ipip.net";
const proxyIp = "219.151.125.106";
const proxyPort = 31615;
const authKey = "895314XY";
const password = "24D6YB309ZCB";
-
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 = "http://myip.ipip.net";
const proxyIp = "219.151.125.106";
const proxyPort = 31615;
const authKey = "895314XY";
const password = "24D6YB309ZCB";
-
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 targetUrl = "http://myip.ipip.net";
const proxyIp = "219.151.125.106";
const proxyPort = 31615;
const authKey = "895314XY";
const password = "24D6YB309ZCB";
-
var proxy = {
host: proxyIp,
port: proxyPort,
auth: {
username: authKey,
password: password
}
};
-
axios.get(targetUrl, {proxy:proxy}).then(function (response) {
console.log("response body: " + response.data);
}).catch(function (error) {
console.log("request failed: " + error);
}).finally(function () {
console.log("request finished.")
});
相关文章